// useWorks.jsx — hook para cargar trabajos públicos desde Supabase
function useWorks() {
  const [works, setWorks] = React.useState(window.__WORKS_CACHE || []);
  const [loading, setLoading] = React.useState(!window.__WORKS_CACHE);

  const reload = React.useCallback(async () => {
    setLoading(true);
    const list = await window.supabaseApi.fetchPublicWorks();
    window.__WORKS_CACHE = list;
    setWorks(list);
    setLoading(false);
  }, []);

  React.useEffect(() => {
    reload();
    const onRefresh = () => reload();
    window.addEventListener('works:refresh', onRefresh);
    return () => window.removeEventListener('works:refresh', onRefresh);
  }, [reload]);

  return { works, loading, reload };
}

window.useWorks = useWorks;
