feat(file): add bulk download links API and frontend batch download dialog

This commit is contained in:
yiling
2026-06-11 11:44:26 +08:00
parent 81ccc803db
commit ec42c4e9ff
8 changed files with 639 additions and 19 deletions
@@ -13,7 +13,7 @@ import { useFieldStore } from '@/ui/stores/fields'
import { useUploadStore } from '@/ui/stores/upload'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useNavigate } from '@tanstack/react-router'
import { Download } from 'lucide-react'
import { Download, AlertTriangle, Loader2 } from 'lucide-react'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { toast } from 'sonner'
import type { DragState } from '../dnd-types'
@@ -30,6 +30,15 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '../ui/alert-dialog'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
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'
@@ -327,6 +336,12 @@ export function FileBrowser({
isDeleteDialogOpen,
setIsDeleteDialogOpen,
confirmDelete,
isDownloadDialogOpen,
setIsDownloadDialogOpen,
itemsToDownload,
isDownloading,
downloadProgress,
startDownload,
} = useFileActions({
teamId,
projectId,
@@ -1007,6 +1022,79 @@ export function FileBrowser({
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<Dialog open={isDownloadDialogOpen} onOpenChange={setIsDownloadDialogOpen}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{isDownloading ? 'Downloading Files' : 'Prepare Download'}</DialogTitle>
<DialogDescription>
{isDownloading
? 'Please wait while we queue your downloads. Do not close this dialog.'
: 'Selected files and folders will be prepared for download.'}
</DialogDescription>
</DialogHeader>
{!isDownloading ? (
<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).
</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" />
<div>
<p className="font-semibold">No Folder Structure</p>
<p className="mt-0.5 text-xs text-amber-700/90 dark:text-amber-400/90">
Folder hierarchy will be flattened. All nested files will be downloaded directly
into your default download folder.
</p>
</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 ? (
<>
<Button variant="outline" onClick={() => setIsDownloadDialogOpen(false)}>
Cancel
</Button>
<Button onClick={startDownload} className="gap-2">
<Download className="h-4 w-4" />
Start Download
</Button>
</>
) : (
<Button disabled variant="outline">
Downloading...
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
</>
)
}
@@ -5,6 +5,7 @@ import type { AssetInfo } from '@shumai/dtos'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import type { InferRequestType, InferResponseType } from 'hono/client'
import { useState } from 'react'
import { toast } from 'sonner'
interface UseFileActionsProps {
teamId: string
@@ -26,6 +27,14 @@ export function useFileActions({
const [editingItemId, setEditingItemId] = useState<string | null>(null)
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 queryClient = useQueryClient()
const $createFolder = client.api.folders.$post
@@ -111,6 +120,19 @@ export function useFileActions({
},
})
const $getDownloadLinks = client.api.files['download-links'].$post
const { mutateAsync: getDownloadLinks } = useMutation<
InferResponseType<typeof $getDownloadLinks, 200>,
Error,
InferRequestType<typeof $getDownloadLinks>
>({
mutationFn: async (request) => {
const res = await $getDownloadLinks(request)
if (!res.ok) throw new Error('Failed to get download links')
return (await res.json()) as unknown as InferResponseType<typeof $getDownloadLinks, 200>
},
})
const handleRename = (item: AssetInfo) => {
setEditingItemId(item.id!)
}
@@ -186,11 +208,57 @@ export function useFileActions({
}
const handleDownload = (items: AssetInfo[]) => {
alert(
`Download functionality - Would download ${
items.length
} item(s): ${items.map((i) => i.name).join(', ')}`,
)
if (items.length === 0) return
setItemsToDownload(items)
setDownloadProgress({ current: 0, total: 0, currentName: '' })
setIsDownloadDialogOpen(true)
}
const startDownload = async () => {
setIsDownloading(true)
try {
const res = await getDownloadLinks({
json: { ids: itemsToDownload.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')
} catch (error) {
toast.error('Download failed')
console.error(error)
} finally {
setIsDownloading(false)
setIsDownloadDialogOpen(false)
}
}
const handleNewFolder = (name: string) => {
@@ -283,5 +351,11 @@ export function useFileActions({
setIsDeleteDialogOpen,
itemsToDelete,
confirmDelete,
isDownloadDialogOpen,
setIsDownloadDialogOpen,
itemsToDownload,
isDownloading,
downloadProgress,
startDownload,
}
}
+13 -13
View File
@@ -1,22 +1,22 @@
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/ui/components/ui/alert-dialog'
import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar'
import { Button } from '@/ui/components/ui/button'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
} from '@/ui/components/ui/dropdown-menu'
import { Input } from '@/ui/components/ui/input'
import { ChevronDown, Copy, Loader2, Trash2 } from 'lucide-react'