({ service, onClose, embedded = false, active = true, ...browser }: Props)
| 42 | } |
| 43 | |
| 44 | export default function ConfigPanel({ service, onClose, embedded = false, active = true, ...browser }: Props) { |
| 45 | const [snapshot, setSnapshot] = useState<ConfigSnapshot>(); |
| 46 | const [category, setCategory] = useState(''); |
| 47 | const [draft, setDraft] = useState<Record<string, string | null>>({}); |
| 48 | const [loading, setLoading] = useState(true); |
| 49 | const [saving, setSaving] = useState(false); |
| 50 | const [error, setError] = useState(''); |
| 51 | const [notice, setNotice] = useState(''); |
| 52 | const [confirmClose, setConfirmClose] = useState(false); |
| 53 | const dialog = useRef<HTMLDivElement>(null); |
| 54 | const generation = useRef(0); |
| 55 | const pending = useRef(false); |
| 56 | const count = Object.keys(draft).length; |
| 57 | const draftCount = useRef(count); |
| 58 | draftCount.current = count; |
| 59 | const references = snapshot?.settings.filter(setting => setting.category.toLowerCase() === 'reference') ?? []; |
| 60 | const categories = [...new Set(snapshot?.settings.filter(setting => setting.category.toLowerCase() !== 'reference').map(setting => setting.category) ?? [])]; |
| 61 | const activeCategory = category || categories[0] || 'browser'; |
| 62 | const invalid = snapshot?.settings.some(setting => Object.hasOwn(draft, setting.key) && configValueError(setting, draft[setting.key])); |
| 63 | const reload = useCallback(async () => { |
| 64 | const ticket = ++generation.current; |
| 65 | setLoading(true); setError(''); setNotice(''); |
| 66 | try { const next = await service.configuration(); if (ticket === generation.current) { setSnapshot(next); setDraft({}); setConfirmClose(false); } } |
| 67 | catch (failure) { if (ticket === generation.current) setError(failure instanceof Error ? failure.message : 'Could not read configuration.'); } |
| 68 | finally { if (ticket === generation.current) setLoading(false); } |
| 69 | }, [service]); |
| 70 | useEffect(() => () => { generation.current++; }, [service]); |
| 71 | useEffect(() => { if (active && draftCount.current === 0 && !pending.current) void reload(); }, [active, reload]); |
| 72 | useEffect(() => { |
| 73 | if (embedded) return; |
| 74 | const previous = document.activeElement; |
| 75 | dialog.current?.focus(); |
| 76 | return () => { if (previous instanceof HTMLElement && previous.isConnected) previous.focus(); }; |
| 77 | }, [embedded]); |
| 78 | const close = () => { if (pending.current) return; if (count) setConfirmClose(true); else onClose(); }; |
| 79 | const keyDown = (event: KeyboardEvent<HTMLDivElement>) => { |
| 80 | if (event.key === 'Escape') { event.stopPropagation(); close(); } |
| 81 | if (event.key !== 'Tab') return; |
| 82 | const focusable = [...(dialog.current?.querySelectorAll<HTMLElement>('button:not(:disabled), input:not(:disabled), select:not(:disabled), summary, [tabindex="0"]') ?? [])].filter(element => element.getClientRects().length); |
| 83 | const first = focusable[0], last = focusable.at(-1); |
| 84 | if (!first) { event.preventDefault(); return; } |
| 85 | if (event.shiftKey && (document.activeElement === first || document.activeElement === dialog.current)) { event.preventDefault(); last?.focus(); } |
| 86 | else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first.focus(); } |
| 87 | }; |
| 88 | const save = async () => { |
| 89 | if (!snapshot || pending.current || !count || invalid) return; |
| 90 | pending.current = true; setSaving(true); setError(''); setNotice(''); |
| 91 | const ticket = ++generation.current; |
| 92 | try { |
| 93 | const next = await service.saveConfiguration(snapshot.revision, draft); |
| 94 | if (ticket === generation.current) { |
| 95 | setSnapshot(next); setDraft({}); setConfirmClose(false); |
| 96 | setNotice(next.settings.some(setting => setting.pendingRestart) ? 'Saved. Restart CBM to apply pending settings throughout.' : 'Saved. Changes apply at the times shown below.'); |
| 97 | } |
| 98 | } catch (failure) { if (ticket === generation.current) setError(failure instanceof Error ? failure.message : 'Could not save configuration. Your edits are retained.'); } |
| 99 | finally { pending.current = false; if (ticket === generation.current) setSaving(false); } |
| 100 | }; |
| 101 | const navigate = (action: () => void) => { if (count) { setError('Save or discard your daemon changes before opening another settings panel.'); return; } action(); }; |
nothing calls this directly
no test coverage detected