2026-06-01 10:34:22 +08:00
import { Checkbox } from '@/ui/components/ui/checkbox'
2026-05-19 16:05:09 +08:00
import {
DropdownMenu ,
DropdownMenuContent ,
DropdownMenuItem ,
DropdownMenuSeparator ,
DropdownMenuTrigger ,
} from '@/ui/components/ui/dropdown-menu'
2026-06-01 10:34:22 +08:00
import { EditableText } from '@/ui/components/ui/editable-text'
2026-05-19 16:05:09 +08:00
import { cn } from '@/ui/lib/utils'
import { useDraggable , useDroppable } from '@dnd-kit/react'
2026-06-11 01:50:35 +08:00
import type { AssetInfo , ChildPreview } from '@shumai/dtos'
2026-05-19 16:05:09 +08:00
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
2026-06-07 10:49:48 +08:00
onAction ?: (
action : 'rename' | 'delete' | 'download' | 'restore' | 'remove-from-share' ,
item : AssetInfo ,
) => void
2026-05-19 16:05:09 +08:00
isRecentlyDeleted? : boolean
selectedCount? : number
2026-06-07 09:51:33 +08:00
canEdit? : boolean
2026-06-07 10:49:48 +08:00
isShareView? : boolean
2026-05-19 16:05:09 +08:00
}
const FolderPreviewGrid = ({ items } : { items : ChildPreview [] }) => {
const latestChildren = items as NonNullable < AssetInfo [ 'latestChildren' ] >
const safeChildren = latestChildren ? latestChildren . slice ( 0 , 3 ) : []
const count = safeChildren . length
const renderItem = ( child : ChildPreview ) => {
if ( ! child ) return null
return < FilePreview item = { child } />
}
if ( count === 0 ) {
return < div ></ div >
}
// 1 Item
if ( count === 1 ) {
2026-06-11 01:50:35 +08:00
return (
< div className = "w-full h-full bg-foreground/6 rounded-sm" >{ renderItem ( safeChildren [ 0 ])}</ div >
)
2026-05-19 16:05:09 +08:00
}
// 2 Items
if ( count === 2 ) {
return (
< div className = "grid grid-cols-2 gap-0.5 h-full w-full" >
2026-06-11 01:50:35 +08:00
< div className = "overflow-hidden h-full bg-foreground/6 rounded-sm" >
2026-05-19 16:05:09 +08:00
{ renderItem ( safeChildren [ 0 ])}
</ div >
2026-06-11 01:50:35 +08:00
< div className = "overflow-hidden h-full bg-foreground/6 rounded-sm" >
2026-05-19 16:05:09 +08:00
{ renderItem ( safeChildren [ 1 ])}
</ div >
</ div >
)
}
// 3 Items
return (
< div className = "grid grid-cols-3 gap-0.5 h-full w-full" >
2026-06-11 01:50:35 +08:00
< div className = "col-span-1 overflow-hidden h-full relative bg-foreground/6 rounded-sm col-span-2" >
2026-05-19 16:05:09 +08:00
< div className = "absolute inset-0 w-full h-full" >{ renderItem ( safeChildren [ 0 ])}</ div >
</ div >
< div className = "col-span-1 grid grid-rows-2 gap-0.5 h-full col-span-1" >
2026-06-11 01:50:35 +08:00
< div className = "overflow-hidden h-full relative bg-foreground/6 rounded-sm" >
2026-05-19 16:05:09 +08:00
< div className = "absolute inset-0 w-full h-full" >{ renderItem ( safeChildren [ 1 ])}</ div >
</ div >
2026-06-11 01:50:35 +08:00
< div className = "overflow-hidden h-full relative bg-foreground/6 rounded-sm" >
2026-05-19 16:05:09 +08:00
< div className = "absolute inset-0 w-full h-full" >{ renderItem ( safeChildren [ 2 ])}</ div >
</ div >
</ div >
</ div >
)
}
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 ,
2026-06-07 09:51:33 +08:00
canEdit = true ,
2026-06-07 10:49:48 +08:00
isShareView ,
2026-05-19 16:05:09 +08:00
} : FolderCardProps ) {
const [ name , setName ] = useState ( item . name || '' )
const inputRef = useRef < HTMLInputElement >( 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 < HTMLInputElement >) => {
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 ,
2026-06-11 01:50:35 +08:00
tabRadius : 5 ,
2026-06-01 10:34:22 +08:00
tabSlope : 15 ,
2026-06-11 01:50:35 +08:00
tabBaseWidth : 120 ,
2026-05-19 16:05:09 +08:00
}
const folderPath = useMemo (() => {
const { cornerRadius : cr , tabRadius : tr , tabSlope : ts , tabBaseWidth : tbw } = config
const s = 1
2026-06-11 01:50:35 +08:00
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`
2026-05-19 16:05:09 +08:00
. replace ( /\s+/g , ' ' )
. trim ()
}, [])
return (
< div
ref = { setNodeRef }
style = {{ opacity }}
onClick = { handleClick }
onDoubleClick = {() => 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' ,
)}
>
2026-06-01 10:34:22 +08:00
< div className = "absolute inset-0 z-10 pointer-events-none" >
< svg
width = "100%"
height = "100%"
viewBox = { `0 0 ${ width } ${ height } ` }
preserveAspectRatio = "none"
className = "overflow-visible"
>
< path
d = { folderPath }
vectorEffect = "non-scaling-stroke"
strokeLinejoin = "round"
2026-06-11 15:42:40 +08:00
strokeWidth = { showDropFeedback || isSelected ? 2 : 1 }
2026-06-01 10:34:22 +08:00
className = { cn (
2026-06-11 01:50:35 +08:00
'transition-colors duration-200 fill-none group-hover:stroke-primary stroke-foreground/10' ,
2026-06-01 10:34:22 +08:00
( isSelected || isChecked || showDropFeedback ) && 'stroke-primary' ,
)}
/>
</ svg >
</ div >
2026-06-11 01:50:35 +08:00
< div className = "absolute inset-0 -z-10 pointer-events-none" >
2026-05-19 16:05:09 +08:00
< svg
width = "100%"
height = "100%"
viewBox = { `0 0 ${ width } ${ height } ` }
preserveAspectRatio = "none"
className = "overflow-visible"
>
< path
d = { folderPath }
vectorEffect = "non-scaling-stroke"
strokeLinejoin = "round"
2026-06-01 10:34:22 +08:00
strokeWidth = { showDropFeedback ? 3 : 1 }
2026-06-11 01:50:35 +08:00
className = { cn (
'transition-colors duration-200 fill-foreground/6 dark:fill-foreground/4' ,
)}
2026-05-19 16:05:09 +08:00
/>
</ svg >
</ div >
2026-06-11 01:50:35 +08:00
< div className = "shadow-[0_-4px_6px_rgba(0,0,0,0.08)] relative w-full flex flex-1 flex-col p-2 mt-5 bg-muted dark:bg-muted/50 rounded-sm" >
2026-05-19 16:05:09 +08:00
< div className = "relative w-full aspect-video shrink-0 overflow-hidden" >
< div className = "absolute top-1 left-1 z-30 transition-all duration-200" >
< Checkbox
checked = { isChecked }
onClick = {( e ) => e . stopPropagation ()} // Let parent handler deal with it
2026-06-01 10:34:22 +08:00
className = "w-5 h-5 border-1 border-foreground/20 bg-background/50 data-[state=checked]:bg-primary data-[state=checked]:border-primary"
2026-05-19 16:05:09 +08:00
/>
</ div >
{ item . latestChildren && item . latestChildren . length > 0 && (
< FolderPreviewGrid items = { item . latestChildren } />
)}
</ div >
< div className = "flex flex-col gap-1 mt-2 px-1" >
< EditableText
ref = { inputRef }
value = { name || '' }
onChange = {( e ) => setName ( e . target . value )}
onBlur = { handleRename }
onKeyDown = { handleKeyDown }
disabled = { ! isEditing }
2026-06-01 10:34:22 +08:00
className = "font-semibold text-foreground text-lg leading-tight truncate h-auto p-0 !bg-transparent"
2026-05-19 16:05:09 +08:00
title = { name }
/>
2026-06-01 10:34:22 +08:00
< div className = "flex items-center justify-between text-muted-foreground" >
2026-05-19 16:05:09 +08:00
< span className = "text-xs tracking-wide opacity-80" >
{ item . fileCount || 0 } {( item . fileCount || 0 ) === 1 ? 'Item' : 'Items' }
</ span >
2026-05-24 10:02:08 +08:00
< DropdownMenu modal = { false }>
2026-05-19 16:05:09 +08:00
< DropdownMenuTrigger asChild >
< button
onClick = {( e ) => e . stopPropagation ()}
2026-06-01 10:34:22 +08:00
className = "p-1.5 -mr-2 rounded-full hover:bg-muted text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
2026-05-19 16:05:09 +08:00
>
< MoreHorizontal size = { 18 } />
</ button >
</ DropdownMenuTrigger >
< DropdownMenuContent align = "end" >
2026-06-07 10:49:48 +08:00
{ isShareView ? (
<>
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'download' , item )
}}
>
< Download className = "mr-2 h-4 w-4" />
< span > Download </ span >
</ DropdownMenuItem >
{ canEdit && (
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'remove-from-share' , item )
}}
className = "text-destructive"
>
< Trash2 className = "mr-2 h-4 w-4" />
< span > Remove from Share </ span >
</ DropdownMenuItem >
)}
</>
) : isRecentlyDeleted ? (
2026-05-19 16:05:09 +08:00
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'restore' , item )
}}
>
< History className = "mr-2 h-4 w-4" />
< span > Restore </ span >
</ DropdownMenuItem >
) : (
<>
2026-06-07 09:51:33 +08:00
{ canEdit && ( ! isChecked || ( selectedCount || 0 ) <= 1 ) && (
2026-05-19 16:05:09 +08:00
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'rename' , item )
}}
>
< Edit className = "mr-2 h-4 w-4" />
< span > Rename </ span >
</ DropdownMenuItem >
)}
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'download' , item )
}}
>
< Download className = "mr-2 h-4 w-4" />
< span > Download </ span >
</ DropdownMenuItem >
2026-06-07 09:51:33 +08:00
{ canEdit && (
<>
< DropdownMenuSeparator />
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'delete' , item )
}}
className = "text-destructive"
>
< Trash2 className = "mr-2 h-4 w-4" />
< span > Delete </ span >
</ DropdownMenuItem >
</>
)}
2026-05-19 16:05:09 +08:00
</>
)}
</ DropdownMenuContent >
</ DropdownMenu >
</ div >
</ div >
</ div >
</ div >
)
}