import { t } from "@lingui/core/macro"; import { useForm } from "react-hook-form"; import { HiOutlineArrowUp } from "react-icons/hi2"; import type { WorkspaceMember } from "~/components/Editor"; import Editor from "~/components/Editor"; import LoadingSpinner from "~/components/LoadingSpinner"; import { Tooltip } from "~/components/Tooltip"; import { usePermissions } from "~/hooks/usePermissions"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; import { invalidateCard } from "~/utils/cardInvalidation"; interface FormValues { comment: string; } const NewCommentForm = ({ cardPublicId, workspaceMembers, }: { cardPublicId: string; workspaceMembers: WorkspaceMember[]; }) => { const utils = api.useUtils(); const { showPopup } = usePopup(); const { canCreateComment } = usePermissions(); const { handleSubmit, setValue, watch, reset } = useForm({ values: { comment: "", }, }); const addCommentMutation = api.card.addComment.useMutation({ onError: (_error, _newList) => { showPopup({ header: t`Unable to add comment`, message: t`Please try again later, or contact customer support.`, icon: "error", }); }, onSettled: async () => { reset(); await invalidateCard(utils, cardPublicId); }, }); const onSubmit = (data: FormValues) => { addCommentMutation.mutate({ cardPublicId, comment: data.comment, }); }; const isMac = typeof navigator !== "undefined" && navigator.userAgent.includes("Mac"); const submitTooltip = (
{t`Submit`} {isMac ? "⌘" : "Ctrl"} Enter
); if (!canCreateComment) { return null; } return (
setValue("comment", value)} onSubmit={handleSubmit(onSubmit)} workspaceMembers={workspaceMembers} enableYouTubeEmbed={false} placeholder={t`Add comment... (type '/' to open commands or '@' to mention)`} disableHeadings={true} />
); }; export default NewCommentForm;