/** * `useProject` — the active project + a smooth switch. Switching sets the * active cwd (the fetch/EventSource shim scopes to it) and invalidates the * per-project queries so they refetch — no hard page reload. */ import { computed, ref } from "vue"; import { useQueryClient } from "@tanstack/vue-query"; import { getActiveProjectCwd, setActiveProjectCwd, loadCatalog } from "../lib/project-catalog"; export function useProject() { // useQueryClient throws without a provider (e.g. a bare unit test); tolerate it. let queryClient: ReturnType | undefined; try { queryClient = useQueryClient(); } catch { queryClient = undefined; } const activeCwd = ref(getActiveProjectCwd()); const activeName = computed(() => { const cwd = activeCwd.value; if (!cwd) return null; return loadCatalog()[cwd]?.name ?? cwd; }); function switchTo(cwd: string | null): void { setActiveProjectCwd(cwd); activeCwd.value = cwd; // Refetch everything against the new active cwd — no reload. void queryClient?.invalidateQueries(); } return { activeCwd, activeName, switchTo }; }