"use client"; import { useCallback, useEffect, useState } from "react"; import { IconPlug } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@kandev/ui/dialog"; import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@kandev/ui/drawer"; import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint"; import type { TaskGitCredentialsState } from "@/hooks/domains/github/use-task-git-credentials"; import type { GitHubStatus } from "@/lib/types/github"; import type { GitHubAutomationMethod } from "./github-auth-method-list"; import { GitHubConnectionSettingsForm } from "./github-connection-settings-form"; import { useTranslation } from "react-i18next"; function methodForStatus(status: GitHubStatus): GitHubAutomationMethod { if (status.automation?.source === "github_app_installation") return "app"; if (status.automation?.source === "gh_cli") return "cli"; return "pat"; } export function GitHubConnectionDialog({ status, workspaceId, onSaved, taskAccess, }: { status: GitHubStatus; workspaceId: string; onSaved: () => void; taskAccess: TaskGitCredentialsState; }) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [method, setMethod] = useState(() => methodForStatus(status)); const { isMobile } = useResponsiveBreakpoint(); const connected = Boolean(status.automation); useEffect(() => { setMethod(methodForStatus(status)); }, [status]); useEffect(() => { setOpen(false); }, [workspaceId]); const openChange = useCallback( (next: boolean) => { if (next) setMethod(methodForStatus(status)); setOpen(next); }, [status], ); const trigger = ( ); const body = ( setOpen(false)} taskAccess={taskAccess} isMobile={isMobile} /> ); if (isMobile) { return ( {trigger} {connected ? t("github:changeGithubConnection") : t("github:connectGithub")} {t("github:thisWorkspaceUsesOneCredentialFor")} {body} ); } return ( {trigger} {connected ? t("github:changeGithubConnection") : t("github:connectGithub")} {t("github:thisWorkspaceUsesOneCredentialFor")} {body} ); }