import React, { useState } from 'react' import { format } from 'date-fns' import Markdown from 'react-markdown' import { Download, FileText, MoreHorizontal, Trash2 } from 'lucide-react' import { useMutation, useQueryClient } from '@tanstack/react-query' import { client } from '@/ui/api/client' import { m } from '@/ui/paraglide/messages.js' import { toast } from 'sonner' 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 { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/ui/components/ui/alert-dialog' import { formatSize } from '@/ui/lib/format' import type { KanbanAttachmentInfo, KanbanCommentInfo } from '@shumai/dtos' interface TaskCommentCardProps { teamId: string taskId: string comment: KanbanCommentInfo currentUserId?: string isOwnerOrAdmin?: boolean onViewAttachment?: (attachment: KanbanAttachmentInfo) => void } export function TaskCommentCard({ teamId, taskId, comment, currentUserId, isOwnerOrAdmin, onViewAttachment, }: TaskCommentCardProps) { const queryClient = useQueryClient() const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) const canDelete = isOwnerOrAdmin || comment.author.id === currentUserId const { mutate: deleteComment, isPending: isDeleting } = useMutation({ mutationFn: async () => { const res = await client.api.teams[':teamId'].kanban.tasks[':taskId'].comments[ ':commentId' ].$delete({ param: { teamId, taskId, commentId: comment.id }, }) if (!res.ok) { const err = await res.json().catch(() => ({ message: m.error() })) throw new Error((err as { message?: string }).message || m.error()) } return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'tasks', taskId, 'comments'], }) queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'task', taskId], }) queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'tasks'], }) toast.success('Comment deleted') setIsDeleteDialogOpen(false) }, onError: (err) => { toast.error(err.message) }, }) const authorInitials = (comment.author.name || 'U') .split(' ') .map((n) => n[0]) .slice(0, 2) .join('') .toUpperCase() return (
{/* Left Avatar */} {comment.author.image && ( )} {authorInitials} {/* Main Content Area */}
{/* Header: Author + Timestamp + Actions */}
{comment.author.name} {format(new Date(comment.createdAt), 'MMM d, h:mm a')}
{canDelete && (
setIsDeleteDialogOpen(true)} > {m.delete()}
)}
{/* Markdown Comment Body */} {comment.body && (
{comment.body}
)} {/* Attachments Section */} {comment.attachments && comment.attachments.length > 0 && (
{comment.attachments.map((att) => { const isImage = att.proxyType === 'image' || att.contentType?.startsWith('image/') return (
{isImage ? (
onViewAttachment?.(att)} > {att.name} e.stopPropagation()} title="Download" >
) : (

{att.name}

{formatSize(att.sizeByte)}

)}
) })}
)}
{/* Delete Confirmation Alert Dialog */} {m.delete_comment_confirm()} {m.delete_comment_description()} {m.cancel()} deleteComment()} > {m.delete()}
) }