From 9c8a83b83ecda7c9b951e418acda3c01e9378798 Mon Sep 17 00:00:00 2001 From: Vukasin Date: Fri, 13 Jun 2025 19:51:30 +0200 Subject: [PATCH] fix: share page file load --- .../react-query/files/use-file-queries.ts | 19 +++++++++++-------- frontend/src/hooks/use-cached-file.ts | 9 ++++++--- frontend/src/hooks/use-file-content.ts | 4 ++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/frontend/src/hooks/react-query/files/use-file-queries.ts b/frontend/src/hooks/react-query/files/use-file-queries.ts index 56253fbf..9674079a 100644 --- a/frontend/src/hooks/react-query/files/use-file-queries.ts +++ b/frontend/src/hooks/react-query/files/use-file-queries.ts @@ -117,10 +117,13 @@ export async function fetchFileContent( console.log(`[FILE QUERY] Fetching ${contentType} content for: ${normalizedPath}`); + const headers: Record = {}; + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + const response = await fetch(url.toString(), { - headers: { - 'Authorization': `Bearer ${token}`, - }, + headers, }); if (!response.ok) { @@ -218,13 +221,13 @@ export function useFileContentQuery( queryKey: sandboxId && normalizedPath ? fileQueryKeys.content(sandboxId, normalizedPath, effectiveContentType) : [], queryFn: async () => { - if (!sandboxId || !normalizedPath || !session?.access_token) { + if (!sandboxId || !normalizedPath) { throw new Error('Missing required parameters'); } - return fetchFileContent(sandboxId, normalizedPath, effectiveContentType, session.access_token); + return fetchFileContent(sandboxId, normalizedPath, effectiveContentType, session?.access_token || ''); }, - enabled: Boolean(sandboxId && normalizedPath && session?.access_token && (options.enabled !== false)), + enabled: Boolean(sandboxId && normalizedPath && (options.enabled !== false)), staleTime: options.staleTime || (effectiveContentType === 'blob' ? 5 * 60 * 1000 : 2 * 60 * 1000), // 5min for blobs, 2min for text gcTime: options.gcTime || 10 * 60 * 1000, // 10 minutes retry: (failureCount, error: any) => { @@ -279,14 +282,14 @@ export function useDirectoryQuery( queryKey: sandboxId && normalizedPath ? fileQueryKeys.directory(sandboxId, normalizedPath) : [], queryFn: async (): Promise => { - if (!sandboxId || !normalizedPath || !session?.access_token) { + if (!sandboxId || !normalizedPath) { throw new Error('Missing required parameters'); } console.log(`[FILE QUERY] Fetching directory listing for: ${normalizedPath}`); return await listSandboxFiles(sandboxId, normalizedPath); }, - enabled: Boolean(sandboxId && normalizedPath && session?.access_token && (options.enabled !== false)), + enabled: Boolean(sandboxId && normalizedPath && (options.enabled !== false)), staleTime: options.staleTime || 30 * 1000, // 30 seconds for directory listings gcTime: 5 * 60 * 1000, // 5 minutes retry: 2, diff --git a/frontend/src/hooks/use-cached-file.ts b/frontend/src/hooks/use-cached-file.ts index 3d5faf08..c83b161c 100644 --- a/frontend/src/hooks/use-cached-file.ts +++ b/frontend/src/hooks/use-cached-file.ts @@ -129,10 +129,13 @@ export function useCachedFile( // Fetch with authentication const attemptFetch = async (isRetry: boolean = false): Promise => { + const headers: Record = {}; + if (session?.access_token) { + headers['Authorization'] = `Bearer ${session.access_token}`; + } + const response = await fetch(url.toString(), { - headers: { - 'Authorization': `Bearer ${session?.access_token}` - } + headers }); if (!response.ok) { diff --git a/frontend/src/hooks/use-file-content.ts b/frontend/src/hooks/use-file-content.ts index 81fc5d17..47c7c955 100644 --- a/frontend/src/hooks/use-file-content.ts +++ b/frontend/src/hooks/use-file-content.ts @@ -21,7 +21,7 @@ export function useFileContent(sandboxId?: string, filePath?: string) { const { session } = useAuth(); useEffect(() => { - if (!sandboxId || !filePath || !session?.access_token) { + if (!sandboxId || !filePath) { setContent(null); return; } @@ -38,7 +38,7 @@ export function useFileContent(sandboxId?: string, filePath?: string) { // Otherwise, load and cache the file content setIsLoading(true); getCachedFile(sandboxId, filePath, { - token: session.access_token, + token: session?.access_token || '', contentType: 'text' }) .then(fileContent => {