Keep cached dashboard data visible while reconnecting after transient tab-resume fetch failures. - detect likely tab suspension and visibility-resume fetch errors across dashboard data hooks - suppress transient "Failed to fetch" errors when cached project and node data already exist - show a "Connecting…" executor status state instead of surfacing raw resume-time fetch errors - add dashboard tests covering visibility suspension handling and executor reconnect rendering - add a patch changeset for the published CLI package Files changed: .changeset/tame-tab-resume-fetch.md | 5 + packages/dashboard/app/App.tsx | 16 ++- .../dashboard/app/components/ExecutorStatusBar.css | 19 ++- .../dashboard/app/components/ExecutorStatusBar.tsx | 12 ++ .../__tests__/ExecutorStatusBar.test.tsx | 20 ++- .../dashboard/app/hooks/__tests__/useNodes.test.ts | 149 ++++++++++++++++++++- .../app/hooks/__tests__/useProjects.test.ts | 72 +++++++++- .../hooks/__tests__/visibilitySuspension.test.ts | 13 ++ packages/dashboard/app/hooks/useExecutorStats.ts | 15 ++- .../dashboard/app/hooks/useManagedDockerNodes.ts | 25 +++- packages/dashboard/app/hooks/useMeshState.ts | 25 +++- packages/dashboard/app/hooks/useNodes.ts | 25 +++- packages/dashboard/app/hooks/useProjectHealth.ts | 18 ++- packages/dashboard/app/hooks/useProjects.ts | 27 +++- packages/dashboard/app/hooks/useUsageData.ts | 24 +++- .../dashboard/app/hooks/visibilitySuspension.ts | 4 + 16 files changed, 435 insertions(+), 34 deletions(-) Fusion-Task-Id: FN-6197 Fusion-Task-Lineage: 834f56d8-8391-414f-8cbd-7e626c5724d0
183 lines
6.0 KiB
TypeScript
183 lines
6.0 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import type { ManagedDockerNodeInput } from "@fusion/core";
|
|
import type { ContainerStatusInfo, ManagedDockerNodeInfo } from "../api";
|
|
import {
|
|
createManagedDockerNode,
|
|
fetchDockerNodeLogs,
|
|
fetchManagedDockerNodeContainerStatus,
|
|
fetchManagedDockerNodes,
|
|
} from "../api";
|
|
import { recordResumeEvent } from "../utils/resumeInstrumentation";
|
|
import { isVisibilityResumeError, useTabVisibilitySuspension } from "./visibilitySuspension";
|
|
|
|
export interface UseManagedDockerNodesResult {
|
|
dockerNodes: ManagedDockerNodeInfo[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
refresh: () => Promise<void>;
|
|
getContainerStatus: (id: string) => Promise<ContainerStatusInfo>;
|
|
getLogs: (id: string, options?: { tail?: number }) => Promise<string>;
|
|
create: (input: ManagedDockerNodeInput) => Promise<ManagedDockerNodeInfo>;
|
|
}
|
|
|
|
const POLL_INTERVAL_MS = 15000;
|
|
const VISIBILITY_REFRESH_DEBOUNCE_MS = 1000;
|
|
|
|
export function useManagedDockerNodes(): UseManagedDockerNodesResult {
|
|
const [dockerNodes, setDockerNodes] = useState<ManagedDockerNodeInfo[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
const lastVisibilityRefreshRef = useRef<number>(0);
|
|
const dockerNodesRef = useRef(dockerNodes);
|
|
const visibilitySuspension = useTabVisibilitySuspension();
|
|
|
|
useEffect(() => {
|
|
dockerNodesRef.current = dockerNodes;
|
|
}, [dockerNodes]);
|
|
|
|
const shouldSuppressVisibilityResumeError = useCallback((errorMessage: string): boolean => {
|
|
return dockerNodesRef.current.length > 0 && isVisibilityResumeError(errorMessage, visibilitySuspension.wasRecentlyHidden());
|
|
}, [visibilitySuspension]);
|
|
|
|
const refresh = useCallback(async () => {
|
|
try {
|
|
setError(null);
|
|
const data = await fetchManagedDockerNodes();
|
|
setDockerNodes(data);
|
|
} catch (err) {
|
|
const errorMessage = err instanceof Error ? err.message : "Failed to fetch managed Docker nodes";
|
|
if (!shouldSuppressVisibilityResumeError(errorMessage)) {
|
|
setError(errorMessage);
|
|
}
|
|
}
|
|
}, [shouldSuppressVisibilityResumeError]);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
|
|
async function load() {
|
|
setLoading(true);
|
|
try {
|
|
const data = await fetchManagedDockerNodes();
|
|
if (!cancelled) {
|
|
setDockerNodes(data);
|
|
setError(null);
|
|
}
|
|
} catch (err) {
|
|
const errorMessage = err instanceof Error ? err.message : "Failed to fetch managed Docker nodes";
|
|
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: "useManagedDockerNodes",
|
|
trigger: "visibility",
|
|
projectId: undefined,
|
|
replayAttempted: false,
|
|
reason: "debounce-skipped",
|
|
detail: { timeSinceLastRefreshMs: timeSinceLastRefresh },
|
|
});
|
|
return;
|
|
}
|
|
|
|
lastVisibilityRefreshRef.current = now;
|
|
recordResumeEvent({
|
|
view: "useManagedDockerNodes",
|
|
trigger: "visibility",
|
|
projectId: undefined,
|
|
replayAttempted: false,
|
|
reason: "debounced-refresh",
|
|
});
|
|
void refresh();
|
|
};
|
|
|
|
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
return () => {
|
|
cancelled = true;
|
|
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
};
|
|
}, [refresh, shouldSuppressVisibilityResumeError]);
|
|
|
|
useEffect(() => {
|
|
if (loading) {
|
|
return;
|
|
}
|
|
|
|
intervalRef.current = setInterval(() => {
|
|
void refresh();
|
|
}, POLL_INTERVAL_MS);
|
|
|
|
return () => {
|
|
if (intervalRef.current) {
|
|
clearInterval(intervalRef.current);
|
|
intervalRef.current = null;
|
|
}
|
|
};
|
|
}, [loading, refresh]);
|
|
|
|
const getContainerStatus = useCallback(async (id: string): Promise<ContainerStatusInfo> => {
|
|
return fetchManagedDockerNodeContainerStatus(id);
|
|
}, []);
|
|
|
|
const getLogs = useCallback(async (id: string, options?: { tail?: number }): Promise<string> => {
|
|
const result = await fetchDockerNodeLogs(id, options);
|
|
return result.logs;
|
|
}, []);
|
|
|
|
const create = useCallback(async (input: ManagedDockerNodeInput): Promise<ManagedDockerNodeInfo> => {
|
|
const created = await createManagedDockerNode(input);
|
|
const normalized = {
|
|
...created,
|
|
nodeId: created.nodeId ?? undefined,
|
|
containerId: created.containerId ?? undefined,
|
|
status: created.status,
|
|
hostConfig: {
|
|
type: created.hostConfig.host || created.hostConfig.context ? "remote" : "local",
|
|
host: created.hostConfig.host,
|
|
context: created.hostConfig.context,
|
|
},
|
|
reachableUrl: created.reachableUrl ?? undefined,
|
|
volumeMounts: created.volumeMounts.map((mount) => ({
|
|
hostPath: mount.hostPath,
|
|
containerPath: mount.containerPath,
|
|
readOnly: mount.mode === "ro" ? true : undefined,
|
|
})),
|
|
persistentStorage: created.persistentStorage,
|
|
resourceSizing: {
|
|
cpuLimit: created.resourceSizing.cpus !== undefined ? String(created.resourceSizing.cpus) : undefined,
|
|
memoryLimit: created.resourceSizing.memoryMB !== undefined ? `${created.resourceSizing.memoryMB}MB` : undefined,
|
|
},
|
|
errorMessage: created.errorMessage ?? undefined,
|
|
} satisfies ManagedDockerNodeInfo;
|
|
setDockerNodes((previous) => [...previous, normalized]);
|
|
return normalized;
|
|
}, []);
|
|
|
|
return {
|
|
dockerNodes,
|
|
loading,
|
|
error,
|
|
refresh,
|
|
getContainerStatus,
|
|
getLogs,
|
|
create,
|
|
};
|
|
}
|