({ api, active, paused, pollMs, refreshToken }: Omit<Props, 'listProjects' | 'onOpenProjects'> & { pollMs: number })
| 25 | }; |
| 26 | |
| 27 | function WatcherSetting({ api, active, paused, pollMs, refreshToken }: Omit<Props, 'listProjects' | 'onOpenProjects'> & { pollMs: number }) { |
| 28 | const [snapshot, setSnapshot] = useState<ConfigSnapshot | null>(null); |
| 29 | const [optimistic, setOptimistic] = useState<boolean | null>(null); |
| 30 | const [saving, setSaving] = useState(false); |
| 31 | const [saveError, setSaveError] = useState(''); |
| 32 | const pending = useRef(false); |
| 33 | const readConfiguration = useCallback(async () => { |
| 34 | if (!api.configuration) throw new Error('Watcher configuration is unavailable.'); |
| 35 | return api.configuration(); |
| 36 | }, [api]); |
| 37 | const reading = useSystemPoll(readConfiguration, active, paused || saving, pollMs); |
| 38 | useEffect(() => { if (reading.data) setSnapshot(reading.data); }, [reading.data]); |
| 39 | useEffect(() => { if (active && refreshToken > 0) reading.refresh(); }, [active, refreshToken, reading.refresh]); |
| 40 | const setting = snapshot?.settings.find(item => item.key === 'watcher_enabled'); |
| 41 | const saved = setting?.override ?? setting?.value; |
| 42 | const known = saved === 'true' || saved === 'false'; |
| 43 | const change = async (enabled: boolean) => { |
| 44 | if (!snapshot || !setting?.editable || !api.saveConfiguration || pending.current) return; |
| 45 | pending.current = true; setSaving(true); setOptimistic(enabled); setSaveError(''); |
| 46 | try { setSnapshot(await api.saveConfiguration(snapshot.revision, { watcher_enabled: enabled ? 'true' : 'false' })); } |
| 47 | catch (error) { setSaveError(`Could not save the watcher setting. ${failureText(error)}`); } |
| 48 | finally { pending.current = false; setSaving(false); setOptimistic(null); } |
| 49 | }; |
| 50 | return <section className="system-watcher-setting" aria-label="Background watcher configuration"> |
| 51 | <label className="system-watcher-toggle"><input type="checkbox" role="switch" aria-label="Background watcher" checked={optimistic ?? saved === 'true'} disabled={!known || !setting?.editable || !api.saveConfiguration || saving || !!reading.error || reading.loading} onChange={event => { void change(event.currentTarget.checked); }} /><span>Background watcher <small>Applies after daemon restart</small></span></label> |
| 52 | <div className="system-watcher-details"><span>{saving ? 'Saving…' : `Saved: ${booleanText(saved)}`}</span><span>Running setting: {setting?.effectiveKnown === false ? 'Unknown' : booleanText(setting?.effective)}</span>{setting?.pendingRestart && <strong>Restart pending</strong>}</div> |
| 53 | {setting?.readOnlyReason && <p className="system-muted">{setting.readOnlyReason}</p>} |
| 54 | {(!setting && !reading.loading) && <p className="system-muted">This daemon does not report an editable watcher setting.</p>} |
| 55 | {(saveError || reading.error) && <p className="system-warning" role="alert">{saveError || reading.error}</p>} |
| 56 | <p className="system-muted">This setting applies to all projects. Project registrations come from agent sessions.</p> |
| 57 | </section>; |
| 58 | } |
| 59 | |
| 60 | export default function IndexesPanel({ api, listProjects, active, paused, refreshToken, pollMs = 15000, onOpenProjects }: Props) { |
| 61 | const previous = useRef(new Map<string, IndexReading>()); |
nothing calls this directly
no test coverage detected