fix and update

This commit is contained in:
lendry
2026-06-29 21:22:20 +03:00
parent 7233e8b70a
commit 8369abb023
7 changed files with 98 additions and 23 deletions

View File

@@ -27,7 +27,7 @@ function formatTime(value: string) {
export function NotificationBell() {
const router = useRouter();
const { token, isPinLocked } = useAuth();
const { token, isPinLocked, isLoading } = useAuth();
const { showToast } = useToast();
const { unreadCount, setUnreadCount, subscribe } = useRealtime();
const [open, setOpen] = useState(false);
@@ -36,7 +36,7 @@ export function NotificationBell() {
const [respondingId, setRespondingId] = useState<string | null>(null);
const load = useCallback(async () => {
if (!token || isPinLocked) return;
if (!token || isPinLocked || isLoading) return;
setLoading(true);
try {
const [list, count] = await Promise.all([fetchNotifications(token), fetchUnreadNotificationCount(token)]);
@@ -48,7 +48,7 @@ export function NotificationBell() {
} finally {
setLoading(false);
}
}, [isPinLocked, setUnreadCount, showToast, token]);
}, [isLoading, isPinLocked, setUnreadCount, showToast, token]);
useEffect(() => {
void load();

View File

@@ -2,7 +2,7 @@
import * as React from 'react';
import { useAuth } from '@/components/id/auth-provider';
import { getWsUrl, type ChatMessage } from '@/lib/api';
import { ensureApiGatewayReady, getWsUrl, type ChatMessage } from '@/lib/api';
import { dispatchAvatarUpdated, dispatchChatRoomAvatarUpdated } from '@/lib/avatar-events';
export interface RealtimeEvent {
@@ -31,7 +31,7 @@ interface RealtimeContextValue {
const RealtimeContext = React.createContext<RealtimeContextValue | null>(null);
export function RealtimeProvider({ children }: { children: React.ReactNode }) {
const { user, token, isPinLocked } = useAuth();
const { user, token, isPinLocked, isLoading } = useAuth();
const [unreadCount, setUnreadCount] = React.useState(0);
const [connected, setConnected] = React.useState(false);
const listenersRef = React.useRef(new Set<Listener>());
@@ -48,7 +48,7 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
}, []);
React.useEffect(() => {
if (!user || !token || isPinLocked) {
if (!user || !token || isPinLocked || isLoading) {
socketRef.current?.close();
socketRef.current = null;
setConnected(false);
@@ -57,7 +57,13 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
let cancelled = false;
function connect() {
async function connect() {
if (cancelled) return;
try {
await ensureApiGatewayReady();
} catch {
// Пробуем подключиться даже если health не ответил в срок.
}
if (cancelled) return;
const url = `${getWsUrl()}?token=${encodeURIComponent(token!)}`;
const socket = new WebSocket(url);
@@ -105,7 +111,7 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
};
}
connect();
void connect();
return () => {
cancelled = true;
@@ -114,7 +120,7 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
socketRef.current = null;
setConnected(false);
};
}, [emit, isPinLocked, token, user]);
}, [emit, isLoading, isPinLocked, token, user]);
return (
<RealtimeContext.Provider value={{ unreadCount, setUnreadCount, subscribe, connected }}>