import { useState, useEffect } from "react"; import { get } from "../api/client"; import type { FormDescriptor } from "../api/types"; // Module-level cache to avoid refetching across mounts. let cached: FormDescriptor | null = null; export function useFormDescriptor(schemaName = "kindred-rd") { const [descriptor, setDescriptor] = useState(cached); const [loading, setLoading] = useState(cached === null); useEffect(() => { if (cached) return; get(`/api/schemas/${encodeURIComponent(schemaName)}/form`) .then((desc) => { cached = desc; setDescriptor(desc); }) .catch(() => {}) .finally(() => setLoading(false)); }, [schemaName]); // Derive flat categories map from the category_picker stages const categories: Record = {}; if (descriptor?.category_picker) { const subcatStage = descriptor.category_picker.stages.find( (s) => s.values_by_domain, ); if (subcatStage?.values_by_domain) { for (const domainVals of Object.values(subcatStage.values_by_domain)) { Object.assign(categories, domainVals); } } } return { descriptor, categories, loading }; }