'use client' 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, { useMemo, useState, useEffect, useRef } from 'react' import { cn } from '../../lib/utils' import type { DragState } from '../dnd-types' interface ReorderIndicatorProps { id: string item: AssetInfo position: 'before' | 'after' active: boolean className?: string dragState?: DragState isNextItemDragging?: boolean } function ReorderIndicator({ id, item, position, active, className, dragState, isNextItemDragging, }: ReorderIndicatorProps) { const isDraggingItem = dragState?.draggedIds.has(item.id!) const isDraggingAny = dragState?.isActive const { ref: setNodeRef, isDropTarget: isOver } = useDroppable({ id, data: { type: 'reorder', item, position, }, disabled: !active || isDraggingItem || !isDraggingAny, }) if (isDraggingItem || isNextItemDragging) return null return (
) } interface FileBrowserGridViewProps { folders: AssetInfo[] files: AssetInfo[] totalFolders?: number totalFiles?: number renderItem: (item: AssetInfo) => React.ReactNode foldersExpanded: boolean setFoldersExpanded: (expanded: boolean) => void filesExpanded: boolean setFilesExpanded: (expanded: boolean) => void hasNextFoldersPage: boolean hasNextFilesPage: boolean isFetchingNextFoldersPage: boolean isFetchingNextFilesPage: boolean fetchNextFoldersPage: () => void fetchNextFilesPage: () => void formatCount: (count: number, isFile: boolean) => string formatSize: (bytes: number) => string foldersSize: number filesSize: number handleEmptyAreaClick: (e: React.MouseEvent) => void dragState?: DragState sort?: SearchSort scrollContainerRef: React.RefObject } export function FileBrowserGridView({ folders, files, totalFolders, totalFiles, renderItem, foldersExpanded, setFoldersExpanded, filesExpanded, setFilesExpanded, hasNextFoldersPage, hasNextFilesPage, isFetchingNextFoldersPage, isFetchingNextFilesPage, fetchNextFoldersPage, fetchNextFilesPage, formatCount, formatSize, foldersSize, filesSize, handleEmptyAreaClick, dragState, sort, scrollContainerRef, }: FileBrowserGridViewProps) { const containerRef = useRef(null) const [cols, setCols] = useState(5) 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 for (const virtualItem of virtualItems) { const row = rows[virtualItem.index] if (row?.type === 'row') { if (row.kind === 'folder') { const lastIndex = (row.rowIndex + 1) * cols if ( lastIndex >= folders.length - cols * 2 && hasNextFoldersPage && !isFetchingNextFoldersPage ) { fetchNextFoldersPage() } } else if (row.kind === 'file') { const lastIndex = (row.rowIndex + 1) * cols if ( lastIndex >= files.length - cols * 2 && hasNextFilesPage && !isFetchingNextFilesPage ) { fetchNextFilesPage() } } } } }, [ virtualItems, rows, folders.length, files.length, hasNextFoldersPage, hasNextFilesPage, isFetchingNextFoldersPage, isFetchingNextFilesPage, fetchNextFoldersPage, fetchNextFilesPage, cols, ]) return (
{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 (
) } 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 (
{rowItems.map((item, i) => { const itemIndex = startIndex + i if (!item) { const totalCount = isFolder ? (totalFolders ?? folders.length) : (totalFiles ?? files.length) if (itemIndex < totalCount) { return (
) } return
} return (
{renderItem(item)}
) })}
) })}
) }