"use client"; // Web fetch backend select — the Settings UI face of the persisted // `web_fetch.backend` system setting (#2448). Reads `GET /api/settings/web-fetch` // (choices come from the backend's FETCH_PROVIDERS table, never duplicated // here) and saves through the global Apply bar like every other settings // extension. import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ChevronDown, Loader2 } from "lucide-react"; import { useTranslation } from "react-i18next"; import { SettingRow, SettingSection, selectClass, selectOptionClass, } from "./shared"; import { type WebFetchSettingsPayload, webFetchBackendOptions, webFetchBackendValue, } from "./web-fetch-backend"; import { useSettings } from "@/features/settings/store/SettingsStore"; import { apiFetch, apiUrl } from "@/lib/api"; import { EXTENSION_ENDPOINTS } from "@/lib/settings-extensions"; export function WebFetchBackendCard() { const { t } = useTranslation(); const { catalogEditable, registerExtension, pendingExtensionPayload, draftRevision } = useSettings(); const [payload, setPayload] = useState(null); const [draft, setDraft] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (catalogEditable !== true) return; let cancelled = false; async function load() { setLoading(true); setError(null); try { const response = await apiFetch(apiUrl(EXTENSION_ENDPOINTS["web-fetch"])); const data = (await response.json().catch(() => ({}))) as | WebFetchSettingsPayload | { detail?: string }; if (!response.ok) { throw new Error( "detail" in data && data.detail ? String(data.detail) : t("Failed to load web fetch settings."), ); } if (cancelled) return; const next = data as WebFetchSettingsPayload; setPayload(next); const pending = pendingExtensionPayload("web-fetch") as | { backend?: string } | undefined; setDraft(pending?.backend ?? next.settings.backend); } catch (err) { if (!cancelled) setError(err instanceof Error ? err.message : String(err)); } finally { if (!cancelled) setLoading(false); } } load(); return () => { cancelled = true; }; }, [catalogEditable, draftRevision, pendingExtensionPayload, t]); const options = useMemo( () => webFetchBackendOptions(payload?.choices, draft ?? payload?.settings.backend), [draft, payload], ); const selected = webFetchBackendValue(draft, options); const dirty = Boolean( payload && draft !== null && payload.settings.backend !== selected, ); // Flush through the global Apply (top toolbar) instead of a local button. // Refs keep the registered ``save`` closure reading the latest draft. const draftRef = useRef(draft); draftRef.current = draft; const save = useCallback(async () => { const current = draftRef.current; if (current === null) return; setError(null); try { const response = await apiFetch(apiUrl(EXTENSION_ENDPOINTS["web-fetch"]), { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ backend: current }), }); const data = (await response.json().catch(() => ({}))) as | WebFetchSettingsPayload | { detail?: string }; if (!response.ok) { throw new Error( "detail" in data && data.detail ? String(data.detail) : t("Failed to save web fetch settings."), ); } const next = data as WebFetchSettingsPayload; setPayload(next); setDraft(next.settings.backend); } catch (err) { setError(err instanceof Error ? err.message : String(err)); throw err; } }, [t]); useEffect(() => { if (catalogEditable !== true) return; registerExtension("web-fetch", { dirty, save, payload: draft === null ? null : { backend: draft }, }); return () => registerExtension("web-fetch", null); }, [catalogEditable, dirty, draft, save, registerExtension]); if (catalogEditable !== true) return null; return ( {loading && (
{t("Loading web fetch settings...")}
)} {!loading && error && (
{error}
)} {!loading && payload && ( } /> )} {!loading && payload && payload.effective.backend !== payload.settings.backend && (

{t("DEEPTUTOR_FETCH_BACKEND is overriding the saved choice for this deployment.")}

)}
); }