diff --git a/apps/mobile/__tests__/use-uploads.test.tsx b/apps/mobile/__tests__/use-uploads.test.tsx index 50e80efd4..b49840c02 100644 --- a/apps/mobile/__tests__/use-uploads.test.tsx +++ b/apps/mobile/__tests__/use-uploads.test.tsx @@ -425,3 +425,21 @@ test('a file picked before the destination exists is not inherited later', async expect(send).not.toHaveBeenCalled(); await waitFor(() => expect(result.current.items[0].status).toBe('failed')); }); + +test('a picker returning after an account switch releases the file without queuing it', async () => { + const { result, rerender } = setup(); + let finishPick!: (files: ReturnType[]) => void; + const selection = new Promise[]>(resolve => { + finishPick = resolve; + }); + const upload = result.current.upload(selection); + rerender({ destination: destination({ accountId: 'account-2' }) }); + await act(async () => { + finishPick([picked('a.pdf')]); + await upload; + }); + + expect(send).not.toHaveBeenCalled(); + expect(discard).toHaveBeenCalledWith('file:///cache/a.pdf'); + expect(result.current.items).toHaveLength(0); +}); diff --git a/apps/mobile/app/knowledge/detail.tsx b/apps/mobile/app/knowledge/detail.tsx index e35d0c2ee..8944a4ecb 100644 --- a/apps/mobile/app/knowledge/detail.tsx +++ b/apps/mobile/app/knowledge/detail.tsx @@ -72,10 +72,7 @@ export default function KnowledgeDetailRoute() { const pick = useCallback(async () => { setActionError(null); try { - const files = await pickDocuments(); - // If the screen left while the OS picker was open, `upload` sees an - // unmounted hook and releases the copies instead of queueing them. - if (files.length) uploads.upload(files); + await uploads.upload(pickDocuments()); } catch { setActionError('Could not open the file picker on this device.'); } diff --git a/apps/mobile/src/features/knowledge/useUploads.ts b/apps/mobile/src/features/knowledge/useUploads.ts index b4032d195..ba9d948ac 100644 --- a/apps/mobile/src/features/knowledge/useUploads.ts +++ b/apps/mobile/src/features/knowledge/useUploads.ts @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useRef, useState } from 'react'; +import { useCallback, useLayoutEffect, useRef, useState } from 'react'; import { ApiError } from '../../services/apiClient'; import type { PickedFile, UploadPolicy } from '../../services/knowledgeApi'; @@ -276,13 +276,13 @@ export function useUploads({ destination, policy, onUploaded }: UploadOptions) { }, [patch]); // Keep the live values for callbacks that outlive a render. - useEffect(() => { + useLayoutEffect(() => { destinationRef.current = destination; policyRef.current = policy; onUploadedRef.current = onUploaded; }, [destination, policy, onUploaded]); - useEffect(() => { + useLayoutEffect(() => { mounted.current = true; return () => { mounted.current = false; @@ -308,7 +308,7 @@ export function useUploads({ destination, policy, onUploaded }: UploadOptions) { // Signing out, switching account, server or base retires the queue before // another request can start. The generation check inside `drain` covers the // task that is already in flight. - useEffect(() => { + useLayoutEffect(() => { generation.current += 1; const target = origin && ref ? { origin, ref, accountId } : null; @@ -334,11 +334,12 @@ export function useUploads({ destination, policy, onUploaded }: UploadOptions) { }, [origin, ref, accountId, drain, retire]); const upload = useCallback( - (files: readonly PickedFile[]) => { + async (selection: Promise | readonly PickedFile[]) => { + const pickedAt = generation.current; + const files = await selection; if (!files.length) return; - if (!mounted.current) { - // The picker resolved after the screen closed. Nothing can upload - // these, so the copies go with it. + if (!mounted.current || generation.current !== pickedAt) { + // The native picker can outlive both the route and the selected account. files.forEach(file => discardPickedCopy(file.uri).catch(() => {})); return; }