"use client"; import { useState, useRef, useCallback, useEffect } from "react"; import { Trash2, Loader2 } from "lucide-react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { useTRPC } from "@/trpc/client"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Separator } from "@/components/ui/separator"; import { Sheet, SheetContent, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; import { getAvatarUrl } from "@/lib/avatar-url"; import { useTranslations } from "next-intl"; interface CommentSheetProps { postId: string; petId: string; // active pet (commenter) open: boolean; onOpenChange: (open: boolean) => void; } export function CommentSheet({ postId, petId, open, onOpenChange }: CommentSheetProps) { const t = useTranslations("Comments"); const trpc = useTRPC(); function formatRelativeTime(date: Date | string): string { const diffMs = Date.now() - new Date(date).getTime(); const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24); if (diffMins < 1) return t("justNow"); if (diffMins < 60) return `${diffMins}m`; if (diffHours < 24) return `${diffHours}h`; if (diffDays < 7) return `${diffDays}d`; return new Date(date).toLocaleDateString(); } const queryClient = useQueryClient(); const [body, setBody] = useState(""); const textareaRef = useRef(null); const { data, isLoading } = useQuery( trpc.comments.listByPost.queryOptions({ postId, limit: 50 }) ); const comments = data?.comments ?? []; const createMutation = useMutation( trpc.comments.create.mutationOptions({ onSuccess: () => { setBody(""); void queryClient.invalidateQueries({ queryKey: [["trpc", "comments", "listByPost"]], }); void queryClient.invalidateQueries({ queryKey: [["trpc", "feed", "getFeed"]], }); }, onError: () => toast.error(t("postError")), }) ); const deleteMutation = useMutation( trpc.comments.delete.mutationOptions({ onSuccess: () => { void queryClient.invalidateQueries({ queryKey: [["trpc", "comments", "listByPost"]], }); void queryClient.invalidateQueries({ queryKey: [["trpc", "feed", "getFeed"]], }); }, onError: () => toast.error(t("deleteError")), }) ); const handleSubmit = useCallback(() => { const trimmed = body.trim(); if (!trimmed || createMutation.isPending) return; createMutation.mutate({ postId, petId, body: trimmed }); }, [body, postId, petId, createMutation]); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); handleSubmit(); } }, [handleSubmit] ); // Auto-focus textarea when sheet opens useEffect(() => { if (open) { setTimeout(() => textareaRef.current?.focus(), 100); } }, [open]); return ( {t("title")} {isLoading ? (
) : comments.length === 0 ? (

{t("empty")}

) : (
{comments.map((comment) => { const isOwn = comment.pet.id === petId; const avatarUrl = getAvatarUrl(comment.pet.avatarKey); return (
{avatarUrl && ( {comment.pet.name} )} {comment.pet.name[0]?.toUpperCase() ?? "?"}
{comment.pet.name} {formatRelativeTime(comment.createdAt)}

{comment.body}

{isOwn && ( )}
); })}
)}
{/* Comment input — sticky at bottom */}