@@ -3,7 +3,7 @@ import { formatUptime } from "../formatUptime"; |
3 | 3 | import { IconAlert, IconExternal, IconInfo, IconRefresh, IconX } from "../icons"; |
4 | 4 | import { Trans } from "../i18n/provider"; |
5 | 5 | import { useI18n, type TKey } from "../i18n/shared"; |
6 | | -import { startupRiskDetailKey } from "../startup-health-ui"; |
| 6 | +import { settingsPollMayCommit, startupRiskDetailKey } from "../startup-health-ui"; |
7 | 7 | import { formatTokens } from "../format-tokens"; |
8 | 8 | import { EmptyState, Select } from "../ui"; |
9 | 9 |
|
@@ -222,6 +222,9 @@ export default function Dashboard({ apiBase }: { apiBase: string }) { |
222 | 222 | const updateRetryRef = useRef(0); |
223 | 223 | const updateRetryTimerRef = useRef<number | null>(null); |
224 | 224 | const updateRequestEpochRef = useRef(0); |
| 225 | + const settingsRequestEpochRef = useRef(0); |
| 226 | + const settingsMutationEpochRef = useRef(0); |
| 227 | + const settingsMutationInFlightRef = useRef(false); |
225 | 228 | const [updateCheck, setUpdateCheck] = useState<UpdateCheckData | null>(null); |
226 | 229 | const [updateError, setUpdateError] = useState<string | null>(null); |
227 | 230 | const [updateJob, setUpdateJob] = useState<UpdateJob | null>(null); |
@@ -244,6 +247,8 @@ export default function Dashboard({ apiBase }: { apiBase: string }) { |
244 | 247 |
|
245 | 248 | useEffect(() => { |
246 | 249 | const fetchData = async () => { |
| 250 | + const settingsRequestEpoch = ++settingsRequestEpochRef.current; |
| 251 | + const settingsMutationEpoch = settingsMutationEpochRef.current; |
247 | 252 | try { |
248 | 253 | const [hRes, pRes, sRes, scRes, shRes, uRes] = await Promise.all([ |
249 | 254 | fetch(`${apiBase}/healthz`), |
@@ -255,7 +260,15 @@ export default function Dashboard({ apiBase }: { apiBase: string }) { |
255 | 260 | ]); |
256 | 261 | setHealth(await hRes.json()); |
257 | 262 | setProviders(await pRes.json()); |
258 | | - setSettings(await sRes.json()); |
| 263 | + const nextSettings = await sRes.json() as SettingsData; |
| 264 | + if (settingsPollMayCommit( |
| 265 | + { request: settingsRequestEpoch, mutation: settingsMutationEpoch }, |
| 266 | + { |
| 267 | + request: settingsRequestEpochRef.current, |
| 268 | + mutation: settingsMutationEpochRef.current, |
| 269 | + mutationInFlight: settingsMutationInFlightRef.current, |
| 270 | + }, |
| 271 | + )) setSettings(nextSettings); |
259 | 272 | setSidecar(await scRes.json()); |
260 | 273 | // Old servers fall through to the SPA HTML for this route; don't let a parse |
261 | 274 | // failure here take down the whole dashboard. |
@@ -296,7 +309,10 @@ export default function Dashboard({ apiBase }: { apiBase: string }) { |
296 | 309 | }; |
297 | 310 | fetchData(); |
298 | 311 | const interval = setInterval(fetchData, 5000); |
299 | | - return () => clearInterval(interval); |
| 312 | + return () => { |
| 313 | + clearInterval(interval); |
| 314 | + settingsRequestEpochRef.current += 1; |
| 315 | + }; |
300 | 316 | }, [apiBase]); |
301 | 317 |
|
302 | 318 | useEffect(() => { |
@@ -450,6 +466,7 @@ export default function Dashboard({ apiBase }: { apiBase: string }) { |
450 | 466 | if (!settings || settingsSaving) return; |
451 | 467 | const next = !settings.codexAutoStart; |
452 | 468 | setSettingsSaving(true); |
| 469 | + settingsMutationInFlightRef.current = true; |
453 | 470 | setSettings({ ...settings, codexAutoStart: next }); |
454 | 471 | try { |
455 | 472 | const res = await fetch(`${apiBase}/api/settings`, { |
@@ -459,11 +476,13 @@ export default function Dashboard({ apiBase }: { apiBase: string }) { |
459 | 476 | }); |
460 | 477 | if (!res.ok) throw new Error("save failed"); |
461 | 478 | const data = await res.json() as { codexAutoStart: boolean; startupHealth?: SettingsData["startupHealth"] }; |
| 479 | + settingsMutationEpochRef.current += 1; |
462 | 480 | setSettings(prev => prev ? { ...prev, codexAutoStart: data.codexAutoStart, startupHealth: data.startupHealth ?? prev.startupHealth } : prev); |
463 | 481 | } catch { |
464 | 482 | setSettings(prev => prev ? { ...prev, codexAutoStart: !next } : prev); |
465 | 483 | setError(true); |
466 | 484 | } finally { |
| 485 | + settingsMutationInFlightRef.current = false; |
467 | 486 | setSettingsSaving(false); |
468 | 487 | } |
469 | 488 | }; |
|