// useArticles.jsx — hook para cargar artículos públicos desde Supabase
function useArticles() {
  const [articles, setArticles] = React.useState(window.__ARTICLES_CACHE || []);
  const [loading, setLoading] = React.useState(!window.__ARTICLES_CACHE);

  const reload = React.useCallback(async () => {
    setLoading(true);
    const list = await window.supabaseApi.fetchPublicArticles();
    window.__ARTICLES_CACHE = list;
    setArticles(list);
    setLoading(false);
  }, []);

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

  return { articles, loading, reload };
}

window.useArticles = useArticles;
