({ onRefresh }: Props)
| 32 | } |
| 33 | |
| 34 | export default function Routing({ onRefresh }: Props) { |
| 35 | const [config, setConfig] = useState<RoutingConfigState | null>(null); |
| 36 | const [mapping, setMapping] = useState<Mapping | null>(null); |
| 37 | const [drafts, setDrafts] = useState<Record<string, DraftState>>({}); |
| 38 | const [busyKey, setBusyKey] = useState<string | null>(null); |
| 39 | const [notice, setNotice] = useState<NoticeState | null>(null); |
| 40 | const [editorMode, setEditorMode] = useState<ModeName>("auto"); |
| 41 | |
| 42 | const load = useCallback(async () => { |
| 43 | const [nextConfig, nextMapping] = await Promise.all([ |
| 44 | fetchRoutingConfig(), |
| 45 | fetchMapping(), |
| 46 | ]); |
| 47 | if (nextConfig) { |
| 48 | setConfig(nextConfig); |
| 49 | setDrafts(buildDrafts(nextConfig)); |
| 50 | setEditorMode(nextConfig.default_mode as ModeName); |
| 51 | } |
| 52 | if (nextMapping) setMapping(nextMapping); |
| 53 | }, []); |
| 54 | |
| 55 | useEffect(() => { |
| 56 | load(); |
| 57 | }, [load]); |
| 58 | |
| 59 | useEffect(() => { |
| 60 | if (!notice) return; |
| 61 | const timeoutId = window.setTimeout(() => setNotice(null), 2400); |
| 62 | return () => window.clearTimeout(timeoutId); |
| 63 | }, [notice]); |
| 64 | |
| 65 | const defaultMode = config?.default_mode ?? "auto"; |
| 66 | const editable = config?.editable ?? true; |
| 67 | const modeMeta = getModeMeta(defaultMode); |
| 68 | const editModeMeta = getModeMeta(editorMode); |
| 69 | const selectedModeRows = config?.modes?.[editorMode]?.tiers ?? {}; |
| 70 | const modelOptions = mapping?.pool.map((model) => model.id) ?? []; |
| 71 | const modeSwitchBusy = busyKey?.startsWith("mode:") ?? false; |
| 72 | |
| 73 | function applyConfig(next: RoutingConfigState) { |
| 74 | setConfig(next); |
| 75 | setDrafts(buildDrafts(next)); |
| 76 | } |
| 77 | |
| 78 | function showNotice(text: string, tone: NoticeState["tone"] = "success") { |
| 79 | setNotice({ text, tone }); |
| 80 | } |
| 81 | |
| 82 | async function handleModeChange(nextMode: ModeName) { |
| 83 | if (!editable || nextMode === defaultMode) return; |
| 84 | setBusyKey(`mode:${nextMode}`); |
| 85 | setNotice(null); |
| 86 | const next = await setDefaultRoutingMode(nextMode); |
| 87 | if (next) { |
| 88 | applyConfig(next); |
| 89 | showNotice(`Default mode set to ${nextMode}.`); |
| 90 | onRefresh?.(); |
| 91 | } else { |
nothing calls this directly
no test coverage detected