fix and update

This commit is contained in:
lendry
2026-06-30 19:47:26 +03:00
parent d41c9d1121
commit 6ee3ffe0a5
6 changed files with 57 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import * as React from 'react';
import { Loader2, Trash2 } from 'lucide-react';
import { useAuth } from '@/components/id/auth-provider';
import { fetchDocumentPhotoUrl } from '@/lib/api';
import { resolveBrowserMediaUrl } from '@/lib/resilient-media-fetch';
import { DocumentFullscreenViewer, DocumentPhotoThumbnail } from './document-photo-viewer';
interface DocumentPhotoGalleryProps {
@@ -33,7 +34,7 @@ export function DocumentPhotoGallery({ userId, token, storageKeys, readOnly, onR
storageKeys.map(async (storageKey) => {
try {
const response = await fetchDocumentPhotoUrl(userId, storageKey, token);
return { storageKey, accessUrl: response.accessUrl };
return { storageKey, accessUrl: resolveBrowserMediaUrl(response.accessUrl) };
} catch {
return { storageKey, accessUrl: '' };
}

View File

@@ -5,6 +5,9 @@ import { useAuth } from '@/components/id/auth-provider';
import { ensureApiGatewayReady, fetchUnreadNotificationCount, getWsUrl, type ChatMessage } from '@/lib/api';
import { dispatchAvatarUpdated, dispatchChatRoomAvatarUpdated } from '@/lib/avatar-events';
const WS_RECONNECT_BASE_MS = 2_000;
const WS_RECONNECT_MAX_MS = 30_000;
export interface RealtimeEvent {
userId?: string;
type: string;
@@ -37,6 +40,7 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
const listenersRef = React.useRef(new Set<Listener>());
const socketRef = React.useRef<WebSocket | null>(null);
const reconnectTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null);
const reconnectAttemptRef = React.useRef(0);
const subscribe = React.useCallback((listener: Listener) => {
listenersRef.current.add(listener);
@@ -76,24 +80,35 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
let cancelled = false;
function scheduleReconnect() {
if (cancelled) return;
if (reconnectTimer.current) clearTimeout(reconnectTimer.current);
const attempt = reconnectAttemptRef.current;
const delay = Math.min(WS_RECONNECT_MAX_MS, WS_RECONNECT_BASE_MS * 2 ** Math.min(attempt, 4));
reconnectAttemptRef.current = attempt + 1;
reconnectTimer.current = setTimeout(connect, delay);
}
async function connect() {
if (cancelled) return;
try {
await ensureApiGatewayReady();
} catch {
// Пробуем подключиться даже если health не ответил в срок.
scheduleReconnect();
return;
}
if (cancelled) return;
const url = `${getWsUrl()}?token=${encodeURIComponent(token!)}`;
const socket = new WebSocket(url);
socketRef.current = socket;
socket.onopen = () => setConnected(true);
socket.onopen = () => {
reconnectAttemptRef.current = 0;
setConnected(true);
};
socket.onclose = () => {
setConnected(false);
if (!cancelled) {
reconnectTimer.current = setTimeout(connect, 3000);
}
scheduleReconnect();
};
socket.onerror = () => socket.close();
socket.onmessage = (event) => {
@@ -136,6 +151,7 @@ export function RealtimeProvider({ children }: { children: React.ReactNode }) {
return () => {
cancelled = true;
if (reconnectTimer.current) clearTimeout(reconnectTimer.current);
reconnectAttemptRef.current = 0;
socketRef.current?.close();
socketRef.current = null;
setConnected(false);