import { useEffect, useMemo, useRef } from "react"; import { useStore } from "../state/store.tsx"; import { createRepoLoader } from "../state/repoLoader.ts"; import type { GitforestConfig } from "../types/index.ts"; /** * Thin React adapter over createRepoLoader (see ADR-0006). All orchestration * — cache-then-refresh, 400ms debounce on the refreshing flag, in-flight * guard, clone flows — lives in src/state/repoLoader.ts and is tested * without React. This hook wires the loader to the store and triggers * the initial load on mount. * * Future contributors will be tempted to inline this hook back into one * place. Don't — the inversion is the entire point. */ export function useUnifiedRepos(config: GitforestConfig) { const { state, dispatch } = useStore(); const stateRef = useRef(state); stateRef.current = state; const loader = useMemo( () => createRepoLoader({ config, dispatch, getState: () => stateRef.current }), [config, dispatch], ); useEffect(() => { void loader.load(); }, [loader]); return { loadUnifiedRepos: loader.load, refreshGitHub: loader.refreshGitHub, cloneRepo: loader.cloneRepo, batchClone: loader.batchClone, backgroundRefresh: loader.backgroundRefresh, }; }