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 { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog' import { formatSize } from '@/ui/lib/format' import { isImageFileName } from '@/ui/lib/media' import type { KanbanCommentInfo } from '@shumai/dtos' interface TaskCommentCardProps { teamId: string taskId: string comment: KanbanCommentInfo currentUserId?: string isOwnerOrAdmin?: boolean } export function TaskCommentCard({ teamId, taskId, comment, currentUserId, isOwnerOrAdmin, }: TaskCommentCardProps) { const queryClient = useQueryClient() const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) const [previewImage, setPreviewImage] = useState<{ url: string; name: string } | null>(null) 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 = isImageFileName(att.name || att.url) return (
{ if (isImage) { setPreviewImage({ url: att.url, name: att.name }) } else { window.open(att.url, '_blank', 'noreferrer') } }} > {isImage ? (
{att.name}
) : ( )}

{att.name}

{att.sizeByte !== undefined && att.sizeByte > 0 && (

{formatSize(att.sizeByte)}

)}
e.stopPropagation()} title={m.download()} >
) })}
)}
{/* Delete Confirmation Alert Dialog */} {m.delete_comment_confirm()} {m.delete_comment_description()} {m.cancel()} deleteComment()} > {m.delete()} {/* Image Preview Dialog */} !open && setPreviewImage(null)}> e.stopPropagation()} > {previewImage?.name || 'Image Preview'} {previewImage && (
{previewImage.name}

{previewImage.name}

)}
) }