import Link from "next/link"; import { Listbox, Transition } from "@headlessui/react"; import { Fragment, useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { FaTrello } from "react-icons/fa"; import { HiChevronUpDown, HiMiniArrowTopRightOnSquare, HiOutlineQuestionMarkCircle, HiXMark, } from "react-icons/hi2"; import Button from "~/components/Button"; import Toggle from "~/components/Toggle"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { useWorkspace } from "~/providers/workspace"; import { api } from "~/utils/api"; const integrationProviders: Record< string, { name: string; icon: JSX.Element } > = { trello: { name: "Trello", icon: , }, }; const SelectSource = ({ handleNextStep }: { handleNextStep: () => void }) => { const { data: integrations, refetch: refetchIntegrations } = api.integration.providers.useQuery(); const { control, handleSubmit } = useForm({ defaultValues: { source: integrations?.[0]?.provider ?? "trello", }, }); const { data: trelloUrl } = api.integration.getAuthorizationUrl.useQuery( { provider: "trello" }, { enabled: !integrations?.some( (integration) => integration.provider === "trello", ), }, ); const hasIntegrations = integrations && integrations.length > 0; useEffect(() => { const handleFocus = () => { refetchIntegrations(); }; window.addEventListener("focus", handleFocus); return () => { window.removeEventListener("focus", handleFocus); }; }, [refetchIntegrations]); const onSubmit = () => { if (!hasIntegrations && trelloUrl) { window.open(trelloUrl.url, "trello_auth", "height=800,width=600"); } else { handleNextStep(); } }; return (
( {({ open }) => ( <>
{integrationProviders[field.value]?.icon} {integrationProviders[field.value]?.name} {hasIntegrations ? ( integrations.map((integration, index) => (
{ integrationProviders[integration.provider] ?.icon } { integrationProviders[integration.provider] ?.name }
)) ) : (
{integrationProviders.trello?.icon} {integrationProviders.trello?.name}
)}
)}
)} />
); }; const ImportTrello: React.FC = () => { const utils = api.useUtils(); const { closeModal } = useModal(); const { workspace } = useWorkspace(); const { showPopup } = usePopup(); const [isSelectAllEnabled, setIsSelectAllEnabled] = useState(false); const refetchBoards = () => utils.board.all.refetch(); const { data: boards, isLoading: boardsLoading } = api.import.trello.getBoards.useQuery(); const { register: registerBoards, handleSubmit: handleSubmitBoards, setValue, watch, } = useForm({ defaultValues: Object.fromEntries( boards?.map((board) => [board.id, true]) ?? [], ), }); const importBoards = api.import.trello.importBoards.useMutation({ onSuccess: async () => { showPopup({ header: "Import complete", message: "Your boards have been imported.", icon: "success", }); try { await refetchBoards(); closeModal(); } catch (e) { console.log(e); } }, onError: () => { showPopup({ header: "Import failed", message: "Please try again later, or contact customer support.", icon: "error", }); }, }); const boardWatchers = boards?.map((board) => ({ id: board.id, value: watch(board.id), })); const onSubmitBoards = (values: Record) => { const boardIds = Object.keys(values).filter((key) => values[key] === true); importBoards.mutate({ boardIds, workspacePublicId: workspace.publicId, }); }; const renderContent = () => { if (boardsLoading) { return (
); } if (!boards?.length) { return (

No boards found

); } return boards.map((board) => (
)); }; return (
{renderContent()}
{ const newState = !isSelectAllEnabled; setIsSelectAllEnabled(newState); for (const board of boards || []) { setValue(board.id, newState); } }} />
); }; export function ImportBoardsForm() { const { closeModal } = useModal(); const [step, setStep] = useState(1); return (

New import

{step === 1 && setStep(step + 1)} />} {step === 2 && }
); }