mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 19:00:02 +08:00
fix(mobile): bind pending file picks to the active upload destination
This commit is contained in:
@@ -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<typeof picked>[]) => void;
|
||||
const selection = new Promise<ReturnType<typeof picked>[]>(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);
|
||||
});
|
||||
|
||||
@@ -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.');
|
||||
}
|
||||
|
||||
@@ -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[]> | 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user