mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 04:57:12 +00:00
feat(ui): update download dialog flow to instantly resolve links and trigger concurrent downloads
This commit is contained in:
@@ -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({
|
||||
<Dialog open={isDownloadDialogOpen} onOpenChange={setIsDownloadDialogOpen}>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{isDownloading ? 'Downloading Files' : 'Prepare Download'}</DialogTitle>
|
||||
<DialogTitle>{isLoadingLinks ? 'Preparing Download' : 'Confirm Download'}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{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.'}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{!isDownloading ? (
|
||||
{isLoadingLinks ? (
|
||||
<div className="space-y-4 py-6 flex flex-col items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
||||
<span className="text-sm font-medium text-muted-foreground mt-2">
|
||||
Preparing download links...
|
||||
</span>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4 py-2">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
You have selected{' '}
|
||||
<span className="font-medium text-foreground">{itemsToDownload.length}</span>{' '}
|
||||
item(s).
|
||||
This download will include{' '}
|
||||
<span className="font-semibold text-foreground">{resolvedFiles.length}</span>{' '}
|
||||
file(s).
|
||||
</div>
|
||||
<div className="flex items-start gap-3 p-3 text-sm rounded-lg bg-amber-50 dark:bg-amber-950/30 text-amber-800 dark:text-amber-300 border border-amber-200 dark:border-amber-900/30">
|
||||
<AlertTriangle className="h-5 w-5 shrink-0 mt-0.5" />
|
||||
@@ -1052,44 +1057,26 @@ export function FileBrowser({
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4 py-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<Loader2 className="h-5 w-5 animate-spin text-primary" />
|
||||
<span className="text-sm font-medium text-foreground">
|
||||
{downloadProgress.total === 0
|
||||
? 'Preparing download links...'
|
||||
: `Downloading file ${downloadProgress.current} of ${downloadProgress.total}...`}
|
||||
</span>
|
||||
</div>
|
||||
{downloadProgress.total > 0 && (
|
||||
<div className="space-y-2">
|
||||
<Progress
|
||||
value={Math.round((downloadProgress.current / downloadProgress.total) * 100)}
|
||||
className="h-2"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground truncate">
|
||||
Current file: {downloadProgress.currentName}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<DialogFooter>
|
||||
{!isDownloading ? (
|
||||
{!isLoadingLinks ? (
|
||||
<>
|
||||
<Button variant="outline" onClick={() => setIsDownloadDialogOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={startDownload} className="gap-2">
|
||||
<Button
|
||||
onClick={startDownload}
|
||||
className="gap-2"
|
||||
disabled={resolvedFiles.length === 0}
|
||||
>
|
||||
<Download className="h-4 w-4" />
|
||||
Start Download
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button disabled variant="outline">
|
||||
Downloading...
|
||||
<Button variant="outline" onClick={() => setIsDownloadDialogOpen(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
)}
|
||||
</DialogFooter>
|
||||
|
||||
@@ -28,13 +28,10 @@ export function useFileActions({
|
||||
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false)
|
||||
const [itemsToDelete, setItemsToDelete] = useState<AssetInfo[]>([])
|
||||
const [isDownloadDialogOpen, setIsDownloadDialogOpen] = useState(false)
|
||||
const [itemsToDownload, setItemsToDownload] = useState<AssetInfo[]>([])
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user