import { t } from "@lingui/core/macro"; import { formatDistanceToNow } from "date-fns"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { HiEllipsisHorizontal, HiPencil, HiTrash } from "react-icons/hi2"; import Avatar from "~/components/Avatar"; import Button from "~/components/Button"; import Editor from "~/components/Editor"; import type { WorkspaceMember } from "~/components/Editor"; import Dropdown from "~/components/Dropdown"; import { usePermissions } from "~/hooks/usePermissions"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; import { invalidateCard } from "~/utils/cardInvalidation"; import { getAvatarUrl } from "~/utils/helpers"; interface FormValues { comment: string; } const Comment = ({ publicId, cardPublicId, name, email, image, isLoading, createdAt, comment, isAuthor, isEdited = false, isViewOnly = false, }: { publicId: string | undefined; cardPublicId: string; name: string; email: string; image: string | null; isLoading: boolean; createdAt: string; comment: string | undefined; isAuthor: boolean; isEdited: boolean; isViewOnly: boolean; }) => { const [isEditing, setIsEditing] = useState(false); const utils = api.useUtils(); const { showPopup } = usePopup(); const { openModal } = useModal(); const { canEditComment, canDeleteComment } = usePermissions(); const { handleSubmit, setValue, watch } = useForm({ defaultValues: { comment, }, }); const { data: cardData } = api.card.byId.useQuery( { cardPublicId, }, { enabled: !!cardPublicId && cardPublicId.length >= 12, }, ); const workspaceMembers: WorkspaceMember[] = cardData?.list.board.workspace.members .filter((member) => member.email) .map((member) => ({ publicId: member.publicId, email: member.email, user: member.user ? { id: member.user.id, name: member.user.name ?? null, image: member.user.image ?? null, } : null, })) ?? []; if (!publicId) return null; const updateCommentMutation = api.card.updateComment.useMutation({ onSuccess: async () => { await invalidateCard(utils, cardPublicId); setIsEditing(false); }, onError: () => { showPopup({ header: t`Unable to update comment`, message: t`Please try again later, or contact customer support.`, icon: "error", }); }, }); const onSubmit = (data: FormValues) => { updateCommentMutation.mutate({ cardPublicId, comment: data.comment, commentPublicId: publicId, }); }; const dropdownItems = [ ...(isAuthor && canEditComment ? [ { label: t`Edit comment`, action: () => setIsEditing(true), icon: , }, ] : []), ...((isAuthor || canDeleteComment) ? [ { label: t`Delete comment`, action: () => openModal("DELETE_COMMENT", publicId), icon: , }, ] : []), ]; return (

{`${name} `} ยท {formatDistanceToNow(new Date(createdAt), { addSuffix: true, })} {isEdited && ( {t` (edited)`} )}

{dropdownItems.length > 0 && !isViewOnly && (
)}
{!isEditing ? (
) : (
setValue("comment", value)} workspaceMembers={workspaceMembers} enableYouTubeEmbed={false} placeholder={t`Add comment... (type '/' to open commands or '@' to mention)`} disableHeadings={true} />
)}
); }; export default Comment;