import { Checkbox } from '@/ui/components/ui/checkbox' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/ui/components/ui/dropdown-menu' import { EditableText } from '@/ui/components/ui/editable-text' import { cn } from '@/ui/lib/utils' import { useDraggable, useDroppable } from '@dnd-kit/react' import type { AssetInfo, ChildPreview } from '@shumai/dtos' import { Download, Edit, History, MoreHorizontal, Trash2 } from 'lucide-react' import React, { useEffect, useMemo, useRef, useState } from 'react' import type { DragState } from '../dnd-types' import { FilePreview } from './file-preview' interface FolderCardProps { item: AssetInfo isSelected: boolean isChecked: boolean isEditing: boolean onSelect: (item: AssetInfo, event: React.MouseEvent) => void onDoubleClick: (item: AssetInfo) => void onContextMenu: (e: React.MouseEvent, item: AssetInfo) => void // Deprecated manual drag props replaced by dnd-kit onDragStart: (item: AssetInfo) => void onDrop: (target: AssetInfo) => void onRename: (newName: string) => void onFinishEditing: () => void dragState?: DragState disabled?: boolean onAction?: ( action: 'rename' | 'delete' | 'download' | 'restore' | 'remove-from-share', item: AssetInfo, ) => void isRecentlyDeleted?: boolean selectedCount?: number canEdit?: boolean isShareView?: boolean } const FolderPreviewGrid = ({ items }: { items: ChildPreview[] }) => { const latestChildren = items as NonNullable const safeChildren = latestChildren ? latestChildren.slice(0, 3) : [] const count = safeChildren.length const renderItem = (child: ChildPreview) => { if (!child) return null return } if (count === 0) { return
} // 1 Item if (count === 1) { return (
{renderItem(safeChildren[0])}
) } // 2 Items if (count === 2) { return (
{renderItem(safeChildren[0])}
{renderItem(safeChildren[1])}
) } // 3 Items return (
{renderItem(safeChildren[0])}
{renderItem(safeChildren[1])}
{renderItem(safeChildren[2])}
) } export function FolderCard({ item, isSelected, isChecked, isEditing, onSelect, onDoubleClick, onContextMenu, // eslint-disable-next-line @typescript-eslint/no-unused-vars onDragStart: _onDragStart, // eslint-disable-next-line @typescript-eslint/no-unused-vars onDrop: _onDrop, onRename, onFinishEditing, dragState, disabled, onAction, isRecentlyDeleted, selectedCount, canEdit = true, isShareView, }: FolderCardProps) { const [name, setName] = useState(item.name || '') const inputRef = useRef(null) // dnd-kit hooks const { ref: setDraggableRef, isDragging: isDraggableDragging } = useDraggable({ id: `browser:${item.id!}`, data: { type: 'folder', id: item.id, item: item, }, disabled: disabled, }) const { ref: setDroppableRef, isDropTarget: isOver } = useDroppable({ id: `browser:${item.id!}`, data: { type: 'folder', id: item.id, item: item, }, }) const setNodeRef = (node: HTMLElement | null) => { setDraggableRef(node) setDroppableRef(node) } const isBeingDragged = isDraggableDragging || (dragState?.draggedIds.has(item.id!) ?? false) const isValidDropTarget = useMemo(() => { if (!dragState?.isActive) return false if (dragState.draggedIds.has(item.id!)) return false // Can't drop on self // Folder is always valid drop target unless self/descendant return true }, [dragState, item.id]) const showDropFeedback = isOver && isValidDropTarget const opacity = isBeingDragged ? 0.5 : 1 useEffect(() => { if (isEditing) { // Use a longer timeout to ensure the context menu has closed and // the DOM has stabilized before focusing. Radix focus restoration // can sometimes conflict with immediate focus calls. const timeoutId = setTimeout(() => { inputRef.current?.focus() inputRef.current?.select() }, 150) return () => clearTimeout(timeoutId) } }, [isEditing]) useEffect(() => { setName(item.name || '') }, [item.name]) const handleClick = (e: React.MouseEvent) => { onSelect(item, e) } const handleRename = () => { if (name !== item.name) { onRename(name || '') } onFinishEditing() } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleRename() } if (e.key === 'Escape') { setName(item.name || '') onFinishEditing() } } // SVG background logic from fancy card const width = 300 const height = 220 const tabHeight = 8 const config = { cornerRadius: 4, tabRadius: 5, tabSlope: 15, tabBaseWidth: 120, } const folderPath = useMemo(() => { const { cornerRadius: cr, tabRadius: tr, tabSlope: ts, tabBaseWidth: tbw } = config const s = 1 return `M ${s}, ${tabHeight + s} L ${s}, ${s + tr} A ${tr} ${tr} 0 0 1 ${s + tr} ${s} L ${tbw - ts + s - tr} ${s} Q ${tbw - ts + s} ${s} ${tbw - ts + s} ${s} L ${tbw + s} ${tabHeight + s} L ${width - cr - s} ${tabHeight + s} A ${cr} ${cr} 0 0 1 ${width - s} ${tabHeight + cr + s} L ${width - s} ${height - cr - s} A ${cr} ${cr} 0 0 1 ${width - cr - s} ${height - s} L ${cr + s} ${height - s} A ${cr} ${cr} 0 0 1 ${s} ${height - cr - s} Z` .replace(/\s+/g, ' ') .trim() }, []) return (
onDoubleClick(item)} onContextMenu={(e) => onContextMenu(e, item)} onMouseDown={(e) => { if (e.shiftKey) { e.preventDefault() } }} className={cn( 'group relative w-full max-w-[340px] select-none cursor-pointer isolate flex flex-col items-center h-full', )} >
e.stopPropagation()} // Let parent handler deal with it className="w-5 h-5 border-1 border-foreground/20 bg-background/50 data-[state=checked]:bg-primary data-[state=checked]:border-primary" />
{item.latestChildren && item.latestChildren.length > 0 && ( )}
setName(e.target.value)} onBlur={handleRename} onKeyDown={handleKeyDown} disabled={!isEditing} className="font-semibold text-foreground text-lg leading-tight truncate h-auto p-0 !bg-transparent" title={name} />
{item.fileCount || 0} {(item.fileCount || 0) === 1 ? 'Item' : 'Items'} {isShareView ? ( <> { e.stopPropagation() onAction?.('download', item) }} > Download {canEdit && ( { e.stopPropagation() onAction?.('remove-from-share', item) }} className="text-destructive" > Remove from Share )} ) : isRecentlyDeleted ? ( { e.stopPropagation() onAction?.('restore', item) }} > Restore ) : ( <> {canEdit && (!isChecked || (selectedCount || 0) <= 1) && ( { e.stopPropagation() onAction?.('rename', item) }} > Rename )} { e.stopPropagation() onAction?.('download', item) }} > Download {canEdit && ( <> { e.stopPropagation() onAction?.('delete', item) }} className="text-destructive" > Delete )} )}
) }