import type { ReactNode } from "react"; import { t } from "@lingui/core/macro"; import { Draggable } from "react-beautiful-dnd"; import { useForm } from "react-hook-form"; import { HiEllipsisHorizontal, HiOutlinePlusSmall, HiOutlineSquaresPlus, HiOutlineTrash, } from "react-icons/hi2"; import { authClient } from "@kan/auth/client"; import Dropdown from "~/components/Dropdown"; import { Tooltip } from "~/components/Tooltip"; import { usePermissions } from "~/hooks/usePermissions"; import { useModal } from "~/providers/modal"; import { api } from "~/utils/api"; interface ListProps { children: ReactNode; index: number; list: List; setSelectedPublicListId: (publicListId: PublicListId) => void; } interface List { publicId: string; name: string; createdBy?: string | null; } interface FormValues { listPublicId: string; name: string; } type PublicListId = string; export default function List({ children, index, list, setSelectedPublicListId, }: ListProps) { const { openModal } = useModal(); const { canCreateCard, canEditList, canDeleteList } = usePermissions(); const { data: session } = authClient.useSession(); const isCreator = list.createdBy && session?.user.id === list.createdBy; const canEdit = canEditList || isCreator; const canDrag = canEditList || isCreator; const openNewCardForm = (publicListId: PublicListId) => { if (!canCreateCard) return; openModal("NEW_CARD"); setSelectedPublicListId(publicListId); }; const updateList = api.list.update.useMutation(); const { register, handleSubmit } = useForm({ defaultValues: { listPublicId: list.publicId, name: list.name, }, values: { listPublicId: list.publicId, name: list.name, }, }); const onSubmit = (values: FormValues) => { if (!canEdit) return; updateList.mutate({ listPublicId: values.listPublicId, name: values.name, }); }; const handleOpenDeleteListConfirmation = () => { setSelectedPublicListId(list.publicId); openModal("DELETE_LIST"); }; return ( {(provided) => (
{(() => { const dropdownItems = [ ...(canCreateCard ? [ { label: t`Add a card`, action: () => openNewCardForm(list.publicId), icon: ( ), }, ] : []), ...(canDeleteList || isCreator ? [ { label: t`Delete list`, action: handleOpenDeleteListConfirmation, icon: ( ), }, ] : []), ]; if (dropdownItems.length === 0) { return null; } return (
); })()}
{children}
)}
); }