import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { HiOutlineBarsArrowDown, HiOutlineBarsArrowUp, HiXMark, } from "react-icons/hi2"; import type { NewCardInput } from "@kan/api/types"; import Avatar from "~/components/Avatar"; import Button from "~/components/Button"; import CheckboxDropdown from "~/components/CheckboxDropdown"; import Input from "~/components/Input"; import LabelIcon from "~/components/LabelIcon"; import Toggle from "~/components/Toggle"; import { useBoard } from "~/providers/board"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; import { formatMemberDisplayName } from "~/utils/helpers"; import { getPublicUrl } from "~/utils/supabase/getPublicUrl"; type NewCardFormInput = NewCardInput & { isCreateAnotherEnabled: boolean; }; interface NewCardFormProps { listPublicId: string; } export function NewCardForm({ listPublicId }: NewCardFormProps) { const { boardData, addCard, refetchBoard } = useBoard(); const { showPopup } = usePopup(); const { closeModal } = useModal(); const { register, handleSubmit, reset, setValue, watch } = useForm({ defaultValues: { title: "", description: "", listPublicId, labelPublicIds: [], memberPublicIds: [], isCreateAnotherEnabled: false, position: "start", }, }); const labelPublicIds = watch("labelPublicIds") || []; const memberPublicIds = watch("memberPublicIds") || []; const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); const position = watch("position"); const createCard = api.card.create.useMutation({ onSuccess: async () => { await refetchBoard(); }, onError: async () => { closeModal(); await refetchBoard(); showPopup({ header: "Unable to create card", message: "Please try again later, or contact customer support.", icon: "error", }); }, }); useEffect(() => { const titleElement: HTMLElement | null = document.querySelector("#title"); if (titleElement) titleElement.focus(); }, []); const formattedLabels = boardData?.labels.map((label) => ({ key: label.publicId, value: label.name, leftIcon: , })) ?? []; const formattedLists = boardData?.lists.map((list) => ({ key: list.publicId, value: list.name, selected: list.publicId === watch("listPublicId"), })) ?? []; const formattedMembers = boardData?.workspace?.members.map((member) => ({ key: member.publicId, value: formatMemberDisplayName( member.user?.name ?? null, member.user?.email ?? null, ), leftIcon: ( ), })) ?? []; const onSubmit = (data: NewCardInput) => { addCard(data); const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); if (!isCreateAnotherEnabled) closeModal(); reset({ title: "", description: "", listPublicId: watch("listPublicId"), labelPublicIds: [], memberPublicIds: [], isCreateAnotherEnabled, position, }); createCard.mutate({ title: data.title, description: data.description, listPublicId: data.listPublicId, labelPublicIds: data.labelPublicIds, memberPublicIds: data.memberPublicIds, position: data.position, }); }; const handleToggleCreateAnother = (): void => { setValue("isCreateAnotherEnabled", !isCreateAnotherEnabled); }; const handleSelectList = (listPublicId: string): void => { setValue("listPublicId", listPublicId); }; const handleSelectMembers = (memberPublicId: string): void => { const currentIndex = memberPublicIds.indexOf(memberPublicId); if (currentIndex === -1) { setValue("memberPublicIds", [...memberPublicIds, memberPublicId]); } else { const newMemberPublicIds = [...memberPublicIds]; newMemberPublicIds.splice(currentIndex, 1); setValue("memberPublicIds", newMemberPublicIds); } }; const handleSelectLabels = (labelPublicId: string): void => { const currentIndex = labelPublicIds.indexOf(labelPublicId); if (currentIndex === -1) { setValue("labelPublicIds", [...labelPublicIds, labelPublicId]); } else { const newLabelPublicIds = [...labelPublicIds]; newLabelPublicIds.splice(currentIndex, 1); setValue("labelPublicIds", newLabelPublicIds); } }; const selectedList = formattedLists.find((item) => item.selected); return (

New card

setValue("description", e.target.value)} value={watch("description")} contentEditable />
handleSelectList(item.key)} >
{selectedList?.value}
handleSelectMembers(item.key)} >
{!memberPublicIds.length ? ( "Members" ) : (
{memberPublicIds.map((memberPublicId) => { const member = formattedMembers.find( (member) => member.key === memberPublicId, ); return ( {member?.value .split(" ") .map((namePart) => namePart.charAt(0).toUpperCase(), ) .join("")} ); })}
)}
handleSelectLabels(item.key)} >
{!labelPublicIds.length ? ( "Labels" ) : ( <>
1 ? "flex -space-x-[2px] overflow-hidden" : "flex items-center" } > {labelPublicIds.map((labelPublicId) => { const label = boardData?.labels.find( (label) => label.publicId === labelPublicId, ); return ( <> {labelPublicIds.length === 1 && (
{label?.name}
)} ); })}
{labelPublicIds.length > 1 && (
{`${labelPublicIds.length} labels`}
)} )}
); }