import { useEffect, useState } from 'react'; import { scenarios } from '../api'; import type { RunDefaults } from '../api'; // Server run defaults don't change while the app is open; fetch them once let cached: Promise | null = null; /** * Returns the server-configured defaults for new scenario runs, or null * while they are loading (or if the request failed). */ export function useRunDefaults(): RunDefaults | null { const [defaults, setDefaults] = useState(null); useEffect(() => { let cancelled = false; cached ??= scenarios.runDefaults().catch((err: unknown) => { cached = null; throw err; }); cached.then((d) => !cancelled && setDefaults(d)).catch(() => undefined); return () => { cancelled = true; }; }, []); return defaults; }