"use client"; import { useCallback, useEffect, useState } from "react"; import { IconGitBranch, IconRefresh, IconArrowUp, IconArrowDown, IconCheck, IconAlertTriangle, } from "@tabler/icons-react"; import { Input } from "@kandev/ui/input"; import { Button } from "@kandev/ui/button"; import { Badge } from "@kandev/ui/badge"; import { toast } from "@/lib/toast/sonner"; import { useAppStore } from "@/components/state-provider"; import * as officeApi from "@/lib/api/domains/office-api"; import type { GitStatusData } from "@/lib/api/domains/office-api"; import { useOfficeConfigSyncActive } from "@/hooks/domains/office/use-office-config-sync-active"; import { useTranslation } from "react-i18next"; function useGitOperations(activeWorkspaceId: string) { const { t } = useTranslation(); const [gitStatus, setGitStatus] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const fetchStatus = useCallback(async () => { if (!activeWorkspaceId) return; try { const status = await officeApi.getGitStatus(activeWorkspaceId); setGitStatus(status); setError(null); } catch { setGitStatus(null); } }, [activeWorkspaceId]); useEffect(() => { fetchStatus(); }, [fetchStatus]); // Catalog KEYS, not messages: `failureKey` used to be a verb ("Clone") dropped // into a `${verb} failed` frame, which no language can reorder or inflect. // Each operation now carries a whole failure sentence of its own. const runOp = useCallback( async (op: () => Promise, successKey: string, failureKey: string) => { if (!activeWorkspaceId) return; setLoading(true); setError(null); try { await op(); toast.success(t(successKey)); await fetchStatus(); } catch (err) { const msg = err instanceof Error ? err.message : t(failureKey); setError(msg); toast.error(msg); } finally { setLoading(false); } }, [activeWorkspaceId, fetchStatus, t], ); return { gitStatus, loading, error, fetchStatus, runOp }; } export function GitSection() { const activeWorkspaceId = useAppStore((s) => s.workspaces?.activeId ?? ""); const [repoUrl, setRepoUrl] = useState(""); const [branch, setBranch] = useState("main"); const [commitMessage, setCommitMessage] = useState(""); const { gitStatus, loading, error, fetchStatus, runOp } = useGitOperations(activeWorkspaceId); // AC-OFFICE-CONFIG-SYNC-006.6: clone/pull are refused server-side while // config sync owns this workspace's configuration; push stays available // since it is never refused (AC-OFFICE-CONFIG-SYNC-005.5). const configSyncActive = useOfficeConfigSyncActive(activeWorkspaceId); const handleClone = useCallback( () => runOp( async () => { await officeApi.gitClone(activeWorkspaceId, { repoUrl, branch, workspaceName: activeWorkspaceId, }); }, "office:repositoryCloned", "office:cloneFailed", ), [activeWorkspaceId, repoUrl, branch, runOp], ); const handlePull = useCallback( () => runOp( () => officeApi.gitPull(activeWorkspaceId), "office:pulledLatestChanges", "office:pullFailed", ), [activeWorkspaceId, runOp], ); const handlePush = useCallback( () => runOp( async () => { // i18n-exempt: becomes the git commit message. See the comment below. await officeApi.gitPush(activeWorkspaceId, { // Literal: this becomes the git COMMIT MESSAGE, so it is persisted // content rather than UI copy. The input placeholder that shows it // is translated; the value written to history stays English. message: commitMessage || "Update workspace configuration", }); setCommitMessage(""); }, "office:changesPushed", "office:pushFailed", ), [activeWorkspaceId, commitMessage, runOp], ); const isGit = gitStatus?.is_git ?? false; return (
{error && } {!isGit && ( )} {isGit && gitStatus && ( )}
); } function ErrorBanner({ message }: { message: string }) { return (

{message}

); } function CloneForm({ repoUrl, branch, loading, disabled, onRepoUrlChange, onBranchChange, onClone, }: { repoUrl: string; branch: string; loading: boolean; disabled: boolean; onRepoUrlChange: (v: string) => void; onBranchChange: (v: string) => void; onClone: () => void; }) { const { t } = useTranslation(); return (

{t("office:connectAGitRepositoryToVersion")}

onRepoUrlChange(e.target.value)} placeholder="https://github.com/org/config.git" className="mt-1" />
onBranchChange(e.target.value)} placeholder={t("office:main")} className="mt-1" />
{disabled && (

{t("office:configSyncActiveGuardReason")}

)}
); } function GitStatusDisplay({ status, commitMessage, loading, pullDisabled, onCommitMessageChange, onPull, onPush, onRefresh, }: { status: GitStatusData; commitMessage: string; loading: boolean; pullDisabled: boolean; onCommitMessageChange: (v: string) => void; onPull: () => void; onPush: () => void; onRefresh: () => void; }) { const { t } = useTranslation(); return (
{status.branch} {status.is_dirty ? ( {t("office:dirty")} ) : ( clean )}
{status.has_remote && (status.ahead > 0 || status.behind > 0) && (
{status.ahead > 0 && ( {status.ahead} ahead )} {status.behind > 0 && ( {status.behind} behind )}
)}
{pullDisabled && (

{t("office:configSyncActiveGuardReason")}

)}
onCommitMessageChange(e.target.value)} placeholder={t("office:updateWorkspaceConfiguration")} className="mt-1" />
); }