import { useRouter } from "next/router"; import { t } from "@lingui/core/macro"; import { env } from "next-runtime-env"; import { useEffect, useRef } from "react"; import { HiMiniArrowTopRightOnSquare } from "react-icons/hi2"; import Button from "~/components/Button"; import FeedbackModal from "~/components/FeedbackModal"; import { LanguageSelector } from "~/components/LanguageSelector"; 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 { useWorkspace } from "~/providers/workspace"; import { api } from "~/utils/api"; import Avatar from "./components/Avatar"; import CreateAPIKeyForm from "./components/CreateAPIKeyForm"; import { CustomURLConfirmation } from "./components/CustomURLConfirmation"; import { DeleteAccountConfirmation } from "./components/DeleteAccountConfirmation"; import { DeleteWorkspaceConfirmation } from "./components/DeleteWorkspaceConfirmation"; import UpdateDisplayNameForm from "./components/UpdateDisplayNameForm"; import UpdateWorkspaceDescriptionForm from "./components/UpdateWorkspaceDescriptionForm"; import UpdateWorkspaceNameForm from "./components/UpdateWorkspaceNameForm"; import UpdateWorkspaceUrlForm from "./components/UpdateWorkspaceUrlForm"; export default function SettingsPage() { const { modalContentType, openModal } = useModal(); const { workspace } = useWorkspace(); const utils = api.useUtils(); const { showPopup } = usePopup(); const router = useRouter(); const workspaceUrlSectionRef = useRef(null); const scrollContainerRef = useRef(null); const { data } = api.user.getUser.useQuery(); 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, }, ); useEffect(() => { const handleFocus = () => { refetchIntegrations(); }; window.addEventListener("focus", handleFocus); return () => { window.removeEventListener("focus", handleFocus); }; }, [refetchIntegrations]); useEffect(() => { if ( router.query.edit === "workspace_url" && workspaceUrlSectionRef.current && scrollContainerRef.current ) { const element = workspaceUrlSectionRef.current; const container = scrollContainerRef.current; container.scrollTop = element.offsetTop - 40; const input = element.querySelector('input[type="text"]'); if (input instanceof HTMLInputElement) { input.focus(); } } }, [router.query.edit]); const { mutateAsync: disconnectTrello } = api.integration.disconnect.useMutation({ onSuccess: () => { refetchUser(); refetchIntegrations(); 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 refetchUser = () => utils.user.getUser.refetch(); const handleOpenBillingPortal = async () => { try { const response = await fetch("/api/stripe/create_billing_session", { method: "POST", headers: { "Content-Type": "application/json", }, }); const { url } = (await response.json()) as { url: string }; if (url) { window.location.href = url; } } catch (error) { console.error("Error creating billing session:", error); } }; return ( <>

{t`Settings`}

{t`Profile picture`}

{t`Display name`}

{t`Workspace name`}

{t`Workspace URL`}

{t`Workspace description`}

{t`Language`}

{t`Change the language of the app.`}

{env("NEXT_PUBLIC_KAN_ENV") === "cloud" && (

{t`Billing`}

{t`View and manage your billing and subscription.`}

)}

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`API keys`}

{t`View and manage your API keys.`}

{t`Delete workspace`}

{t`Once you delete your workspace, there is no going back. This action cannot be undone.`}

{t`Delete account`}

{t`Once you delete your account, there is no going back. This action cannot be undone.`}

{modalContentType === "NEW_FEEDBACK" && } {modalContentType === "NEW_WORKSPACE" && } {modalContentType === "DELETE_WORKSPACE" && ( )} {modalContentType === "UPDATE_WORKSPACE_URL" && ( )} {modalContentType === "DELETE_ACCOUNT" && ( )}
); }