mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,341 @@
|
||||
import type { AssetInfo, ChildPreview } from '@/dtos/asset'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/ui/components/ui/dropdown-menu'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useDraggable, useDroppable } from '@dnd-kit/react'
|
||||
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'
|
||||
import { Checkbox } from '@/ui/components/ui/checkbox'
|
||||
import { EditableText } from '@/ui/components/ui/editable-text'
|
||||
|
||||
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', item: AssetInfo) => void
|
||||
isRecentlyDeleted?: boolean
|
||||
selectedCount?: number
|
||||
}
|
||||
|
||||
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) {
|
||||
return (
|
||||
<div className="w-full h-full bg-zinc-200 dark:bg-zinc-800/50 rounded-sm">
|
||||
{renderItem(safeChildren[0])}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// 2 Items
|
||||
if (count === 2) {
|
||||
return (
|
||||
<div className="grid grid-cols-2 gap-0.5 h-full w-full">
|
||||
<div className="overflow-hidden h-full bg-zinc-200 dark:bg-zinc-800/50 rounded-sm">
|
||||
{renderItem(safeChildren[0])}
|
||||
</div>
|
||||
<div className="overflow-hidden h-full bg-zinc-200 dark:bg-zinc-800/50 rounded-sm">
|
||||
{renderItem(safeChildren[1])}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// 3 Items
|
||||
return (
|
||||
<div className="grid grid-cols-3 gap-0.5 h-full w-full">
|
||||
<div className="col-span-1 overflow-hidden h-full relative bg-zinc-200 dark:bg-zinc-800/50 rounded-sm col-span-2">
|
||||
<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">
|
||||
<div className="overflow-hidden h-full relative bg-zinc-200 dark:bg-zinc-800/50 rounded-sm">
|
||||
<div className="absolute inset-0 w-full h-full">{renderItem(safeChildren[1])}</div>
|
||||
</div>
|
||||
<div className="overflow-hidden h-full relative bg-zinc-200 dark:bg-zinc-800/50 rounded-sm">
|
||||
<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,
|
||||
}: 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,
|
||||
tabRadius: 3,
|
||||
tabSlope: 10,
|
||||
tabBaseWidth: 150,
|
||||
}
|
||||
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 + tr} ${s + tr} 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 (
|
||||
<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',
|
||||
)}
|
||||
>
|
||||
<div className="absolute inset-0 -z-10 drop-shadow-sm 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"
|
||||
strokeWidth={showDropFeedback ? 3 : 2}
|
||||
className={cn(
|
||||
'transition-colors duration-200 fill-foreground/10 dark:fill-slate-800 dark:stroke-slate-600 group-hover:stroke-indigo-400 dark:group-hover:stroke-indigo-400',
|
||||
(isSelected || isChecked || showDropFeedback) &&
|
||||
'stroke-indigo-500 dark:stroke-indigo-400',
|
||||
)}
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
<div className="relative w-[calc(100%-0.2rem)] mb-[0.1rem] flex flex-1 flex-col p-2 mt-4 bg-secondary rounded-sm">
|
||||
<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
|
||||
className="w-5 h-5 border-1 border-black/30 bg-black/5 data-[state=checked]:bg-indigo-600 data-[state=checked]:border-indigo-600"
|
||||
/>
|
||||
</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}
|
||||
className="font-semibold text-slate-800 dark:text-slate-100 text-lg leading-tight truncate h-auto p-0"
|
||||
title={name}
|
||||
/>
|
||||
|
||||
<div className="flex items-center justify-between text-slate-500 dark:text-slate-400">
|
||||
<span className="text-xs tracking-wide opacity-80">
|
||||
{item.fileCount || 0} {(item.fileCount || 0) === 1 ? 'Item' : 'Items'}
|
||||
</span>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className="p-1.5 -mr-2 rounded-full hover:bg-slate-200/50 dark:hover:bg-slate-700/50 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 transition-colors cursor-pointer"
|
||||
>
|
||||
<MoreHorizontal size={18} />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
{isRecentlyDeleted ? (
|
||||
<DropdownMenuItem
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onAction?.('restore', item)
|
||||
}}
|
||||
>
|
||||
<History className="mr-2 h-4 w-4" />
|
||||
<span>Restore</span>
|
||||
</DropdownMenuItem>
|
||||
) : (
|
||||
<>
|
||||
{(!isChecked || (selectedCount || 0) <= 1) && (
|
||||
<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>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onAction?.('delete', item)
|
||||
}}
|
||||
className="text-destructive"
|
||||
>
|
||||
<Trash2 className="mr-2 h-4 w-4" />
|
||||
<span>Delete</span>
|
||||
</DropdownMenuItem>
|
||||
</>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user