Skip to content

Open-source, local-first AI agent for real work.

5.7 KB
Raw Blame
import { ModelPicker, useTranslation } from "@vetta-org/plugin-sdk";
import { SettingRow, SettingSection } from "@vetta-org/theme-ui/plugin-ui";
import { Button, Switch } from "@vetta-org/ui";
import { useCallback, useEffect, useState } from "react";
import { getGitCommand } from "../git/runtime";
import { DEFAULT_SETTINGS, type GitSettings, loadSettings, saveSettings } from "../git/settings";
import { isGitAvailable } from "../git/tab-visibility";
import { RefreshIcon } from "./icons";

/** 本机没有 git 时的提示条:配置照常可改,但面板要装好 git 才能用。 */
function NoGitBanner({ onRecheck }: { onRecheck: () => Promise<void> }): JSX.Element {
	const { t } = useTranslation();
	const [busy, setBusy] = useState(false);
	return (
		<div
			role="status"
			className="flex items-start gap-3 rounded-lg border border-amber-500/40 bg-amber-500/10 px-3.5 py-2.5 text-[12px] leading-relaxed"
		>
			<div className="min-w-0 flex-1">
				<div className="font-medium text-foreground">{t("noGit.title")}</div>
				<div className="mt-0.5 text-muted-foreground">{t("noGit.settingsHint")}</div>
			</div>
			<Button
				type="button"
				size="sm"
				variant="outline"
				disabled={busy}
				onClick={() => {
					setBusy(true);
					void onRecheck().finally(() => setBusy(false));
				}}
			>
				<RefreshIcon className={`h-3.5 w-3.5 ${busy ? "animate-spin" : ""}`} />
				{busy ? t("noGit.checking") : t("noGit.recheck")}
			</Button>
		</div>
	);
}

/**
 * The plugin's configuration page, registered as an off-sidebar workspace view
 * (Settings → Extensions). The host has no declarative settings schema since
 * plugin API 1.6.0 (ADR-0105), so the page is ours to compose — but the chrome
 * (section cards, setting rows) and the model picker come from the host, so it
 * looks and behaves like the built-in settings pages.
 *
 * Settings are global to the plugin: project-specific commit rules belong in the
 * project's AGENTS.md / CLAUDE.md, which generation reads with higher priority.
 */
export function GitSettingsView(): JSX.Element {
	const { t } = useTranslation();
	const [settings, setSettings] = useState<GitSettings | null>(null);
	const [saved, setSaved] = useState(false);
	const [gitAvailable, setGitAvailable] = useState(true);

	const checkGit = useCallback(async () => {
		setGitAvailable(await isGitAvailable(getGitCommand()));
	}, []);

	useEffect(() => {
		void checkGit();
	}, [checkGit]);

	useEffect(() => {
		let alive = true;
		void loadSettings().then((value) => {
			if (alive) setSettings(value);
		});
		return () => {
			alive = false;
		};
	}, []);

	const update = useCallback((patch: Partial<GitSettings>) => {
		setSaved(false);
		setSettings((prev) => (prev ? { ...prev, ...patch } : prev));
	}, []);

	const persist = useCallback(async () => {
		if (!settings) return;
		await saveSettings(settings);
		setSaved(true);
	}, [settings]);

	if (!settings) {
		return <div className="px-6 py-4 text-[12px] text-muted-foreground">{t("state.loading")}</div>;
	}

	return (
		<div className="mx-auto flex w-full max-w-2xl flex-col gap-2 px-6 py-5">
			<div>
				<h1 className="text-[15px] font-semibold text-foreground">{t("settings.title")}</h1>
				<p className="mt-1 mb-4 text-[12px] leading-relaxed text-muted-foreground">{t("settings.tagline")}</p>
			</div>

			{!gitAvailable && <NoGitBanner onRecheck={checkGit} />}

			<SettingSection
				section={{ id: "git-commit-message", title: t("settings.commitSectionTitle") }}
			>
				<div className="px-4 py-3">
					<div className="text-[13px] font-medium text-foreground">{t("settings.templateTitle")}</div>
					<div className="mt-0.5 text-[12px] leading-relaxed text-muted-foreground">
						{t("settings.templateHint")}
					</div>
					<textarea
						value={settings.messageTemplate}
						onChange={(event) => update({ messageTemplate: event.target.value })}
						rows={6}
						placeholder={t("settings.templatePlaceholder")}
						className="mt-2 w-full resize-y rounded-md border border-border bg-background px-2.5 py-2 text-[12px] leading-relaxed text-foreground outline-none placeholder:text-muted-foreground/70 focus:border-ring"
					/>
				</div>
				<SettingRow title={t("settings.modelTitle")} description={t("settings.modelHint")} border={false}>
					{/* 与会话输入栏、知识库设置同一个选择器:模型目录、服务商分栏与收藏都由宿主提供。 */}
					<ModelPicker
						value={settings.modelKey ?? null}
						onChange={(modelKey) => update({ modelKey })}
						emptyOption={{ label: t("settings.modelDefault") }}
						placeholder={t("settings.modelDefault")}
						ariaLabel={t("settings.modelTitle")}
						className="w-56"
					/>
				</SettingRow>
			</SettingSection>

			<SettingSection
				section={{ id: "git-panel-behavior", title: t("settings.behaviorSectionTitle") }}
			>
				<SettingRow title={t("settings.pushAfterCommitTitle")} description={t("settings.pushAfterCommitHint")}>
					<Switch checked={settings.pushAfterCommit} onCheckedChange={(next) => update({ pushAfterCommit: next })} />
				</SettingRow>
				<SettingRow
					title={t("settings.confirmDiscardTitle")}
					description={t("settings.confirmDiscardHint")}
					border={false}
				>
					<Switch checked={settings.confirmDiscard} onCheckedChange={(next) => update({ confirmDiscard: next })} />
				</SettingRow>
			</SettingSection>

			<div className="mt-2 flex items-center gap-2">
				<Button type="button" size="sm" onClick={() => void persist()}>
					{t("settings.save")}
				</Button>
				<Button type="button" size="sm" variant="ghost" onClick={() => update(DEFAULT_SETTINGS)}>
					{t("settings.reset")}
				</Button>
				{saved && <span className="text-[12px] text-emerald-500">{t("settings.saved")}</span>}
			</div>
		</div>
	);
}