"use client"; import { useTranslations } from "next-intl"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { adminFetch } from "@/lib/admin-fetch"; interface Configuration { provider: string; enabled: boolean; remote: string; branch: string; username: string; hasToken: boolean; } export function CatalogGitConfiguration({ onSaved }: { onSaved: () => void }) { const t = useTranslations("admin.studio.sync"); const [config, setConfig] = useState(null), [token, setToken] = useState(""), [busy, setBusy] = useState(false), [error, setError] = useState(""), [message, setMessage] = useState(""); useEffect(() => { const abort = new AbortController(); void (async () => { try { const res = await adminFetch("/api/admin/catalog-export/config", { signal: abort.signal, cache: "no-store", }); if (!res.ok) throw new Error(t("loadFailed")); const data = await res.json(); if (!abort.signal.aborted) setConfig(data); } catch (error) { if (!abort.signal.aborted) setError((error as Error).message); } })(); return () => abort.abort(); }, [t]); async function submit(action: "save" | "test") { if (!config || busy) return; setBusy(true); setError(""); setMessage(""); try { const res = await adminFetch("/api/admin/catalog-export/config", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...config, token, action }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || t("requestFailed")); if (action === "save") { setConfig(data); setToken(""); setMessage(t("saved")); onSaved(); } else setMessage(data.message); } catch (error) { setError((error as Error).message); } finally { setBusy(false); } } return (
{t("gitTitle")}

{t("gitIntro")}

{config && (

{t("providerLabel")} {t("providerValue")}

{t("tokenNote")}

)} {error && (

{error}

)} {message && (

{message}

)}
); }