import { zodResolver } from "@hookform/resolvers/zod"; import { t } from "@lingui/core/macro"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { HiMiniArrowTopRightOnSquare } from "react-icons/hi2"; import { z } from "zod"; import Button from "~/components/Button"; import FeedbackModal from "~/components/FeedbackModal"; import Input from "~/components/Input"; import Modal from "~/components/modal"; import { NewWorkspaceForm } from "~/components/NewWorkspaceForm"; import { PageHead } from "~/components/PageHead"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; const githubTokenSchema = z.object({ token: z.string().min(1, { message: t`Token is required` }), }); type GitHubTokenFormValues = z.infer; export default function IntegrationsSettings() { const { modalContentType, isOpen } = useModal(); const { showPopup } = usePopup(); const { register, handleSubmit, formState: { isDirty, errors }, reset, } = useForm({ resolver: zodResolver(githubTokenSchema), defaultValues: { token: "", }, }); const { data: integrations, refetch: refetchIntegrations, isLoading: integrationsLoading, } = api.integration.providers.useQuery(); const { data: trelloUrl, refetch: refetchTrelloUrl } = api.integration.getAuthorizationUrl.useQuery( { provider: "trello" }, { enabled: !integrationsLoading && !integrations?.some( (integration) => integration.provider === "trello", ), refetchOnWindowFocus: true, }, ); const { data: githubStatus, refetch: refetchGithubStatus } = api.integration.getGitHubStatus.useQuery(); useEffect(() => { const handleFocus = () => { void refetchIntegrations(); void refetchGithubStatus(); }; window.addEventListener("focus", handleFocus); return () => { window.removeEventListener("focus", handleFocus); }; }, [refetchIntegrations, refetchGithubStatus]); const { mutateAsync: disconnectTrello } = api.integration.disconnect.useMutation({ onSuccess: () => { void refetchIntegrations(); void refetchTrelloUrl(); showPopup({ header: t`Trello disconnected`, message: t`Your Trello account has been disconnected.`, icon: "success", }); }, onError: () => { showPopup({ header: t`Error disconnecting Trello`, message: t`An error occurred while disconnecting your Trello account.`, icon: "error", }); }, }); const { mutateAsync: saveGithubToken, isPending: isSavingGithubToken } = api.integration.saveGitHubToken.useMutation({ onSuccess: () => { void refetchGithubStatus(); reset(); showPopup({ header: t`GitHub connected`, message: t`Your GitHub account has been connected.`, icon: "success", }); }, onError: () => { showPopup({ header: t`Error connecting GitHub`, message: t`An error occurred while connecting your GitHub account.`, icon: "error", }); }, }); const onSubmitGithubToken = (data: GitHubTokenFormValues) => { void saveGithubToken({ token: data.token }); }; const { mutateAsync: disconnectGithub } = api.integration.disconnectGitHub.useMutation({ onSuccess: () => { void refetchGithubStatus(); showPopup({ header: t`GitHub disconnected`, message: t`Your GitHub account has been disconnected.`, icon: "success", }); }, onError: () => { showPopup({ header: t`Error disconnecting GitHub`, message: t`An error occurred while disconnecting your GitHub account.`, icon: "error", }); }, }); return ( <>

{t`Trello`}

{!integrations?.some( (integration) => integration.provider === "trello", ) && trelloUrl ? ( <>

{t`Connect your Trello account to import boards.`}

) : ( integrations?.some( (integration) => integration.provider === "trello", ) && ( <>

{t`Your Trello account is connected.`}

) )}

{t`GitHub`}

{!githubStatus?.connected ? ( <>

{t`Connect your GitHub account to import projects.`}

) : ( <>

{t`Your GitHub account is connected.`}

)}
{/* Global modals */} ); }