|
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import { useT } from "../i18n/shared"; |
| 3 | +import { readJsonOrThrow } from "../fetch-json"; |
| 4 | + |
| 5 | +const FEATURE_ENDPOINT = "/api/codex-auth/features/default-mode-request-user-input"; |
| 6 | + |
| 7 | +type Feedback = { tone: "ok" | "err"; message: string } | null; |
| 8 | + |
| 9 | +/** |
| 10 | + * Codex Auth page toggle for Codex's own `default_mode_request_user_input` |
| 11 | + * feature flag. Reads/writes $CODEX_HOME/config.toml through the management |
| 12 | + * API, which flips the flag via the official `codex features` CLI. |
| 13 | + */ |
| 14 | +export default function DefaultModeRequestUserInputSetting({ apiBase }: { apiBase: string }) { |
| 15 | + const t = useT(); |
| 16 | + const [enabled, setEnabled] = useState(false); |
| 17 | + const [hydrated, setHydrated] = useState(false); |
| 18 | + const [saving, setSaving] = useState(false); |
| 19 | + const [loadError, setLoadError] = useState(false); |
| 20 | + const [feedback, setFeedback] = useState<Feedback>(null); |
| 21 | + const savingRef = useRef(false); |
| 22 | + const enabledRef = useRef(false); |
| 23 | + const loadGenerationRef = useRef(0); |
| 24 | + |
| 25 | + const load = useCallback(async () => { |
| 26 | + // A poll landing between the optimistic flip and the PUT response must not |
| 27 | + // revert the UI to the server's pre-save value. The generation also drops |
| 28 | + // GETs that were already in flight when a save started. |
| 29 | + if (savingRef.current) return; |
| 30 | + const generation = ++loadGenerationRef.current; |
| 31 | + try { |
| 32 | + const res = await fetch(`${apiBase}${FEATURE_ENDPOINT}`); |
| 33 | + if (!res.ok) throw new Error("load"); |
| 34 | + const payload = await res.json() as { enabled?: unknown }; |
| 35 | + if (savingRef.current || generation !== loadGenerationRef.current) return; |
| 36 | + enabledRef.current = payload.enabled === true; |
| 37 | + setEnabled(enabledRef.current); |
| 38 | + setHydrated(true); |
| 39 | + setLoadError(false); |
| 40 | + } catch { |
| 41 | + if (!savingRef.current && generation === loadGenerationRef.current) setLoadError(true); |
| 42 | + } |
| 43 | + }, [apiBase]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + const timeout = window.setTimeout(() => { void load(); }, 0); |
| 47 | + const interval = window.setInterval(() => { void load(); }, 30_000); |
| 48 | + return () => { |
| 49 | + window.clearTimeout(timeout); |
| 50 | + window.clearInterval(interval); |
| 51 | + }; |
| 52 | + }, [load]); |
| 53 | + |
| 54 | + const toggle = useCallback(async () => { |
| 55 | + if (savingRef.current || !hydrated || loadError) return; |
| 56 | + const next = !enabledRef.current; |
| 57 | + const previous = enabledRef.current; |
| 58 | + enabledRef.current = next; |
| 59 | + setEnabled(next); |
| 60 | + savingRef.current = true; |
| 61 | + setSaving(true); |
| 62 | + setFeedback(null); |
| 63 | + loadGenerationRef.current++; |
| 64 | + try { |
| 65 | + const res = await fetch(`${apiBase}${FEATURE_ENDPOINT}`, { |
| 66 | + method: "PUT", |
| 67 | + headers: { "content-type": "application/json" }, |
| 68 | + body: JSON.stringify({ enabled: next }), |
| 69 | + }); |
| 70 | + const payload = (await readJsonOrThrow<{ ok?: boolean; enabled?: unknown; changed?: unknown }>(res)) ?? {}; |
| 71 | + if (payload.ok !== true) throw new Error(String(res.status)); |
| 72 | + enabledRef.current = payload.enabled === true; |
| 73 | + setEnabled(enabledRef.current); |
| 74 | + setHydrated(true); |
| 75 | + setFeedback({ |
| 76 | + tone: "ok", |
| 77 | + message: t(payload.changed === true ? "codexAuth.requestUserInputUpdatedRestart" : "codexAuth.requestUserInputUpdated"), |
| 78 | + }); |
| 79 | + } catch (error) { |
| 80 | + enabledRef.current = previous; |
| 81 | + setEnabled(previous); |
| 82 | + const reason = error instanceof Error && error.message && !/^HTTP \d{3}$/.test(error.message) |
| 83 | + ? error.message |
| 84 | + : t("codexAuth.requestUserInputUpdateFailed"); |
| 85 | + setFeedback({ tone: "err", message: reason }); |
| 86 | + } finally { |
| 87 | + savingRef.current = false; |
| 88 | + setSaving(false); |
| 89 | + } |
| 90 | + }, [apiBase, hydrated, loadError, t]); |
| 91 | + |
| 92 | + const controlsDisabled = saving || !hydrated || loadError; |
| 93 | + |
| 94 | + return ( |
| 95 | + <div |
| 96 | + className="card card-row codex-request-user-input-card" |
| 97 | + style={{ marginTop: 16 }} |
| 98 | + aria-busy={saving || (!hydrated && !loadError) || undefined} |
| 99 | + > |
| 100 | + <div className="codex-request-user-input-copy"> |
| 101 | + <strong>{t("codexAuth.requestUserInput")}</strong> |
| 102 | + <div className="card-sub" role={loadError ? "alert" : undefined}> |
| 103 | + {loadError ? t("codexAuth.requestUserInputLoadFailed") : t("codexAuth.requestUserInputDesc")} |
| 104 | + </div> |
| 105 | + <code className="mono codex-request-user-input-config"> |
| 106 | + {`[features]\ndefault_mode_request_user_input = true`} |
| 107 | + </code> |
| 108 | + </div> |
| 109 | + <div className="codex-request-user-input-controls"> |
| 110 | + {loadError && ( |
| 111 | + <button type="button" className="btn btn-ghost btn-sm" onClick={() => { void load(); }}> |
| 112 | + {t("common.retry")} |
| 113 | + </button> |
| 114 | + )} |
| 115 | + <button |
| 116 | + type="button" |
| 117 | + className={`toggle ${enabled ? "on" : ""}`} |
| 118 | + onClick={() => { void toggle(); }} |
| 119 | + disabled={controlsDisabled} |
| 120 | + aria-pressed={enabled} |
| 121 | + aria-label={t("codexAuth.requestUserInput")} |
| 122 | + title={t("codexAuth.requestUserInput")} |
| 123 | + > |
| 124 | + <span className="toggle-knob" /> |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + {feedback && ( |
| 128 | + <div |
| 129 | + className={`codex-request-user-input-feedback${feedback.tone === "err" ? " is-error" : ""}`} |
| 130 | + role={feedback.tone === "err" ? "alert" : "status"} |
| 131 | + aria-atomic="true" |
| 132 | + > |
| 133 | + {feedback.message} |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + ); |
| 138 | +} |
0 commit comments