fix and update

This commit is contained in:
lendry
2026-06-30 23:43:49 +03:00
parent 4b2ade9354
commit deb213bd77
6 changed files with 60 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
'use client';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useFamilyOverlay } from '@/components/family/family-overlay-provider';
import { useAuth } from '@/components/id/auth-provider';
import {
@@ -15,23 +15,26 @@ import {
export function useSelectedFamily(enabled = true) {
const { user, token, isPinLocked, isLoading, isContentReady } = useAuth();
const { selectedGroupId, setSelectedGroupId } = useFamilyOverlay();
const userId = user?.id;
const [groups, setGroups] = useState<FamilyGroup[]>([]);
const [group, setGroup] = useState<FamilyGroup | null>(null);
const [presenceMembers, setPresenceMembers] = useState<FamilyPresenceMember[]>([]);
const [loading, setLoading] = useState(true);
const hasLoadedRef = useRef(false);
const refresh = useCallback(async () => {
const accessToken = getAccessToken() ?? token?.trim() ?? null;
if (!enabled || !user || !accessToken || isPinLocked || isLoading || !isContentReady) {
if (!enabled || !userId || !accessToken || isPinLocked || isLoading || !isContentReady) {
setGroups([]);
setGroup(null);
setPresenceMembers([]);
hasLoadedRef.current = false;
setLoading(false);
return;
}
try {
const groupsResponse = await fetchFamilyGroups(user.id, accessToken);
const groupsResponse = await fetchFamilyGroups(userId, accessToken);
const list = groupsResponse.groups ?? [];
setGroups(list);
@@ -39,6 +42,7 @@ export function useSelectedFamily(enabled = true) {
setGroup(null);
setPresenceMembers([]);
if (selectedGroupId) setSelectedGroupId(null);
hasLoadedRef.current = true;
return;
}
@@ -55,19 +59,26 @@ export function useSelectedFamily(enabled = true) {
]);
setGroup(groupResponse);
setPresenceMembers(presenceResponse.members ?? []);
hasLoadedRef.current = true;
} catch {
setGroups([]);
setGroup(null);
setPresenceMembers([]);
// Фоновая ошибка не должна сбрасывать уже показанную семью в сайдбаре.
} finally {
setLoading(false);
}
}, [enabled, isContentReady, isLoading, isPinLocked, selectedGroupId, setSelectedGroupId, token, user]);
}, [enabled, isContentReady, isLoading, isPinLocked, selectedGroupId, setSelectedGroupId, token, userId]);
useEffect(() => {
setLoading(true);
if (!enabled || !userId || !token || isPinLocked || isLoading || !isContentReady) {
setGroups([]);
setGroup(null);
setPresenceMembers([]);
hasLoadedRef.current = false;
setLoading(false);
return;
}
setLoading(!hasLoadedRef.current);
void refresh();
}, [refresh]);
}, [enabled, isContentReady, isLoading, isPinLocked, refresh, token, userId]);
useEffect(() => {
const accessToken = getAccessToken() ?? token?.trim() ?? null;