feat(webui): add simplified mobile-friendly layout for file and share lists (#356)

* feat(webui): add simplified mobile-friendly layout for file and share lists

* refactor(webui): switch mobile file list layout to 1-column grid

* feat(webui): show foldertree by default in child sidebar and add close button

* feat(webui): add collapsible section headers and fix folder card full-width on mobile

* feat(webui): improve mobile breadcrumb bar with middle truncation and dual ellipsis

* fix(webui): unify breadcrumb DOM structure across desktop and mobile

* fix(webui): remove unused breadcrumbs variable in BreadcrumbNav

* fix(webui): complete and confirm file uploads on mobile and handle errors properly

* fix(webui): remove active scale animation on mobile FAB to stabilize dropdown menu anchor

* refactor(webui): use PresignedUrl type from dtos for uploadFiles and eliminate any cast

* fix(webui): scope folder card max width to desktop with responsive md:max-w-[340px]
This commit is contained in:
Yiling-J
2026-08-27 20:44:08 +08:00
committed by GitHub
parent d9a8b6a191
commit d16e31c0e5
14 changed files with 2053 additions and 324 deletions
@@ -12,6 +12,7 @@ import type {
AssetInfo,
CollectionInfo,
CreateUploadTaskRequest,
PresignedUrl,
SearchCondition,
SearchSort,
ShareLinkInfo,
@@ -558,9 +559,7 @@ export function FileBrowser({
queryKey: ['teams', teamId, 'upload', 'tasks'],
})
// The RPC client returns an object that we cast to the expected type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await uploadFiles(currentFiles, data.presignedUrls as any, data.taskId!)
await uploadFiles(currentFiles, data.presignedUrls, data.taskId!)
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
@@ -731,14 +730,11 @@ export function FileBrowser({
const uploadFiles = async (
files: FileWithId[],
presignedUrls: { id?: string; url?: string; fileId?: string }[],
presignedUrls: PresignedUrl[] | undefined,
taskId: string,
) => {
const uploadUrlMap = presignedUrls?.reduce(
(
acc: Record<string, { url: string; fileId: string }>,
item: { id?: string; url?: string; fileId?: string },
) => {
(acc: Record<string, { url: string; fileId: string }>, item: PresignedUrl) => {
if (item.id) {
acc[item.id] = {
url: item.url || '',
@@ -277,7 +277,7 @@ export function FolderCard({
: undefined
}
className={cn(
'group relative w-full max-w-[340px] select-none cursor-pointer isolate flex flex-col items-center h-full outline-none focus:outline-none focus-visible:outline-none',
'group relative w-full md:max-w-[340px] select-none cursor-pointer isolate flex flex-col items-center h-full outline-none focus:outline-none focus-visible:outline-none',
)}
>
<div className="absolute inset-0 z-10 pointer-events-none">
@@ -0,0 +1,424 @@
// @vitest-environment happy-dom
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { MobileFileBrowser } from './mobile-file-browser'
import type { AssetInfo } from '@shumai/dtos'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
vi.mock('@dnd-kit/react', () => ({
useDraggable: () => ({
ref: vi.fn(),
isDragging: false,
}),
useDroppable: () => ({
ref: vi.fn(),
isOver: false,
}),
}))
vi.mock('@/ui/hooks/use-permissions', () => ({
usePermissions: () => ({
canEdit: true,
canAdmin: true,
canView: true,
}),
}))
vi.mock('@/ui/api/client', () => ({
client: {
api: {
files: {
$delete: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
'download-links': {
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({ files: [] }) }),
},
restore: {
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
':fileId': {
$get: vi.fn().mockResolvedValue({
ok: true,
json: async () => ({}),
}),
$put: vi.fn().mockResolvedValue({
ok: true,
json: async () => ({}),
}),
},
},
folders: {
$post: vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ id: 'new-folder-id' }),
}),
$delete: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
restore: {
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
':folderId': {
$put: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
},
teams: {
':teamId': {
upload: {
tasks: {
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
':taskId': {
$patch: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
},
},
},
},
projects: {
':projectId': {
'empty-trash': {
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
},
},
},
},
}))
describe('MobileFileBrowser', () => {
let queryClient: QueryClient
beforeEach(() => {
queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
})
afterEach(() => {
cleanup()
})
const mockFolder: AssetInfo = {
id: 'folder-1',
name: 'Test Folder',
type: 'folder',
fileCount: 3,
sizeByte: 0,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
status: 'processed',
} as AssetInfo
const mockFile: AssetInfo = {
id: 'file-1',
name: 'test_document.pdf',
type: 'file',
sizeByte: 1024 * 100,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
status: 'processed',
} as AssetInfo
const defaultProps = {
teamId: 'team-1',
projectId: 'project-1',
assetId: 'root-folder',
folders: [mockFolder],
files: [mockFile],
selectedItem: null,
selectedIds: new Set<string>(),
onItemSelect: vi.fn(),
onItemDoubleClick: vi.fn(),
onClearSelection: vi.fn(),
fetchNextFoldersPage: vi.fn(),
hasNextFoldersPage: false,
isFetchingNextFoldersPage: false,
fetchNextFilesPage: vi.fn(),
hasNextFilesPage: false,
isFetchingNextFilesPage: false,
}
const renderComponent = (props: Partial<React.ComponentProps<typeof MobileFileBrowser>> = {}) => {
return render(
<QueryClientProvider client={queryClient}>
<MobileFileBrowser {...defaultProps} {...props} />
</QueryClientProvider>,
)
}
it('renders folders and files in mobile grid', () => {
renderComponent()
expect(screen.getByText('Test Folder')).toBeDefined()
expect(screen.getByText('test_document.pdf')).toBeDefined()
})
it('triggers onItemSelect when an item is clicked', () => {
const onItemSelect = vi.fn()
renderComponent({ onItemSelect })
const folderElement = screen.getByText('Test Folder')
fireEvent.click(folderElement)
expect(onItemSelect).toHaveBeenCalled()
})
it('triggers onItemDoubleClick when an item is double-clicked', () => {
const onItemDoubleClick = vi.fn()
renderComponent({ onItemDoubleClick })
const folderElement = screen.getByText('Test Folder')
fireEvent.doubleClick(folderElement)
expect(onItemDoubleClick).toHaveBeenCalledWith(mockFolder)
})
it('renders empty state when there are no folders and files', () => {
renderComponent({ folders: [], files: [] })
expect(screen.getByText(/This folder is empty|此文件夹为空/i)).toBeDefined()
})
it('renders collapsible section headers for folders and files with count and size', () => {
renderComponent({
totalFoldersSize: 50000,
totalFilesSize: 100000,
})
expect(screen.getByText(/1 Folder • 50 KB/i)).toBeDefined()
expect(screen.getByText(/1 Asset • 100 KB/i)).toBeDefined()
})
it('collapses and expands folders and files when clicking section headers', () => {
renderComponent()
expect(screen.getByText('Test Folder')).toBeDefined()
expect(screen.getByText('test_document.pdf')).toBeDefined()
// Collapse folders
const folderHeaderBtn = screen.getByRole('button', { name: /Folder/i })
fireEvent.click(folderHeaderBtn)
expect(screen.queryByText('Test Folder')).toBeNull()
// Expand folders back
fireEvent.click(folderHeaderBtn)
expect(screen.getByText('Test Folder')).toBeDefined()
// Collapse files
const fileHeaderBtn = screen.getByRole('button', { name: /Asset/i })
fireEvent.click(fileHeaderBtn)
expect(screen.queryByText('test_document.pdf')).toBeNull()
// Expand files back
fireEvent.click(fileHeaderBtn)
expect(screen.getByText('test_document.pdf')).toBeDefined()
})
it('renders Floating Action Button when canEdit is true', () => {
renderComponent()
expect(screen.getByRole('button', { name: /actions/i })).toBeDefined()
})
it('completes upload and confirms upload on 2xx response', async () => {
const originalXhr = window.XMLHttpRequest
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
const { client } = await import('@/ui/api/client')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(client.api.teams[':teamId'].upload.tasks as any)['$post'] = vi
.fn()
.mockImplementation(async (req) => {
const json = req.json
return {
ok: true,
json: async () => ({
taskId: 'task-1',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
presignedUrls: json.files.map((f: any) => ({
id: f.id,
url: 'https://upload.example.com',
fileId: 'server-file-1',
})),
}),
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(client.api.teams[':teamId'].upload.tasks[':taskId'] as any)['$patch'] = patchMock
class MockXhr {
open = vi.fn()
setRequestHeader = vi.fn()
send = vi.fn(() => {
this.status = 200
this.onload?.()
})
status = 200
upload = { onprogress: null as unknown }
onload: (() => void) | null = null
onerror: (() => void) | null = null
onabort: (() => void) | null = null
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.XMLHttpRequest = MockXhr as any
const { container } = renderComponent()
const fileInput = container.querySelector(
'input[type="file"]:not([webkitdirectory])',
) as HTMLInputElement
expect(fileInput).toBeDefined()
const testFile = new File(['content'], 'hello.txt', { type: 'text/plain' })
fireEvent.change(fileInput, { target: { files: [testFile] } })
// Wait for mutation to finish
await vi.waitFor(() => {
expect(patchMock).toHaveBeenCalledWith(
expect.objectContaining({
param: { teamId: 'team-1', taskId: 'task-1' },
json: { fileId: 'server-file-1' },
}),
)
})
window.XMLHttpRequest = originalXhr
})
it('fails upload and confirms with error on non-2xx status', async () => {
const originalXhr = window.XMLHttpRequest
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
const { client } = await import('@/ui/api/client')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(client.api.teams[':teamId'].upload.tasks as any)['$post'] = vi
.fn()
.mockImplementation(async (req) => {
const json = req.json
return {
ok: true,
json: async () => ({
taskId: 'task-2',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
presignedUrls: json.files.map((f: any) => ({
id: f.id,
url: 'https://upload.example.com',
fileId: 'server-file-2',
})),
}),
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(client.api.teams[':teamId'].upload.tasks[':taskId'] as any)['$patch'] = patchMock
class MockXhr500 {
open = vi.fn()
setRequestHeader = vi.fn()
send = vi.fn(() => {
this.status = 500
this.onload?.()
})
status = 500
upload = { onprogress: null as unknown }
onload: (() => void) | null = null
onerror: (() => void) | null = null
onabort: (() => void) | null = null
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.XMLHttpRequest = MockXhr500 as any
const { container } = renderComponent()
const fileInput = container.querySelector(
'input[type="file"]:not([webkitdirectory])',
) as HTMLInputElement
const testFile = new File(['content'], 'error.txt', { type: 'text/plain' })
fireEvent.change(fileInput, { target: { files: [testFile] } })
await vi.waitFor(() => {
expect(patchMock).toHaveBeenCalledWith(
expect.objectContaining({
param: { teamId: 'team-1', taskId: 'task-2' },
json: {
fileId: 'server-file-2',
errorMessage: 'upload failed with status: 500',
},
}),
)
})
window.XMLHttpRequest = originalXhr
})
it('fails upload and confirms with error on network failure', async () => {
const originalXhr = window.XMLHttpRequest
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
const { client } = await import('@/ui/api/client')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(client.api.teams[':teamId'].upload.tasks as any)['$post'] = vi
.fn()
.mockImplementation(async (req) => {
const json = req.json
return {
ok: true,
json: async () => ({
taskId: 'task-3',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
presignedUrls: json.files.map((f: any) => ({
id: f.id,
url: 'https://upload.example.com',
fileId: 'server-file-3',
})),
}),
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(client.api.teams[':teamId'].upload.tasks[':taskId'] as any)['$patch'] = patchMock
class MockXhrError {
open = vi.fn()
setRequestHeader = vi.fn()
send = vi.fn(() => {
this.onerror?.()
})
status = 0
upload = { onprogress: null as unknown }
onload: (() => void) | null = null
onerror: (() => void) | null = null
onabort: (() => void) | null = null
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.XMLHttpRequest = MockXhrError as any
const { container } = renderComponent()
const fileInput = container.querySelector(
'input[type="file"]:not([webkitdirectory])',
) as HTMLInputElement
const testFile = new File(['content'], 'network-error.txt', { type: 'text/plain' })
fireEvent.change(fileInput, { target: { files: [testFile] } })
await vi.waitFor(() => {
expect(patchMock).toHaveBeenCalledWith(
expect.objectContaining({
param: { teamId: 'team-1', taskId: 'task-3' },
json: {
fileId: 'server-file-3',
errorMessage: 'upload failed with error: Network error',
},
}),
)
})
window.XMLHttpRequest = originalXhr
})
it('hides Floating Action Button in public share mode', () => {
renderComponent({ isPublic: true })
expect(screen.queryByRole('button', { name: /actions/i })).toBeNull()
})
})
@@ -0,0 +1,831 @@
'use client'
import { client } from '@/ui/api/client'
import { usePermissions } from '@/ui/hooks/use-permissions'
import { m } from '@/ui/paraglide/messages.js'
import { useUploadStore } from '@/ui/stores/upload'
import type { AssetInfo, CreateUploadTaskRequest, PresignedUrl } from '@shumai/dtos'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import type { InferRequestType, InferResponseType } from 'hono/client'
import { formatSize } from '@/ui/lib/format'
import {
AlertTriangle,
ChevronDown,
ChevronRight,
Download,
FolderClock,
FolderPlus,
Loader2,
Plus,
Upload,
} from 'lucide-react'
import React, { useRef, useState } from 'react'
import { useInView } from 'react-intersection-observer'
import { toast } from 'sonner'
import { ulid } from 'ulid'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '../ui/alert-dialog'
import { Button } from '../ui/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '../ui/dialog'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '../ui/dropdown-menu'
import { Input } from '../ui/input'
import { FileCard } from './file-card'
import { FolderCard } from './folder-card'
import { useFileActions } from './use-file-actions'
export interface MobileFileBrowserProps {
teamId: string
projectId: string
assetId: string
folders: AssetInfo[]
files: AssetInfo[]
totalFolders?: number
totalFiles?: number
totalFoldersSize?: number
totalFilesSize?: number
selectedItem: AssetInfo | null
selectedIds: Set<string>
onItemSelect: (item: AssetInfo, event: React.MouseEvent) => void
onItemDoubleClick: (item: AssetInfo) => void
onSaveField?: (fileId: string, fieldId: string, value: unknown) => void
onClearSelection: () => void
fetchNextFoldersPage: () => void
hasNextFoldersPage: boolean
isFetchingNextFoldersPage: boolean
fetchNextFilesPage: () => void
hasNextFilesPage: boolean
isFetchingNextFilesPage: boolean
isRecentlyDeleted?: boolean
isRecents?: boolean
isShareView?: boolean
isPublic?: boolean
shareId?: string
allowDownload?: boolean
rootFolderId?: string
}
type FileWithId = {
file: File
id: string
}
let pendingFilesToUpload: FileWithId[] = []
export function MobileFileBrowser({
teamId,
projectId,
assetId,
folders,
files,
totalFolders,
totalFiles,
totalFoldersSize,
totalFilesSize,
selectedItem,
selectedIds,
onItemSelect,
onItemDoubleClick,
onSaveField,
onClearSelection,
fetchNextFoldersPage,
hasNextFoldersPage,
isFetchingNextFoldersPage,
fetchNextFilesPage,
hasNextFilesPage,
isFetchingNextFilesPage,
isRecentlyDeleted = false,
isRecents = false,
isShareView = false,
isPublic = false,
shareId,
allowDownload = true,
}: MobileFileBrowserProps) {
const queryClient = useQueryClient()
const { canEdit, canAdmin } = usePermissions(projectId)
const fileInputRef = useRef<HTMLInputElement>(null)
const folderInputRef = useRef<HTMLInputElement>(null)
const [foldersExpanded, setFoldersExpanded] = useState(true)
const [filesExpanded, setFilesExpanded] = useState(true)
const [isNewFolderDialogOpen, setIsNewFolderDialogOpen] = useState(false)
const [newFolderName, setNewFolderName] = useState('')
const [isEmptyTrashDialogOpen, setIsEmptyTrashDialogOpen] = useState(false)
const foldersCount = totalFolders ?? folders.length
const filesCount = totalFiles ?? files.length
const foldersSizeVal =
totalFoldersSize ??
(folders.length > 0 ? folders.reduce((acc, f) => acc + (f.sizeByte || 0), 0) : -1)
const filesSizeVal =
totalFilesSize ?? (files.length > 0 ? files.reduce((acc, f) => acc + (f.sizeByte || 0), 0) : -1)
const showFoldersSize = foldersSizeVal > -1
const showFilesSize = filesSizeVal > -1
const formatCount = (count: number, isFile: boolean) => {
if (isFile) {
return count === 1 ? m.n_assets_singular({ count }) : m.n_assets_plural({ count })
} else {
return count === 1 ? m.n_folders_singular({ count }) : m.n_folders_plural({ count })
}
}
const {
editingItemId,
setEditingItemId,
handleAction,
onRenameSubmit,
isDeleteDialogOpen,
setIsDeleteDialogOpen,
confirmDelete,
isDownloadDialogOpen,
setIsDownloadDialogOpen,
isLoadingLinks,
resolvedFiles,
startDownload,
} = useFileActions({
teamId,
projectId,
assetId,
folders,
files,
selectedIds,
isPublic,
shareId,
})
// Infinite scroll trigger
const { ref: loadMoreRef, inView } = useInView()
React.useEffect(() => {
if (inView) {
if (hasNextFoldersPage && !isFetchingNextFoldersPage) {
fetchNextFoldersPage()
} else if (hasNextFilesPage && !isFetchingNextFilesPage) {
fetchNextFilesPage()
}
}
}, [
inView,
hasNextFoldersPage,
isFetchingNextFoldersPage,
hasNextFilesPage,
isFetchingNextFilesPage,
fetchNextFoldersPage,
fetchNextFilesPage,
])
// Upload actions
const incrementUploading = useUploadStore((state) => state.increment)
const decrementUploading = useUploadStore((state) => state.decrement)
const startTask = useUploadStore((state) => state.startTask)
const updateFileProgress = useUploadStore((state) => state.updateFileProgress)
const completeFile = useUploadStore((state) => state.completeFile)
const failFile = useUploadStore((state) => state.failFile)
const $confirmUpload = client.api.teams[':teamId'].upload.tasks[':taskId'].$patch
const { mutateAsync: confirmUpload } = useMutation<
InferResponseType<typeof $confirmUpload>,
Error,
InferRequestType<typeof $confirmUpload>
>({
mutationFn: async (request) => {
const res = await $confirmUpload(request)
if (!res.ok) throw new Error('Failed to confirm upload')
return (await res.json()) as InferResponseType<typeof $confirmUpload>
},
})
const uploadFiles = async (
filesList: FileWithId[],
presignedUrls: PresignedUrl[] | undefined,
taskId: string,
) => {
const uploadUrlMap = presignedUrls?.reduce(
(acc: Record<string, { url: string; fileId: string }>, item: PresignedUrl) => {
if (item.id) {
acc[item.id] = {
url: item.url || '',
fileId: item.fileId || '',
}
}
return acc
},
{},
)
if (!uploadUrlMap) return
const concurrencyLimit = 5
const queue = [...filesList]
const uploadNext = async () => {
while (queue.length > 0) {
const item = queue.shift()
if (item) {
const uploadInfo = uploadUrlMap[item.id]
if (uploadInfo && uploadInfo.url) {
incrementUploading()
try {
const xhr = new XMLHttpRequest()
const uploadPromise = new Promise<{ ok: boolean; status: number }>(
(resolve, reject) => {
xhr.open('PUT', uploadInfo.url)
xhr.setRequestHeader('Content-Type', item.file.type)
xhr.upload.onprogress = (event) => {
if (event.lengthComputable) {
updateFileProgress(taskId, uploadInfo.fileId, event.loaded)
}
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve({ ok: true, status: xhr.status })
} else {
resolve({ ok: false, status: xhr.status })
}
}
xhr.onerror = () => reject(new Error('Network error'))
xhr.onabort = () => reject(new Error('Upload aborted'))
xhr.send(item.file)
},
)
const resp = await uploadPromise
if (resp.ok) {
completeFile(taskId, uploadInfo.fileId)
await confirmUpload({
param: { teamId: teamId!, taskId: taskId },
json: {
fileId: uploadInfo.fileId,
},
})
} else {
failFile(taskId, uploadInfo.fileId)
toast.error(`Failed to upload file: ${item.file.name}`)
await confirmUpload({
param: { teamId: teamId!, taskId: taskId },
json: {
fileId: uploadInfo.fileId,
errorMessage: `upload failed with status: ${resp.status}`,
},
})
return
}
} catch (error) {
failFile(taskId, uploadInfo.fileId)
toast.error(`Failed to upload file: ${item.file.name}`)
await confirmUpload({
param: { teamId: teamId!, taskId: taskId },
json: {
fileId: uploadInfo.fileId,
errorMessage: `upload failed with error: ${error instanceof Error ? error.message : String(error)}`,
},
})
return
} finally {
decrementUploading()
queryClient.invalidateQueries({
queryKey: ['search', teamId, assetId],
})
}
}
}
}
}
const activeUploaders = Array(concurrencyLimit)
.fill(null)
.map(() => uploadNext())
await Promise.all(activeUploaders)
}
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
const { mutate: createUploadTaskMutation } = useMutation<
InferResponseType<typeof $createUploadTask>,
Error,
InferRequestType<typeof $createUploadTask>,
{ files: FileWithId[] }
>({
mutationFn: async (request) => {
const res = await $createUploadTask(request)
if (!res.ok) throw new Error('Failed to create upload task')
return (await res.json()) as InferResponseType<typeof $createUploadTask>
},
onMutate: () => {
return { files: [...pendingFilesToUpload] }
},
onSuccess: async (data, _variables, context) => {
const currentFiles = context?.files || []
const filesProgressInfo = currentFiles
.map((f) => {
const urlInfo = data.presignedUrls?.find((p) => p.id === f.id)
return {
fileId: urlInfo?.fileId || f.id,
name: f.file.name,
size: f.file.size,
}
})
.filter((info) => !!info.fileId)
const visibleFiles = currentFiles.filter((f) => !f.file.name.startsWith('.'))
const taskName =
visibleFiles.length === 1 ? visibleFiles[0].file.name : `${visibleFiles.length} Items`
if (data.taskId) {
startTask(data.taskId, taskName, filesProgressInfo)
}
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
await uploadFiles(currentFiles, data.presignedUrls, data.taskId!)
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
},
})
const processAndUploadFiles = (selectedFiles: FileList | File[]) => {
if (!selectedFiles || selectedFiles.length === 0) return
const fileWithIds: FileWithId[] = []
const root: CreateUploadTaskRequest['files'] = []
const directories = new Map<string, CreateUploadTaskRequest['files']>()
for (const file of selectedFiles) {
const path = (file.webkitRelativePath || file.name).split('/')
if (path.some((part) => part.startsWith('.'))) {
continue
}
const fileName = path.pop()!
let currentLevel = root
let currentPath = ''
for (const dir of path) {
currentPath = `${currentPath}/${dir}`
if (!directories.has(currentPath)) {
const newDir: CreateUploadTaskRequest['files'] = []
const parentDir = {
name: dir,
type: 'folder' as const,
id: ulid(),
children: newDir,
size: 0,
}
currentLevel.push(parentDir)
directories.set(currentPath, newDir)
currentLevel = newDir
} else {
currentLevel = directories.get(currentPath)!
}
}
const fileWithId = { file, id: ulid() }
fileWithIds.push(fileWithId)
let mediaType = file.type
if (fileName.toLowerCase().endsWith('.wma') && mediaType?.startsWith('video/')) {
mediaType = mediaType.replace(/^video\//, 'audio/')
}
currentLevel.push({
name: fileName,
size: file.size,
type: 'file',
id: fileWithId.id,
children: [],
mediaType,
})
}
pendingFilesToUpload = fileWithIds
createUploadTaskMutation({
param: { teamId: teamId },
json: {
parentId: assetId,
files: root,
},
})
}
const $createFolder = client.api.folders.$post
const { mutate: createFolderMutation, isPending: isCreatingFolder } = useMutation({
mutationFn: async (name: string) => {
const res = await $createFolder({
json: { name, parentId: assetId },
})
if (!res.ok) throw new Error('Failed to create folder')
return await res.json()
},
onSuccess: () => {
toast.success(m.new_folder())
setIsNewFolderDialogOpen(false)
setNewFolderName('')
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
queryClient.invalidateQueries({ queryKey: ['folders'] })
},
onError: (err) => {
toast.error(`Error: ${err.message}`)
},
})
const { mutate: emptyTrash, isPending: isEmptyingTrash } = useMutation({
mutationFn: async () => {
const res = await client.api.projects[':projectId']['empty-trash'].$post({
param: { projectId },
})
if (!res.ok) throw new Error('Failed to empty trash')
return await res.json()
},
onSuccess: () => {
toast.success(m.trash_emptied_successfully())
queryClient.invalidateQueries({
queryKey: ['projects', projectId, 'recently-deleted'],
})
onClearSelection()
setIsEmptyTrashDialogOpen(false)
},
onError: (err) => {
toast.error(`Error: ${err.message}`)
},
})
const handleCreateFolderSubmit = (e: React.FormEvent) => {
e.preventDefault()
if (!newFolderName.trim()) return
createFolderMutation(newFolderName.trim())
}
const isFolderEmpty =
folders.length === 0 &&
files.length === 0 &&
!isFetchingNextFoldersPage &&
!isFetchingNextFilesPage
return (
<div className="flex-1 flex flex-col min-h-0 bg-background overflow-hidden relative">
{/* Hidden file inputs for upload actions */}
{canEdit && (
<>
<input
type="file"
ref={fileInputRef}
multiple
className="hidden"
onChange={(e) => {
if (e.target.files) processAndUploadFiles(e.target.files)
}}
/>
<input
type="file"
ref={folderInputRef}
multiple
/* @ts-expect-error - webkitdirectory is not in standard input type */
webkitdirectory="true"
className="hidden"
onChange={(e) => {
if (e.target.files) processAndUploadFiles(e.target.files)
}}
/>
</>
)}
{/* Recently Deleted banner */}
{isRecentlyDeleted && (
<div className="flex items-center justify-between px-4 py-2.5 bg-muted/20 border-b border-border text-sm shrink-0">
<div className="flex items-center gap-2 text-muted-foreground">
<AlertTriangle className="h-4 w-4 text-amber-500 shrink-0" />
<span className="text-xs">{m.recently_deleted_notice()}</span>
</div>
{canAdmin && (
<Button
variant="destructive"
size="sm"
disabled={isEmptyingTrash || (folders.length === 0 && files.length === 0)}
onClick={() => setIsEmptyTrashDialogOpen(true)}
className="h-7 px-2.5 text-xs font-medium bg-destructive/10 text-destructive border border-destructive/20 hover:bg-destructive hover:text-destructive-foreground"
>
Delete
</Button>
)}
</div>
)}
{/* Main scroll area */}
<div className="flex-1 overflow-y-auto min-h-0 p-3 pb-24">
<div className="max-w-lg mx-auto w-full">
{/* Folders Section */}
{foldersCount > 0 && (
<div className="mb-4">
<button
onClick={() => setFoldersExpanded(!foldersExpanded)}
className="flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground mb-3 font-medium select-none cursor-pointer"
>
{foldersExpanded ? (
<ChevronDown className="h-4 w-4" />
) : (
<ChevronRight className="h-4 w-4" />
)}
<span>
{formatCount(foldersCount, false)}
{showFoldersSize ? ` • ${formatSize(foldersSizeVal)}` : ''}
</span>
</button>
{foldersExpanded && (
<div className="grid grid-cols-1 gap-4 w-full">
{folders.map((folder) => (
<FolderCard
key={folder.id}
item={folder}
isSelected={selectedItem?.id === folder.id}
isChecked={selectedIds.has(folder.id!)}
isEditing={editingItemId === folder.id}
onSelect={onItemSelect}
onDoubleClick={onItemDoubleClick}
onContextMenu={() => {}}
onDragStart={() => {}}
onDrop={() => {}}
onRename={(newName) => onRenameSubmit(folder, newName)}
onFinishEditing={() => setEditingItemId(null)}
onAction={(action, item) =>
handleAction(action as 'rename' | 'delete' | 'download' | 'restore', item)
}
isRecentlyDeleted={isRecentlyDeleted}
isRecents={isRecents}
selectedCount={selectedIds.size}
canEdit={canEdit}
isShareView={isShareView}
allowDownload={allowDownload}
/>
))}
</div>
)}
</div>
)}
{/* Files Section */}
{filesCount > 0 && (
<div className="mb-4">
<button
onClick={() => setFilesExpanded(!filesExpanded)}
className="flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground mb-3 font-medium select-none cursor-pointer"
>
{filesExpanded ? (
<ChevronDown className="h-4 w-4" />
) : (
<ChevronRight className="h-4 w-4" />
)}
<span>
{formatCount(filesCount, true)}
{showFilesSize ? ` • ${formatSize(filesSizeVal)}` : ''}
</span>
</button>
{filesExpanded && (
<div className="grid grid-cols-1 gap-4 w-full">
{files.map((file) => (
<FileCard
key={file.id}
teamId={teamId}
item={file}
isSelected={selectedItem?.id === file.id}
isChecked={selectedIds.has(file.id!)}
isEditing={editingItemId === file.id}
onSelect={onItemSelect}
onDoubleClick={onItemDoubleClick}
onContextMenu={() => {}}
onDragStart={() => {}}
onDrop={() => {}}
onRename={(newName) => onRenameSubmit(file, newName)}
onFinishEditing={() => setEditingItemId(null)}
onSaveField={(fieldId, value) => onSaveField?.(file.id!, fieldId, value)}
fields={[]}
onAction={(action, item) =>
handleAction(action as 'rename' | 'delete' | 'download' | 'restore', item)
}
isRecentlyDeleted={isRecentlyDeleted}
isRecents={isRecents}
selectedCount={selectedIds.size}
canEdit={canEdit}
isShareView={isShareView}
allowDownload={allowDownload}
/>
))}
</div>
)}
</div>
)}
</div>
{/* Empty state */}
{isFolderEmpty && (
<div className="flex flex-col items-center justify-center h-64 text-sm text-muted-foreground p-6 text-center">
{isRecents ? (
<div className="flex flex-col items-center gap-2">
<FolderClock className="h-10 w-10 text-muted-foreground/40 mb-1" />
<p className="font-medium text-foreground">{m.no_recent_files()}</p>
<p className="text-xs text-muted-foreground">{m.no_recent_files_description()}</p>
</div>
) : (
<span>{m.this_folder_is_empty()}</span>
)}
</div>
)}
{/* Infinite scroll loader / trigger */}
<div ref={loadMoreRef} className="py-4 flex justify-center items-center min-h-6">
{(isFetchingNextFoldersPage || isFetchingNextFilesPage) && (
<Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
)}
</div>
</div>
{/* Floating Action Button (+) for users with edit permissions */}
{canEdit && !isRecentlyDeleted && !isRecents && !isShareView && !isPublic && (
<div className="fixed bottom-6 right-6 z-30">
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<Button
size="icon"
className="h-14 w-14 rounded-full bg-primary text-primary-foreground shadow-lg shadow-primary/25 hover:bg-primary/90 flex items-center justify-center"
aria-label="Actions"
>
<Plus className="h-6 w-6" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" side="top" className="w-48 mb-2">
<DropdownMenuItem
onClick={() => fileInputRef.current?.click()}
className="flex items-center gap-2.5 py-2 cursor-pointer"
>
<Upload className="h-4 w-4" />
<span>{m.upload_file()}</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => folderInputRef.current?.click()}
className="flex items-center gap-2.5 py-2 cursor-pointer"
>
<Upload className="h-4 w-4" />
<span>{m.upload_folder()}</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => setIsNewFolderDialogOpen(true)}
className="flex items-center gap-2.5 py-2 cursor-pointer"
>
<FolderPlus className="h-4 w-4" />
<span>{m.new_folder()}</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)}
{/* New Folder Dialog */}
<Dialog open={isNewFolderDialogOpen} onOpenChange={setIsNewFolderDialogOpen}>
<DialogContent className="sm:max-w-md">
<form onSubmit={handleCreateFolderSubmit}>
<DialogHeader>
<DialogTitle>{m.new_folder()}</DialogTitle>
<DialogDescription>{m.folder_name()}</DialogDescription>
</DialogHeader>
<div className="py-4">
<Input
autoFocus
placeholder={m.folder_name()}
value={newFolderName}
onChange={(e) => setNewFolderName(e.target.value)}
/>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setIsNewFolderDialogOpen(false)}
>
{m.cancel()}
</Button>
<Button type="submit" disabled={!newFolderName.trim() || isCreatingFolder}>
{isCreatingFolder ? <Loader2 className="h-4 w-4 animate-spin mr-2" /> : null}
{m.confirm()}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
{/* Delete Confirmation Alert Dialog */}
<AlertDialog open={isDeleteDialogOpen} onOpenChange={setIsDeleteDialogOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{m.delete_asset_title()}</AlertDialogTitle>
<AlertDialogDescription>{m.delete_asset_description()}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{m.cancel()}</AlertDialogCancel>
<AlertDialogAction
onClick={() => confirmDelete()}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{m.delete()}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Empty Trash Confirmation Alert Dialog */}
<AlertDialog open={isEmptyTrashDialogOpen} onOpenChange={setIsEmptyTrashDialogOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{m.empty_trash_title()}</AlertDialogTitle>
<AlertDialogDescription>{m.empty_trash_description()}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{m.cancel()}</AlertDialogCancel>
<AlertDialogAction
onClick={() => emptyTrash()}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
disabled={isEmptyingTrash}
>
{isEmptyingTrash ? 'Deleting...' : m.delete()}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Download Dialog */}
<Dialog open={isDownloadDialogOpen} onOpenChange={setIsDownloadDialogOpen}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>
{isLoadingLinks ? m.preparing_download() : m.confirm_download()}
</DialogTitle>
<DialogDescription>
{isLoadingLinks ? m.resolving_files_wait() : m.folder_files_download_description()}
</DialogDescription>
</DialogHeader>
{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">
{m.preparing_download_links()}
</span>
</div>
) : (
<div className="space-y-4 py-2">
<div className="text-sm text-muted-foreground">
{m.download_includes_files({ count: resolvedFiles.length })}
</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">{m.no_folder_structure()}</p>
<p className="mt-0.5 text-xs text-amber-700/90 dark:text-amber-400/90">
{m.folder_structure_warning()}
</p>
</div>
</div>
</div>
)}
<DialogFooter>
{!isLoadingLinks ? (
<>
<Button variant="outline" onClick={() => setIsDownloadDialogOpen(false)}>
{m.cancel()}
</Button>
<Button
onClick={startDownload}
className="gap-2"
disabled={resolvedFiles.length === 0}
>
<Download className="h-4 w-4" />
{m.start_download()}
</Button>
</>
) : (
<Button variant="outline" onClick={() => setIsDownloadDialogOpen(false)}>
{m.cancel()}
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
}