Files
fusion/packages/dashboard/app/hooks/useProjects.ts
gsxdsm f26cbedf4f fix(dashboard): close the code-review findings on the mobile tab-discard work
An 11-reviewer pass over f157bf7460..f5163d8351 found defects in the mobile
tab-discard change set itself. This fixes them.

Silent data loss (the recurring defect class):
- AgentDetailView reconnect refetched limit:100 and replaced wholesale, so 380
  displayed lines vanished with no "Load older" and no indicator; it now
  reconciles through the shared logStreamReconcile helper.
- useActivityLog.loadMore past the cap discarded the page it had just fetched
  while advancing the cursor and leaving hasMore true, so the feed silently
  stopped paginating behind a live-looking button.
- useAgentLogs: loadMore and resyncFromServer had no mutual exclusion, a
  no-overlap resync discarded explicitly paged-back history, a resync outliving
  the reconnect delay left an unmarked gap, and the live-tail trim could evict
  the gap marker itself.
- useLiveTranscript's resync overwrote live entries that raced the refetch.

The premise itself was not fully delivered:
- useProjects, useNodes, and useMeshState never called clearInterval, so they
  polled the whole time the tab was hidden. useProjects is mounted for the
  entire session, so the page never went idle -- the primary mechanism this
  work depends on. All three now use the shared visibility gate.
- sse-bus fired onReconnect twice per reconnect cycle and fanned out ~28
  subscribers in one tick, against a ~6-connection-per-origin cap on a waking
  radio. The successful open is now the single authority, and the fan-out uses
  the same exported stagger primitive as the polling path rather than a second
  copy of the slot formula.
- A channel first subscribed during the hidden window opened a live EventSource
  and keepalive; suspension is now a module-level condition openChannel
  consults, and a channel opened inside the grace window re-arms it.

