import { useState, useEffect, useCallback } from 'react'; import { getActiveCalcuttaTemplates } from '@bettoredge/api'; import type { CalcuttaTemplateProps } from '@bettoredge/types'; export interface CalcuttaTemplatesState { loading: boolean; templates: CalcuttaTemplateProps[]; error?: string; refresh: () => void; } export const useCalcuttaTemplates = (): CalcuttaTemplatesState => { const [loading, setLoading] = useState(true); const [templates, setTemplates] = useState([]); const [error, setError] = useState(); const fetchTemplates = useCallback(async () => { try { setLoading(true); setError(undefined); const resp = await getActiveCalcuttaTemplates(); setTemplates(resp.templates || []); } catch (e: any) { setError(e.message || 'Unable to load templates'); setTemplates([]); } finally { setLoading(false); } }, []); useEffect(() => { fetchTemplates(); }, [fetchTemplates]); return { loading, templates, error, refresh: fetchTemplates }; };