/** * Methodology settings hooks — view and edit golden ratios, * discussion rules, and content checklists. */ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { apiRequest } from "./client"; // ── Types ──────────────────────────────────────────────────────── export type MethodologyItem = { value: T; is_override: boolean; updated_at: string | null; }; export type MethodologyResponse = { items: Record>; }; /** Golden ratio per section: [min%, max%] */ export type GoldenRatios = Record; // ── Query Keys ─────────────────────────────────────────────────── export const methodologyKeys = { all: ["methodology"] as const, category: (cat: string) => [...methodologyKeys.all, cat] as const, }; // ── Hooks ──────────────────────────────────────────────────────── export function useMethodology(category: string) { return useQuery({ queryKey: methodologyKeys.category(category), queryFn: ({ signal }) => apiRequest>( `/api/methodology/${category}`, { signal }, ), staleTime: 30_000, }); } export function useUpdateMethodology(category: string) { const qc = useQueryClient(); return useMutation({ mutationFn: ({ key, value }: { key: string; value: unknown }) => apiRequest<{ key: string; value: unknown; is_override: boolean }>( `/api/methodology/${category}/${key}`, { method: "PUT", body: { value } }, ), onSuccess: () => { qc.invalidateQueries({ queryKey: methodologyKeys.category(category) }); }, }); } export function useResetMethodology(category: string) { const qc = useQueryClient(); return useMutation({ mutationFn: (key: string) => apiRequest<{ key: string; value: unknown; is_override: boolean }>( `/api/methodology/${category}/${key}`, { method: "DELETE" }, ), onSuccess: () => { qc.invalidateQueries({ queryKey: methodologyKeys.category(category) }); }, }); }