Credentials and correctness:
- The service worker persisted every GET /api/* to durable Cache Storage,
  including /api/settings with daemonToken, githubAuthToken, gitlabAuthToken
  and ntfyAccessToken in plaintext, with no exclusion and no purge path --
  "Clear all cached data" only walked localStorage. Now gated, bounded, and
  genuinely purgeable.
- useTasks cleared its own snapshot when the mount revalidation failed on a
  waking radio, so the board blanked and the next restore was empty too.
  Suspension-class failures no longer destroy the cache.
- A single-row SSE update reset lastFetchTimeMs to now while an hours-old
  hydrated snapshot was on screen, re-marking every in-progress card stuck.
- ListView's "Select all visible tasks" acted on the full filtered set while
  only 50 rows rendered, so a bulk delete reached rows the operator could not
  see. Column's search window reset keyed on a boolean, so refining a query
  kept the expanded window.

Tests that could not fail:
- App.test.tsx mocked TerminalModal as isOpen ? <div/> : null, making the
  unmount-on-close invariant unobservable; MockEventSource kept its listeners
  after close(), so cases passed with their onReconnect handlers deleted.
- The SSE resync ratchet scanned only hooks/, exempting ~13 component call
  sites -- the exact regression it exists to prevent.
- MissionControlPanel's bespoke poll and the xterm scrollback constants and
  WebGL disposal had no coverage at all.

Verified: tsc -p tsconfig.app.json clean, pnpm lint clean, pnpm
check:changesets clean, 877 tests passing across 36 scoped files.
Known unrelated red: MailboxView.test.tsx's FN-8407 CSS guard fails at HEAD
too -- this diff adds no @media rule and no .mailbox-view--mobile selector,
the only two things that assertion inspects. Left alone deliberately.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 11:17:52 -07:00

245 lines
8.4 KiB
TypeScript

import { useState, useEffect, useCallback, useRef } from "react";
import type { ProjectInfo } from "../api";
import {
fetchProjectsAcrossNodes,
hasNodeMappingsSupport,
registerProject,
unregisterProject,
updateProject,
type ProjectCreateInput,
type ProjectInfoWithSource,
type ProjectNodeAvailability,
} from "../api";
import { SWR_CACHE_KEYS, SWR_DEFAULT_MAX_AGE_MS, clearCache, readCache, writeCache } from "../utils/swrCache";
import { recordResumeEvent } from "../utils/resumeInstrumentation";
import { isVisibilityResumeError, useTabVisibilitySuspension, useVisibilityAwarePoll } from "./visibilitySuspension";
export interface UseProjectsResult {
/** List of all registered projects (local + remote) */
projects: ProjectInfoWithSource[];
/** Loading state for initial fetch */
loading: boolean;
/** Error message if fetch failed */
error: string | null;
/** Manually refresh projects list */
refresh: () => Promise<void>;
/** Register a new project */
register: (input: ProjectCreateInput) => Promise<ProjectInfo>;
/** Update an existing project */
update: (id: string, updates: Partial<ProjectInfo>) => Promise<ProjectInfo>;
/** Unregister a project */
unregister: (id: string) => Promise<void>;
}
const POLL_INTERVAL_MS = 5000; // 5 seconds
const VISIBILITY_REFRESH_DEBOUNCE_MS = 1000;
function normalizeNodeMappings(project: ProjectInfoWithSource): ProjectNodeAvailability[] {
const mappingSource = hasNodeMappingsSupport(project)
? (project.nodeMappings ?? project.projectNodeMappings ?? project.pathMappings ?? [])
: [];
const normalizedMappings = mappingSource
.filter((mapping) => Boolean(mapping?.nodeId) && Boolean(mapping?.path))
.map((mapping) => ({
nodeId: mapping.nodeId,
nodeName: mapping.nodeName,
path: mapping.path,
available: mapping.available !== false,
}));
if (normalizedMappings.length > 0) {
return normalizedMappings;
}
if (project.nodeId && project.path) {
return [{
nodeId: project.nodeId,
nodeName: project._sourceNodeName,
path: project.path,
available: true,
}];
}
return [];
}
function normalizeProjects(projects: ProjectInfoWithSource[]): ProjectInfoWithSource[] {
return projects.map((project) => ({
...project,
nodeMappings: normalizeNodeMappings(project),
}));
}
/**
* Hook for fetching and managing projects.
* Automatically polls for updates every 5 seconds.
* Refetches when the tab becomes visible again.
* Provides optimistic updates for UI responsiveness.
*/
export function useProjects(): UseProjectsResult {
const [projects, setProjects] = useState<ProjectInfoWithSource[]>(() => {
const cached = readCache<ProjectInfoWithSource[]>(SWR_CACHE_KEYS.PROJECTS, { maxAgeMs: SWR_DEFAULT_MAX_AGE_MS });
if (!Array.isArray(cached)) {
return [];
}
if (cached.length > 0) {
console.info("[swr-cache] hit projects=", cached.length);
}
return normalizeProjects(cached);
});
const [loading, setLoading] = useState(() => projects.length === 0);
const [error, setError] = useState<string | null>(null);
const lastVisibilityRefreshRef = useRef<number>(0);
const projectsRef = useRef(projects);
const visibilitySuspension = useTabVisibilitySuspension();
useEffect(() => {
projectsRef.current = projects;
}, [projects]);
const shouldSuppressVisibilityResumeError = useCallback((errorMessage: string): boolean => {
return projectsRef.current.length > 0 && isVisibilityResumeError(errorMessage, visibilitySuspension.wasRecentlyHidden());
}, [visibilitySuspension]);
const refresh = useCallback(async () => {
try {
setError(null);
const data = await fetchProjectsAcrossNodes();
const normalizedData = normalizeProjects(data);
setProjects(normalizedData);
writeCache(SWR_CACHE_KEYS.PROJECTS, normalizedData);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : "Failed to fetch projects";
if (!shouldSuppressVisibilityResumeError(errorMessage)) {
setError(errorMessage);
}
// Don't clear existing projects on error - keep showing stale data
}
}, [shouldSuppressVisibilityResumeError]);
// Initial fetch and visibility change handler
useEffect(() => {
let cancelled = false;
async function load() {
const hadCachedProjects = projects.length > 0;
if (!hadCachedProjects) {
setLoading(true);
}
const t0 = performance.now();
try {
const data = await fetchProjectsAcrossNodes();
const normalizedData = normalizeProjects(data);
const elapsed = Math.round(performance.now() - t0);
console.log(`[useProjects] initial fetchProjectsAcrossNodes took ${elapsed}ms (${normalizedData.length} projects)`);
if (!cancelled) {
setProjects(normalizedData);
setError(null);
writeCache(SWR_CACHE_KEYS.PROJECTS, normalizedData);
}
} catch (err) {
const elapsed = Math.round(performance.now() - t0);
const errorMessage = err instanceof Error ? err.message : "Failed to fetch projects";
console.warn(`[useProjects] initial fetch failed after ${elapsed}ms: ${errorMessage}`);
if (!cancelled && !shouldSuppressVisibilityResumeError(errorMessage)) {
setError(errorMessage);
}
} finally {
if (!cancelled) {
setLoading(false);
}
}
}
void load();
const handleVisibilityChange = () => {
if (document.visibilityState !== "visible") {
return;
}
const now = Date.now();
const timeSinceLastRefresh = now - lastVisibilityRefreshRef.current;
if (timeSinceLastRefresh < VISIBILITY_REFRESH_DEBOUNCE_MS) {
recordResumeEvent({
view: "useProjects",
trigger: "visibility",
projectId: undefined,
replayAttempted: false,
reason: "debounce-skipped",
detail: { timeSinceLastRefreshMs: timeSinceLastRefresh },
});
return;
}
lastVisibilityRefreshRef.current = now;
recordResumeEvent({
view: "useProjects",
trigger: "visibility",
projectId: undefined,
replayAttempted: false,
reason: "debounced-refresh",
});
void refresh();
};
document.addEventListener("visibilitychange", handleVisibilityChange);
return () => {
cancelled = true;
document.removeEventListener("visibilitychange", handleVisibilityChange);
};
}, [projects.length, refresh, shouldSuppressVisibilityResumeError]);
/*
FNXC:MobileTabRetention 2026-07-26-16:05:
This 5s poll was NOT visibility-gated, despite the mobile tab-retention work claiming every polling loop
was. The `visibilitychange` listener above only ADDS a refresh on the visible edge; it never cleared the
interval, so a backgrounded tab kept issuing a project-list fetch every 5 seconds. `useProjects` is mounted
unconditionally for the whole session (App.tsx), so this single loop was enough to keep the page permanently
non-idle — the exact signal iOS Safari / iOS PWA / Chrome Android use to discard the tab and force the
white-splash reload the whole effort was fixing.
`refreshOnVisible: false` because the debounced, instrumented listener above already owns the visible-edge
refresh; letting the helper refresh too would fetch twice on one edge.
*/
useVisibilityAwarePoll(refresh, POLL_INTERVAL_MS, { enabled: !loading, refreshOnVisible: false });
const register = useCallback(async (input: ProjectCreateInput): Promise<ProjectInfo> => {
const project = await registerProject(input);
// Optimistically add to list
setProjects((prev) => [...prev, project]);
return project;
}, []);
const update = useCallback(async (id: string, updates: Partial<ProjectInfo>): Promise<ProjectInfo> => {
const project = await updateProject(id, updates);
// Optimistically update in list
setProjects((prev) =>
prev.map((p) => (p.id === id ? project : p))
);
return project;
}, []);
const unregister = useCallback(async (id: string): Promise<void> => {
await unregisterProject(id);
// Optimistically remove from list
setProjects((prev) => {
const nextProjects = prev.filter((p) => p.id !== id);
writeCache(SWR_CACHE_KEYS.PROJECTS, nextProjects);
return nextProjects;
});
clearCache(`${SWR_CACHE_KEYS.TASKS_PREFIX}${id}`);
}, []);
return {
projects,
loading,
error,
refresh,
register,
update,
unregister,
};
}