import { t } from "@lingui/core/macro"; import { formatDistanceToNow } from "date-fns"; import { useState } from "react"; import ContentEditable from "react-contenteditable"; 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 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, isAdmin, 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; isAdmin: 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, }, }); 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", e.target.value)} className="block w-full max-w-[800px] border-0 bg-transparent py-1.5 text-sm text-light-900 focus-visible:outline-none dark:text-dark-1000 sm:text-sm sm:leading-6" />
)}
); }; export default Comment;