mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 08:04:37 +00:00
feat(webui): implement virtualization for file list and grid views
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import type { SearchSort } from '@shumai/dtos'
|
||||
import { useDroppable } from '@dnd-kit/react'
|
||||
import { useVirtualizer } from '@tanstack/react-virtual'
|
||||
import { ChevronDown, ChevronRight } from 'lucide-react'
|
||||
import React from 'react'
|
||||
import React, { useMemo, useState, useEffect, useRef } from 'react'
|
||||
import { cn } from '../../lib/utils'
|
||||
import type { DragState } from '../dnd-types'
|
||||
|
||||
@@ -47,13 +48,6 @@ function ReorderIndicator({
|
||||
className={cn(
|
||||
'absolute top-0 bottom-0 w-4 flex-shrink-0 flex items-center justify-center pointer-events-auto z-50',
|
||||
position === 'before' ? '-left-2 -translate-x-1/2' : '-right-2 translate-x-1/2',
|
||||
position === 'before' && [
|
||||
'hidden',
|
||||
'max-md:[.group-reorder:nth-child(2n+1)_&]:flex',
|
||||
'md:max-lg:[.group-reorder:nth-child(3n+1)_&]:flex',
|
||||
'lg:max-xl:[.group-reorder:nth-child(4n+1)_&]:flex',
|
||||
'xl:[.group-reorder:nth-child(5n+1)_&]:flex',
|
||||
],
|
||||
className,
|
||||
)}
|
||||
>
|
||||
@@ -77,10 +71,10 @@ interface FileBrowserGridViewProps {
|
||||
setFoldersExpanded: (expanded: boolean) => void
|
||||
filesExpanded: boolean
|
||||
setFilesExpanded: (expanded: boolean) => void
|
||||
foldersRef: (node?: Element | null) => void
|
||||
filesRef: (node?: Element | null) => void
|
||||
hasNextFoldersPage: boolean
|
||||
hasNextFilesPage: boolean
|
||||
fetchNextFoldersPage: () => void
|
||||
fetchNextFilesPage: () => void
|
||||
formatCount: (count: number, isFile: boolean) => string
|
||||
formatSize: (bytes: number) => string
|
||||
foldersSize: number
|
||||
@@ -88,6 +82,7 @@ interface FileBrowserGridViewProps {
|
||||
handleEmptyAreaClick: (e: React.MouseEvent) => void
|
||||
dragState?: DragState
|
||||
sort?: SearchSort
|
||||
scrollContainerRef: React.RefObject<HTMLDivElement | null>
|
||||
}
|
||||
|
||||
export function FileBrowserGridView({
|
||||
@@ -100,10 +95,10 @@ export function FileBrowserGridView({
|
||||
setFoldersExpanded,
|
||||
filesExpanded,
|
||||
setFilesExpanded,
|
||||
foldersRef,
|
||||
filesRef,
|
||||
hasNextFoldersPage,
|
||||
hasNextFilesPage,
|
||||
fetchNextFoldersPage,
|
||||
fetchNextFilesPage,
|
||||
formatCount,
|
||||
formatSize,
|
||||
foldersSize,
|
||||
@@ -111,78 +106,183 @@ export function FileBrowserGridView({
|
||||
handleEmptyAreaClick,
|
||||
dragState,
|
||||
sort,
|
||||
scrollContainerRef,
|
||||
}: FileBrowserGridViewProps) {
|
||||
return (
|
||||
<div className="flex-1 p-4" onClick={handleEmptyAreaClick}>
|
||||
{folders.length > 0 && (
|
||||
<div className="mb-8">
|
||||
<button
|
||||
onClick={() => setFoldersExpanded(!foldersExpanded)}
|
||||
className="mb-3 flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
{foldersExpanded ? (
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
) : (
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
)}
|
||||
<span>
|
||||
{formatCount(totalFolders ?? folders.length, false)} • {formatSize(foldersSize)}
|
||||
</span>
|
||||
</button>
|
||||
{foldersExpanded && (
|
||||
<>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4">
|
||||
{folders.map((item, index) => (
|
||||
<div key={item.id} className="group-reorder relative flex">
|
||||
<ReorderIndicator
|
||||
id={`reorder-before-folder-${item.id}`}
|
||||
item={item}
|
||||
position="before"
|
||||
active={sort?.field === 'custom'}
|
||||
dragState={dragState}
|
||||
/>
|
||||
<div className="flex-1 min-w-0">{renderItem(item)}</div>
|
||||
<ReorderIndicator
|
||||
id={`reorder-after-folder-${item.id}`}
|
||||
item={item}
|
||||
position="after"
|
||||
active={sort?.field === 'custom'}
|
||||
dragState={dragState}
|
||||
isNextItemDragging={
|
||||
folders[index + 1] && dragState?.draggedIds.has(folders[index + 1].id!)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{hasNextFoldersPage && <div ref={foldersRef} className="h-1" />}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const [cols, setCols] = useState(5)
|
||||
|
||||
{files.length > 0 && (
|
||||
<div>
|
||||
<button
|
||||
onClick={() => setFilesExpanded(!filesExpanded)}
|
||||
className="mb-3 flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
{filesExpanded ? (
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
) : (
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
)}
|
||||
<span>
|
||||
{formatCount(totalFiles ?? files.length, true)} • {formatSize(filesSize)}
|
||||
</span>
|
||||
</button>
|
||||
{filesExpanded && (
|
||||
<>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4">
|
||||
{files.map((item, index) => (
|
||||
useEffect(() => {
|
||||
const updateCols = () => {
|
||||
if (!containerRef.current) return
|
||||
const width = containerRef.current.offsetWidth
|
||||
if (width < 640) setCols(2)
|
||||
else if (width < 768) setCols(2)
|
||||
else if (width < 1024) setCols(3)
|
||||
else if (width < 1280) setCols(4)
|
||||
else setCols(5)
|
||||
}
|
||||
|
||||
updateCols()
|
||||
const observer = new ResizeObserver(updateCols)
|
||||
if (containerRef.current) observer.observe(containerRef.current)
|
||||
return () => observer.disconnect()
|
||||
}, [])
|
||||
|
||||
const rows = useMemo(() => {
|
||||
const list: (
|
||||
| { type: 'header'; kind: 'folder' | 'file' }
|
||||
| { type: 'row'; kind: 'folder' | 'file'; rowIndex: number }
|
||||
)[] = []
|
||||
|
||||
// Folders
|
||||
list.push({ type: 'header', kind: 'folder' })
|
||||
if (foldersExpanded) {
|
||||
const count = totalFolders ?? folders.length
|
||||
const rowCount = Math.ceil(count / cols)
|
||||
for (let i = 0; i < rowCount; i++) {
|
||||
list.push({ type: 'row', kind: 'folder', rowIndex: i })
|
||||
}
|
||||
}
|
||||
|
||||
// Files
|
||||
list.push({ type: 'header', kind: 'file' })
|
||||
if (filesExpanded) {
|
||||
const count = totalFiles ?? files.length
|
||||
const rowCount = Math.ceil(count / cols)
|
||||
for (let i = 0; i < rowCount; i++) {
|
||||
list.push({ type: 'row', kind: 'file', rowIndex: i })
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}, [foldersExpanded, filesExpanded, folders.length, files.length, totalFolders, totalFiles, cols])
|
||||
|
||||
const rowVirtualizer = useVirtualizer({
|
||||
count: rows.length,
|
||||
getScrollElement: () => scrollContainerRef.current,
|
||||
estimateSize: (index) => (rows[index]?.type === 'header' ? 40 : 200),
|
||||
overscan: 5,
|
||||
})
|
||||
|
||||
const virtualItems = rowVirtualizer.getVirtualItems()
|
||||
|
||||
useEffect(() => {
|
||||
if (virtualItems.length === 0) return
|
||||
|
||||
const lastItem = virtualItems[virtualItems.length - 1]
|
||||
const row = rows[lastItem.index]
|
||||
|
||||
if (row.type === 'row') {
|
||||
if (row.kind === 'folder') {
|
||||
const lastIndex = (row.rowIndex + 1) * cols
|
||||
if (lastIndex >= folders.length - cols * 2 && hasNextFoldersPage) {
|
||||
fetchNextFoldersPage()
|
||||
}
|
||||
} else if (row.kind === 'file') {
|
||||
const lastIndex = (row.rowIndex + 1) * cols
|
||||
if (lastIndex >= files.length - cols * 2 && hasNextFilesPage) {
|
||||
fetchNextFilesPage()
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [
|
||||
virtualItems,
|
||||
rows,
|
||||
folders.length,
|
||||
files.length,
|
||||
hasNextFoldersPage,
|
||||
hasNextFilesPage,
|
||||
fetchNextFoldersPage,
|
||||
fetchNextFilesPage,
|
||||
cols,
|
||||
])
|
||||
|
||||
return (
|
||||
<div ref={containerRef} className="flex-1 p-4 relative" onClick={handleEmptyAreaClick}>
|
||||
<div
|
||||
className="relative w-full"
|
||||
style={{
|
||||
height: `${rowVirtualizer.getTotalSize()}px`,
|
||||
}}
|
||||
>
|
||||
{virtualItems.map((virtualRow) => {
|
||||
const row = rows[virtualRow.index]
|
||||
|
||||
if (row.type === 'header') {
|
||||
const isFolder = row.kind === 'folder'
|
||||
const count = isFolder ? (totalFolders ?? folders.length) : (totalFiles ?? files.length)
|
||||
const size = isFolder ? foldersSize : filesSize
|
||||
const expanded = isFolder ? foldersExpanded : filesExpanded
|
||||
const setExpanded = isFolder ? setFoldersExpanded : setFilesExpanded
|
||||
|
||||
return (
|
||||
<div
|
||||
key={`header-${row.kind}`}
|
||||
ref={rowVirtualizer.measureElement}
|
||||
className="absolute top-0 left-0 w-full z-10 bg-background"
|
||||
style={{
|
||||
transform: `translateY(${virtualRow.start}px)`,
|
||||
}}
|
||||
>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
setExpanded(!expanded)
|
||||
}}
|
||||
className="mb-3 flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
{expanded ? (
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
) : (
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
)}
|
||||
<span>
|
||||
{formatCount(count, !isFolder)} • {formatSize(size)}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const isFolder = row.kind === 'folder'
|
||||
const dataList = isFolder ? folders : files
|
||||
const startIndex = row.rowIndex * cols
|
||||
const rowItems = []
|
||||
for (let i = 0; i < cols; i++) {
|
||||
rowItems.push(dataList[startIndex + i])
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
key={`row-${row.kind}-${row.rowIndex}`}
|
||||
ref={rowVirtualizer.measureElement}
|
||||
className="absolute top-0 left-0 w-full grid gap-4"
|
||||
style={{
|
||||
transform: `translateY(${virtualRow.start}px)`,
|
||||
gridTemplateColumns: `repeat(${cols}, minmax(0, 1fr))`,
|
||||
}}
|
||||
>
|
||||
{rowItems.map((item, i) => {
|
||||
const itemIndex = startIndex + i
|
||||
if (!item) {
|
||||
const totalCount = isFolder
|
||||
? (totalFolders ?? folders.length)
|
||||
: (totalFiles ?? files.length)
|
||||
if (itemIndex < totalCount) {
|
||||
return (
|
||||
<div
|
||||
key={`skeleton-${itemIndex}`}
|
||||
className="aspect-square bg-muted animate-pulse rounded-lg"
|
||||
/>
|
||||
)
|
||||
}
|
||||
return <div key={`empty-${itemIndex}`} />
|
||||
}
|
||||
|
||||
return (
|
||||
<div key={item.id} className="group-reorder relative flex">
|
||||
<ReorderIndicator
|
||||
id={`reorder-before-file-${item.id}`}
|
||||
id={`reorder-before-${row.kind}-${item.id}`}
|
||||
item={item}
|
||||
position="before"
|
||||
active={sort?.field === 'custom'}
|
||||
@@ -190,23 +290,23 @@ export function FileBrowserGridView({
|
||||
/>
|
||||
<div className="flex-1 min-w-0">{renderItem(item)}</div>
|
||||
<ReorderIndicator
|
||||
id={`reorder-after-file-${item.id}`}
|
||||
id={`reorder-after-${row.kind}-${item.id}`}
|
||||
item={item}
|
||||
position="after"
|
||||
active={sort?.field === 'custom'}
|
||||
dragState={dragState}
|
||||
isNextItemDragging={
|
||||
files[index + 1] && dragState?.draggedIds.has(files[index + 1].id!)
|
||||
dataList[itemIndex + 1] &&
|
||||
dragState?.draggedIds.has(dataList[itemIndex + 1].id!)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{hasNextFilesPage && <div ref={filesRef} className="h-1" />}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user