Files
shumai/packages/webui/components/kanban/task-related-assets.tsx
T
4b1b64fe2b feat(kanban): link related assets to their file or folder (#453)
Related assets in the task edit dialog were plain rows with no way to
open them. Add an opt-in `linkable` prop to TaskRelatedAssets that turns
each row's thumbnail and name into a router Link to the asset in its
project (files -> /projects/$projectId/files/$fileId, folders ->
/projects/$projectId/folders/$folderId), falling back to the task's
project when the asset has none. The edit dialog opts in; the create
dialog does not, so a click cannot discard an unsaved draft.

Co-authored-by: Frederic De Decker <hello@trinity.video>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 20:14:36 +08:00

186 lines
6.8 KiB
TypeScript

import { useState } from 'react'
import { Link } from '@tanstack/react-router'
import { Button } from '@/ui/components/ui/button'
import { Separator } from '@/ui/components/ui/separator'
import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar'
import { m } from '@/ui/paraglide/messages.js'
import type { KanbanTaskAssetInfo } from '@shumai/dtos'
import { AssetPickerDialog } from './asset-picker-dialog'
import { Plus, X, Folder, File, Files } from 'lucide-react'
import { cn } from '@/ui/lib/utils'
interface TaskRelatedAssetsProps {
teamId: string
projectId?: string | null
assets: KanbanTaskAssetInfo[]
onAddAssets: (newAssets: KanbanTaskAssetInfo[]) => void
onRemoveAsset: (assetId: string) => void
disabled?: boolean
/** Make each row a link to the asset in its project. Off by default so unsaved drafts are not discarded. */
linkable?: boolean
}
export function TaskRelatedAssets({
teamId,
projectId,
assets,
onAddAssets,
onRemoveAsset,
disabled = false,
linkable = false,
}: TaskRelatedAssetsProps) {
const [isPickerOpen, setIsPickerOpen] = useState(false)
const handleConfirmPicker = (selectedAssets: KanbanTaskAssetInfo[]) => {
// Filter out already linked assets
const existingIds = new Set(assets.map((a) => a.id))
const toAdd = selectedAssets.filter((a) => !existingIds.has(a.id))
if (toAdd.length > 0) {
onAddAssets(toAdd)
}
}
return (
<div className="space-y-2">
{/* Title + Plus Button */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-1.5 text-xs font-semibold text-foreground">
<Files className="w-3.5 h-3.5 text-muted-foreground" />
<span>{m.related_assets()}</span>
{assets.length > 0 && (
<span className="text-[11px] text-muted-foreground font-normal">({assets.length})</span>
)}
</div>
{!disabled && (
<Button
type="button"
variant="ghost"
size="sm"
className="h-6 w-6 p-0 hover:bg-muted"
onClick={() => setIsPickerOpen(true)}
title={m.add_assets()}
>
<Plus className="w-3.5 h-3.5" />
</Button>
)}
</div>
<Separator />
{/* Assets List Rows */}
{assets.length === 0 ? (
<p className="text-xs text-muted-foreground/70 py-1 italic">{m.no_related_assets()}</p>
) : (
<div className="space-y-1.5 max-h-48 overflow-y-auto pr-1">
{assets.map((asset) => {
const isFolder = asset.type === 'folder'
const assetProjectId = asset.projectId || projectId
const assetHref =
linkable && assetProjectId
? `/projects/${assetProjectId}/${isFolder ? 'folders' : 'files'}/${asset.id}`
: null
const body = (
<>
{/* Thumbnail / Icon */}
<div className="w-7 h-7 rounded bg-muted border shrink-0 flex items-center justify-center overflow-hidden">
{asset.thumbnailUrl ? (
<img
src={asset.thumbnailUrl}
alt={asset.name}
className="w-full h-full object-cover"
/>
) : isFolder ? (
<Folder className="w-4 h-4 text-primary fill-primary/20" />
) : (
<File className="w-4 h-4 text-muted-foreground" />
)}
</div>
{/* Name & Path */}
<div className="flex-1 min-w-0">
<p
className={cn(
'font-medium truncate text-foreground leading-snug',
assetHref && 'group-hover:underline group-hover:text-primary',
)}
>
{asset.name}
</p>
<p className="text-[10px] text-muted-foreground truncate leading-none mt-0.5">
{asset.path || '/'}
</p>
</div>
</>
)
return (
<div
key={asset.id}
className="flex items-center justify-between gap-2 p-1.5 px-2 rounded-md bg-muted/40 hover:bg-muted/70 transition-colors text-xs border border-border/40"
>
{/* Left: Thumbnail & Names (links to the asset when linkable) */}
{assetHref ? (
<Link
to={assetHref}
title={asset.name}
className="group flex items-center gap-2.5 min-w-0 flex-1 rounded-sm cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
{body}
</Link>
) : (
<div className="flex items-center gap-2.5 min-w-0 flex-1">{body}</div>
)}
{/* Right: Creator & Remove Button */}
<div className="flex items-center gap-2 shrink-0">
{asset.creator && (
<div
className="flex items-center gap-1 text-[11px] text-muted-foreground"
title={asset.creator.name}
>
<Avatar className="w-4 h-4 text-[9px]">
<AvatarImage src={asset.creator.image || undefined} />
<AvatarFallback>
{asset.creator.name.slice(0, 1).toUpperCase()}
</AvatarFallback>
</Avatar>
<span className="hidden sm:inline text-[11px] max-w-[80px] truncate">
{asset.creator.name}
</span>
</div>
)}
{!disabled && (
<Button
type="button"
variant="ghost"
size="sm"
className="h-5 w-5 p-0 text-muted-foreground hover:text-destructive hover:bg-destructive/10"
onClick={() => onRemoveAsset(asset.id)}
title={m.unlink_asset()}
>
<X className="w-3 h-3" />
</Button>
)}
</div>
</div>
)
})}
</div>
)}
{/* Asset Picker Dialog */}
{isPickerOpen && (
<AssetPickerDialog
teamId={teamId}
initialProjectId={projectId}
excludeAssetIds={assets.map((a) => a.id)}
isOpen={isPickerOpen}
onClose={() => setIsPickerOpen(false)}
onConfirm={handleConfirmPicker}
/>
)}
</div>
)
}