From 5d928ce2b79a6600b01f4ab91b254b2b3350e19f Mon Sep 17 00:00:00 2001 From: yiling Date: Thu, 11 Jun 2026 12:08:35 +0800 Subject: [PATCH] feat(ui): update download dialog flow to instantly resolve links and trigger concurrent downloads --- .../components/file-browser/file-browser.tsx | 61 ++++++-------- .../file-browser/use-file-actions.ts | 80 +++++++------------ 2 files changed, 51 insertions(+), 90 deletions(-) diff --git a/packages/webui/components/file-browser/file-browser.tsx b/packages/webui/components/file-browser/file-browser.tsx index fd5a77ef..a1f74655 100644 --- a/packages/webui/components/file-browser/file-browser.tsx +++ b/packages/webui/components/file-browser/file-browser.tsx @@ -38,7 +38,6 @@ import { DialogHeader, DialogTitle, } from '../ui/dialog' -import { Progress } from '../ui/progress' import { Button } from '../ui/button' import { ContextMenu, ContextMenuTrigger } from '../ui/context-menu' import { FileCard } from './file-card' @@ -338,9 +337,8 @@ export function FileBrowser({ confirmDelete, isDownloadDialogOpen, setIsDownloadDialogOpen, - itemsToDownload, - isDownloading, - downloadProgress, + isLoadingLinks, + resolvedFiles, startDownload, } = useFileActions({ teamId, @@ -1026,20 +1024,27 @@ export function FileBrowser({ - {isDownloading ? 'Downloading Files' : 'Prepare Download'} + {isLoadingLinks ? 'Preparing Download' : 'Confirm Download'} - {isDownloading - ? 'Please wait while we queue your downloads. Do not close this dialog.' + {isLoadingLinks + ? 'Resolving all files and folders. Please wait...' : 'Selected files and folders will be prepared for download.'} - {!isDownloading ? ( + {isLoadingLinks ? ( +
+ + + Preparing download links... + +
+ ) : (
- You have selected{' '} - {itemsToDownload.length}{' '} - item(s). + This download will include{' '} + {resolvedFiles.length}{' '} + file(s).
@@ -1052,44 +1057,26 @@ export function FileBrowser({
- ) : ( -
-
- - - {downloadProgress.total === 0 - ? 'Preparing download links...' - : `Downloading file ${downloadProgress.current} of ${downloadProgress.total}...`} - -
- {downloadProgress.total > 0 && ( -
- -

- Current file: {downloadProgress.currentName} -

-
- )} -
)} - {!isDownloading ? ( + {!isLoadingLinks ? ( <> - ) : ( - )} diff --git a/packages/webui/components/file-browser/use-file-actions.ts b/packages/webui/components/file-browser/use-file-actions.ts index 3b45c4c3..8e02dfbb 100644 --- a/packages/webui/components/file-browser/use-file-actions.ts +++ b/packages/webui/components/file-browser/use-file-actions.ts @@ -28,13 +28,10 @@ export function useFileActions({ const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) const [itemsToDelete, setItemsToDelete] = useState([]) const [isDownloadDialogOpen, setIsDownloadDialogOpen] = useState(false) - const [itemsToDownload, setItemsToDownload] = useState([]) - const [isDownloading, setIsDownloading] = useState(false) - const [downloadProgress, setDownloadProgress] = useState<{ - current: number - total: number - currentName: string - }>({ current: 0, total: 0, currentName: '' }) + const [isLoadingLinks, setIsLoadingLinks] = useState(false) + const [resolvedFiles, setResolvedFiles] = useState< + Array<{ id: string; name: string; url: string }> + >([]) const queryClient = useQueryClient() const $createFolder = client.api.folders.$post @@ -207,60 +204,38 @@ export function useFileActions({ } } - const handleDownload = (items: AssetInfo[]) => { + const handleDownload = async (items: AssetInfo[]) => { if (items.length === 0) return - setItemsToDownload(items) - setDownloadProgress({ current: 0, total: 0, currentName: '' }) setIsDownloadDialogOpen(true) - } - - const startDownload = async () => { - setIsDownloading(true) + setIsLoadingLinks(true) + setResolvedFiles([]) try { const res = await getDownloadLinks({ - json: { ids: itemsToDownload.map((i) => i.id!) }, + json: { ids: items.map((i) => i.id!) }, }) - const files = res.files - - const chunkSize = 5 - const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) - - for (let i = 0; i < files.length; i += chunkSize) { - const chunk = files.slice(i, i + chunkSize) - - // Trigger downloads for this batch in parallel by creating/clicking links - for (const file of chunk) { - const absoluteIndex = i + chunk.indexOf(file) + 1 - setDownloadProgress({ - current: absoluteIndex, - total: files.length, - currentName: file.name, - }) - - const a = document.createElement('a') - a.href = file.url - a.download = file.name - a.target = '_blank' - document.body.appendChild(a) - a.click() - document.body.removeChild(a) - } - - // If we have more chunks to process, wait 1 second before triggering the next batch - if (i + chunkSize < files.length) { - await delay(1000) - } - } - toast.success('Downloads started successfully') + setResolvedFiles(res.files) } catch (error) { - toast.error('Download failed') + toast.error('Failed to prepare download links') + setIsDownloadDialogOpen(false) console.error(error) } finally { - setIsDownloading(false) - setIsDownloadDialogOpen(false) + setIsLoadingLinks(false) } } + const startDownload = () => { + for (const file of resolvedFiles) { + const a = document.createElement('a') + a.href = file.url + a.download = file.name + a.target = '_blank' + document.body.appendChild(a) + a.click() + document.body.removeChild(a) + } + setIsDownloadDialogOpen(false) + } + const handleNewFolder = (name: string) => { createFolder( { json: { name, parentId: assetId } }, @@ -353,9 +328,8 @@ export function useFileActions({ confirmDelete, isDownloadDialogOpen, setIsDownloadDialogOpen, - itemsToDownload, - isDownloading, - downloadProgress, + isLoadingLinks, + resolvedFiles, startDownload, } }