import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { RotateCcw, Save, Send } from "lucide-react"; import { api, type ConfigItem, type SystemConfigData } from "../lib/api"; import { notify } from "../lib/notifications"; import Tooltip from "./Tooltip"; // Admin Configuration page: edit DB-overridable operational settings (registry-driven from // the backend — adding a key there makes it appear here automatically). Group order is fixed // where known so logically related settings (e.g. Email/SMTP) stay together. const GROUP_ORDER = ["email"]; export default function ConfigPanel() { const { t } = useTranslation(); const qc = useQueryClient(); const q = useQuery({ queryKey: ["admin-config"], queryFn: api.adminConfig }); const data = q.data; const apply = (fresh: SystemConfigData) => qc.setQueryData(["admin-config"], fresh); const save = useMutation({ mutationFn: ({ key, value }: { key: string; value: string | number | boolean }) => api.setConfig(key, value), onSuccess: (fresh) => { apply(fresh); notify({ level: "success", message: t("config.saved") }); }, onError: () => notify({ level: "error", message: t("config.saveFailed") }), }); const reset = useMutation({ mutationFn: (key: string) => api.resetConfig(key), onSuccess: (fresh) => { apply(fresh); notify({ level: "success", message: t("config.resetDone") }); }, onError: () => notify({ level: "error", message: t("config.saveFailed") }), }); const testEmail = useMutation({ mutationFn: () => api.testEmail(), onSuccess: (r) => notify({ level: "success", message: t("config.testSent", { to: r.to }) }), onError: () => notify({ level: "error", message: t("config.testFailed") }), }); const busy = save.isPending || reset.isPending; if (q.isLoading || !data) { return
{t("config.intro")}
{groupKeys.map((g) => ({t(`config.fields.${item.key}.hint`, "")}
{item.secret ? ({secretDisabled ? t("config.secretsDisabled") : secretStatus}
) : ({item.is_set ? t("config.overridden") : t("config.usingDefault")}
)}