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-07-06 10:38:41 +08:00
import { m } from '@/ui/paraglide/messages.js'
2026-09-19 11:36:14 +08:00
import { getTrashDaysLeft } from '@/ui/lib/time'
2026-11-05 09:57:34 +08:00
import { CopyDropdownMenuSub } from './copy-submenu'
import type { CopyKind } from './copy-options'
2026-11-03 20:31:56 +08:00
import {
Archive ,
ArchiveRestore ,
Clock ,
Download ,
Edit ,
History ,
MoreHorizontal ,
Trash2 ,
} from 'lucide-react'
2026-05-19 16:05:09 +08:00
import React , { useEffect , useMemo , useRef , useState } from 'react'
import type { DragState } from '../dnd-types'
import { FilePreview } from './file-preview'
2026-06-18 15:39:08 +08:00
import { getAllFilesFromEntries } from '@/ui/lib/dnd-utils'
2026-11-07 11:18:47 +08:00
import { handleCardClick } from '@/ui/lib/selection-utils'
2026-05-19 16:05:09 +08:00
interface FolderCardProps {
item : AssetInfo
2026-10-16 10:42:32 +08:00
isSelected? : boolean
isChecked? : boolean
isEditing? : boolean
2026-11-07 11:18:47 +08:00
onSelect ?: ( item : AssetInfo , event : React.MouseEvent , meta ?: { isCheckbox? : boolean }) => void
2026-10-16 10:42:32 +08:00
onDoubleClick ?: ( item : AssetInfo ) => void
onContextMenu ?: ( e : React.MouseEvent , item : AssetInfo ) => void
2026-05-19 16:05:09 +08:00
// Deprecated manual drag props replaced by dnd-kit
2026-10-16 10:42:32 +08:00
onDragStart ?: ( item : AssetInfo ) => void
onDrop ?: ( target : AssetInfo ) => void
onRename ?: ( newName : string ) => void
onFinishEditing ?: () => void
2026-05-19 16:05:09 +08:00
dragState? : DragState
disabled? : boolean
2026-06-07 10:49:48 +08:00
onAction ?: (
2026-11-03 20:31:56 +08:00
action :
| 'rename'
| 'delete'
| 'download'
| 'restore'
| 'remove-from-share'
| 'archive'
| 'restore-archive' ,
2026-06-07 10:49:48 +08:00
item : AssetInfo ,
) => void
2026-05-19 16:05:09 +08:00
isRecentlyDeleted? : boolean
2026-08-25 18:51:24 +08:00
isRecents? : boolean
2026-05-19 16:05:09 +08:00
selectedCount? : number
2026-06-07 09:51:33 +08:00
canEdit? : boolean
2026-11-03 20:31:56 +08:00
archiveStorageEnabled? : boolean
2026-06-07 10:49:48 +08:00
isShareView? : boolean
2026-11-07 11:18:47 +08:00
isPublic? : boolean
2026-06-18 15:39:08 +08:00
isExternalDragging? : boolean
externalOverFolderId? : string | null
setExternalOverFolderId ?: ( id : string | null ) => void
2026-06-18 15:53:18 +08:00
resetExternalDragState ?: () => void
2026-06-18 15:39:08 +08:00
onExternalDrop ?: ( files : File [], folderId : string ) => void
2026-08-08 10:25:02 +08:00
/** When false, hides the download action. Defaults to true. */
allowDownload? : boolean
2026-10-16 10:42:32 +08:00
hideCheckbox? : boolean
hideActions? : boolean
2026-11-05 09:57:34 +08:00
isTeamOwner? : boolean
onCopy ?: ( kind : CopyKind , items : AssetInfo []) => void
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 ,
2026-10-16 10:42:32 +08:00
isSelected = false ,
isChecked = false ,
isEditing = false ,
onSelect = () => {},
onDoubleClick = () => {},
onContextMenu = () => {},
2026-05-19 16:05:09 +08:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onDragStart : _onDragStart ,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onDrop : _onDrop ,
2026-10-16 10:42:32 +08:00
onRename = () => {},
onFinishEditing = () => {},
2026-05-19 16:05:09 +08:00
dragState ,
disabled ,
onAction ,
2026-11-05 09:57:34 +08:00
onCopy ,
isTeamOwner = false ,
2026-05-19 16:05:09 +08:00
isRecentlyDeleted ,
2026-08-25 18:51:24 +08:00
isRecents ,
2026-05-19 16:05:09 +08:00
selectedCount ,
2026-06-07 09:51:33 +08:00
canEdit = true ,
2026-11-03 20:31:56 +08:00
archiveStorageEnabled = false ,
2026-06-07 10:49:48 +08:00
isShareView ,
2026-11-07 11:18:47 +08:00
isPublic = false ,
2026-06-18 15:39:08 +08:00
isExternalDragging ,
externalOverFolderId ,
setExternalOverFolderId ,
2026-06-18 15:53:18 +08:00
resetExternalDragState ,
2026-06-18 15:39:08 +08:00
onExternalDrop ,
2026-08-08 10:25:02 +08:00
allowDownload = true ,
2026-10-16 10:42:32 +08:00
hideCheckbox = false ,
hideActions = false ,
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 ,
},
2026-10-16 10:42:32 +08:00
disabled : disabled ,
2026-05-19 16:05:09 +08:00
})
const setNodeRef = ( node : HTMLElement | null ) => {
2026-10-16 10:42:32 +08:00
if ( disabled ) return
2026-05-19 16:05:09 +08:00
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 ])
2026-06-18 15:39:08 +08:00
const isExternalOver = isExternalDragging && externalOverFolderId === item . id
const showDropFeedback = ( isOver && isValidDropTarget ) || isExternalOver
2026-05-19 16:05:09 +08:00
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 ) => {
2026-11-07 11:18:47 +08:00
handleCardClick ({
item ,
event : e ,
isPublic ,
selectedCount ,
isSelected ,
isChecked ,
onSelect ,
onDoubleClick ,
})
2026-05-19 16:05:09 +08:00
}
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 ()
}, [])
2026-09-19 11:36:14 +08:00
const daysLeft = isRecentlyDeleted ? getTrashDaysLeft ( item . deletedAt ) : null
const daysLeftTooltip =
daysLeft === null
? ''
: daysLeft === 1
? m . n_days_left_singular ({ count : daysLeft })
: m . n_days_left_plural ({ count : daysLeft })
2026-05-19 16:05:09 +08:00
return (
< div
ref = { setNodeRef }
style = {{ opacity }}
onClick = { handleClick }
2026-11-07 11:18:47 +08:00
onDoubleClick = { isPublic ? undefined : () => onDoubleClick ( item )}
2026-05-19 16:05:09 +08:00
onContextMenu = {( e ) => onContextMenu ( e , item )}
onMouseDown = {( e ) => {
if ( e . shiftKey ) {
e . preventDefault ()
}
}}
2026-06-18 15:39:08 +08:00
onDragEnter = {
isExternalDragging
? () => {
setExternalOverFolderId ? .( item . id ! )
}
: undefined
}
onDragLeave = {
isExternalDragging
? ( e ) => {
if ( ! e . currentTarget . contains ( e . relatedTarget as Node )) {
setExternalOverFolderId ? .( null )
}
}
: undefined
}
onDragOver = {
isExternalDragging
? ( e ) => {
e . preventDefault ()
}
: undefined
}
onDrop = {
isExternalDragging
? async ( e ) => {
e . preventDefault ()
e . stopPropagation ()
2026-06-18 15:53:18 +08:00
resetExternalDragState ? .()
2026-06-18 15:39:08 +08:00
const files = await getAllFilesFromEntries ( e . dataTransfer )
onExternalDrop ? .( files , item . id ! )
}
: undefined
}
2026-09-03 21:01:14 +08:00
data-testid = "file-card"
2026-05-19 16:05:09 +08:00
className = { cn (
2026-08-27 20:44:08 +08:00
'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' ,
2026-05-19 16:05:09 +08:00
)}
>
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" >
2026-10-16 10:42:32 +08:00
{ ! hideCheckbox && (
< div className = "absolute top-1 left-1 z-30 transition-all duration-200" >
< Checkbox
checked = { isChecked }
2026-11-07 11:18:47 +08:00
onClick = {( e ) => {
e . stopPropagation ()
onSelect ( item , e , { isCheckbox : true })
}}
className = "w-5 h-5 border-1 border-foreground/20 bg-background/50 hover:border-primary transition-colors cursor-pointer data-[state=checked]:bg-primary data-[state=checked]:border-primary"
2026-10-16 10:42:32 +08:00
/>
</ div >
)}
2026-05-19 16:05:09 +08:00
{ item . latestChildren && item . latestChildren . length > 0 && (
2026-09-19 11:36:14 +08:00
< div data-testid = "folder-preview-grid" className = "h-full w-full" >
< FolderPreviewGrid items = { item . latestChildren } />
</ div >
)}
{ daysLeft !== null && (
< span
data-testid = "folder-card-days-left"
title = { daysLeftTooltip }
className = "pointer-events-none absolute bottom-1 left-1 z-10 flex items-center gap-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
>
< Clock className = "h-3.5 w-3.5" />
< span >{ m . days_left_short ({ count : daysLeft })}</ span >
</ span >
2026-05-19 16:05:09 +08:00
)}
</ 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-08-27 10:54:35 +08:00
className = "h-auto p-0 text-sm font-medium text-foreground truncate !bg-transparent"
2026-05-19 16:05:09 +08:00
/>
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" >
2026-07-06 10:38:41 +08:00
{( item . fileCount || 0 ) === 1
? m . n_items_singular ({ count : item.fileCount || 0 })
: m . n_items_plural ({ count : item.fileCount || 0 })}
2026-05-19 16:05:09 +08:00
</ span >
2026-10-16 10:42:32 +08:00
{ ! hideActions && ! isRecents && (
2026-08-25 18:51:24 +08:00
< DropdownMenu modal = { false }>
< DropdownMenuTrigger asChild >
< button
onClick = {( e ) => e . stopPropagation ()}
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
>
2026-08-25 18:51:24 +08:00
< MoreHorizontal size = { 18 } />
</ button >
</ DropdownMenuTrigger >
< DropdownMenuContent align = "end" >
{ isShareView ? (
<>
{ allowDownload && (
2026-06-07 09:51:33 +08:00
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
2026-08-25 18:51:24 +08:00
onAction ? .( 'download' , item )
}}
>
< Download className = "mr-2 h-4 w-4" />
< span >{ m . download ()}</ span >
</ DropdownMenuItem >
)}
{ canEdit && (
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'remove-from-share' , item )
2026-06-07 09:51:33 +08:00
}}
className = "text-destructive"
>
< Trash2 className = "mr-2 h-4 w-4" />
2026-08-25 18:51:24 +08:00
< span >{ m . remove_from_share ()}</ span >
2026-06-07 09:51:33 +08:00
</ DropdownMenuItem >
2026-08-25 18:51:24 +08:00
)}
</>
) : isRecentlyDeleted ? (
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'restore' , item )
}}
>
< History className = "mr-2 h-4 w-4" />
< span >{ m . restore ()}</ span >
</ DropdownMenuItem >
) : (
<>
{ canEdit && ( ! isChecked || ( selectedCount || 0 ) <= 1 ) && (
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'rename' , item )
}}
>
< Edit className = "mr-2 h-4 w-4" />
< span >{ m . rename ()}</ span >
</ DropdownMenuItem >
)}
{ allowDownload && (
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'download' , item )
}}
>
< Download className = "mr-2 h-4 w-4" />
< span >{ m . download ()}</ span >
</ DropdownMenuItem >
)}
2026-11-05 09:57:34 +08:00
{ ! isRecentlyDeleted && ! isShareView && (
< CopyDropdownMenuSub
items = {[ item ]}
context = {{
isTeamOwner ,
allowDownload ,
}}
onCopy = {( kind , targetItems ) => onCopy ? .( kind , targetItems )}
/>
)}
2026-11-03 20:31:56 +08:00
{ canEdit && archiveStorageEnabled && (
<>
< DropdownMenuItem
data-testid = "folder-card-archive-action"
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'archive' , item )
}}
>
< Archive className = "mr-2 h-4 w-4" />
< span >{ m . archive ()}</ span >
</ DropdownMenuItem >
< DropdownMenuItem
data-testid = "folder-card-restore-archive-action"
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'restore-archive' , item )
}}
>
< ArchiveRestore className = "mr-2 h-4 w-4" />
< span >{ m . restore_from_archive ()}</ span >
</ DropdownMenuItem >
</>
)}
2026-08-25 18:51:24 +08:00
{ canEdit && (
<>
< DropdownMenuSeparator />
< DropdownMenuItem
onClick = {( e ) => {
e . stopPropagation ()
onAction ? .( 'delete' , item )
}}
className = "text-destructive"
>
< Trash2 className = "mr-2 h-4 w-4" />
< span >{ m . delete ()}</ span >
</ DropdownMenuItem >
</>
)}
</>
)}
</ DropdownMenuContent >
</ DropdownMenu >
)}
2026-05-19 16:05:09 +08:00
</ div >
</ div >
</ div >
</ div >
)
}