import { useDraggable, useDroppable, useDragOperation } from '@dnd-kit/react' import { cn } from '@/ui/lib/utils' import { m } from '@/ui/paraglide/messages.js' import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar' import { Button } from '@/ui/components/ui/button' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/ui/components/ui/dropdown-menu' import { KanbanTaskStatus, type KanbanTaskInfo } from '@shumai/dtos' import { getPriorityBadgeColor, getPriorityLabel } from './kanban-types' import { Bot, Calendar, Link2, MessageSquare, Files, AlertTriangle, Target, Sparkles, MoreHorizontal, Trash2, } from 'lucide-react' import { format, isPast } from 'date-fns' interface KanbanCardProps { task: KanbanTaskInfo onClick: (task: KanbanTaskInfo) => void onDelete?: (task: KanbanTaskInfo) => void disabled?: boolean isFirst?: boolean isLast?: boolean showBottomIndicator?: boolean currentUserId?: string currentUserRole?: string } export function KanbanCard({ task, onClick, onDelete, disabled, isFirst, isLast, showBottomIndicator, currentUserId, currentUserRole, }: KanbanCardProps) { const { source } = useDragOperation() const isDraggingAny = !!source const isOwner = currentUserRole?.toLowerCase() === 'owner' const isEditor = currentUserRole?.toLowerCase() === 'editor' const isCreator = !!currentUserId && task.creator?.id === currentUserId const isReporter = task.reporter?.id === currentUserId || isCreator const isAssignee = task.assignee?.id === currentUserId const canChangeStatus = isOwner || isEditor || isReporter || isAssignee const canDelete = isCreator || isOwner || isReporter const isDragDisabled = disabled || !canChangeStatus const { ref: setDraggableRef, isDragging } = useDraggable({ id: task.id, data: { type: 'kanban_task', task, }, disabled: isDragDisabled, }) const { ref: setTopDroppableRef, isDropTarget: isTopOver } = useDroppable({ id: `reorder-before-${task.id}`, data: { type: 'reorder', task, position: 'before', }, disabled: isDragging || disabled, }) const { ref: setBottomDroppableRef, isDropTarget: isBottomOver } = useDroppable({ id: `reorder-after-${task.id}`, data: { type: 'reorder', task, position: 'after', }, disabled: isDragging || disabled, }) const isAgentic = task.isAgentTask const isBlocked = task.status === KanbanTaskStatus.BLOCKED const isOverdue = task.dueDate ? isPast(new Date(task.dueDate)) && task.status !== KanbanTaskStatus.DONE : false return (
{/* Top 50% Droppable Zone (reorder before this card) */}
{/* Bottom 50% Droppable Zone (reorder after this card) */}
{/* Top Reorder Drop Indicator Line */} {isTopOver && !isDragging && (
)} {/* Bottom Reorder Drop Indicator Line */} {(isBottomOver || showBottomIndicator) && !isDragging && (
)} {/* Task Card Body */}
onClick(task)} className={cn( 'group relative flex flex-col gap-2 p-3 rounded-lg border text-card-foreground transition-all duration-200 cursor-pointer select-none', isDragging && 'opacity-40 scale-95 shadow-lg ring-2 ring-primary', !isDragging && 'hover:shadow-md', 'border-border/80 bg-card hover:border-primary/50 shadow-2xs', // Blocked warning state isBlocked && 'border-red-500/60 bg-red-500/[0.03] ring-1 ring-red-500/20', )} > {/* Top Header Row: Goal Tag & Badges */}
{/* Priority Badge */} {getPriorityLabel(task.priority)} {/* Task Type Badge (Agent Task only) */} {isAgentic && ( {m.task_type_agent()} )} {/* Goal Tag */} {task.goal && ( {task.goal.title} )}
{/* Three Dot Action Menu (for Task Creator & Team Owner) */} {canDelete && (
e.stopPropagation()} > onDelete?.(task)} > {m.delete()}
)}
{/* Task Title */}

{task.title}

{/* Blocked Reason Banner */} {isBlocked && (
{task.latestStatusEvent?.blockReason || task.latestStatusEvent?.reason || task.latestStatusEvent?.summary || m.status_blocked()}
)} {/* Review Feedback Banner */} {task.status === KanbanTaskStatus.IN_REVIEW && task.latestStatusEvent?.summary && (
{task.latestStatusEvent.summary}
)} {/* Card Footer: Assignee & Metadata Badges */}
{/* Assignee */}
{task.assignee ? ( <> {isAgentic ? (
) : ( {task.assignee.name.slice(0, 2).toUpperCase()} )} {task.assignee.name} ) : ( {m.unassigned()} )}
{/* Right side stats: Due Date, Dependencies, Comments */}
{/* Due Date */} {task.dueDate && (
{format(new Date(task.dueDate), 'MM/dd')}
)} {/* Linked assets count */} {task.assetCount > 0 && (
{task.assetCount}
)} {/* Dependencies count */} {task.dependencyCount > 0 && (
{task.dependencyCount}
)} {/* Comments count */} {task.commentCount > 0 && (
{task.commentCount}
)}
) }