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 (
{att.name}
{att.sizeByte !== undefined && att.sizeByte > 0 && ({formatSize(att.sizeByte)}
)}