| 13 | |
| 14 | /** Scoped requests are independent of the whole-repository layout budget. */ |
| 15 | export function useGraphScope({ project, layout, filePath, range, fetch: fetchImpl, edgeTypes, limits }: { |
| 16 | project: string; layout?: GraphData; filePath?: string; range?: SourceFocusRange; fetch?: typeof globalThis.fetch; edgeTypes?: readonly string[]; |
| 17 | /** The render limits: a layer that grows past them stops early and is marked partial (hand test K8). */ |
| 18 | limits?: { nodes: number; edges: number }; |
| 19 | }) { |
| 20 | const cache = useRef(new Map<string, ScopedGraph>()); |
| 21 | const [chosen, setChosen] = useState<GraphScope>(); |
| 22 | const [depth, setDepth] = useState(0); |
| 23 | const [direction, setDirection] = useState<TraceDirection>('both'); |
| 24 | const [result, setResult] = useState<{ key: string; scopeKey: string; value: ScopedGraph }>(); |
| 25 | const [cachedPreview, setCachedPreview] = useState<{ key: string; value: ScopedGraph }>(); |
| 26 | const [pendingPhase, setPendingPhase] = useState<{ key: string; validating: boolean }>(); |
| 27 | const [failure, setFailure] = useState<{ key: string; message: string }>(); |
| 28 | const [progress, setProgress] = useState<{ key: string; value: ScopeProgress }>(); |
| 29 | const [reload, setReload] = useState(0); |
| 30 | const transportKey = graphNeighborhoodTransportKey(fetchImpl); |
| 31 | const readerKey = JSON.stringify([transportKey, project, filePath, range?.startLine, range?.endLine]); |
| 32 | const [readerDepth, setReaderDepth] = useState<{ key: string; depth: number }>(); |
| 33 | const actualDepth = filePath ? readerDepth?.key === readerKey ? readerDepth.depth : 1 : depth; |
| 34 | const scope = useMemo<GraphScope | undefined>(() => filePath ? { kind: 'file', path: filePath, |
| 35 | name: `${filePath.split('/').pop()}${range ? `:${range.startLine}-${range.endLine}` : ''}`, ...(range ? { range } : {}) } : chosen, |
| 36 | [filePath, range?.startLine, range?.endLine, chosen]); |
| 37 | const edgeTypesKey = graphEdgeTypesKey(edgeTypes); |
| 38 | const selectedTypes = useMemo<string[] | undefined>(() => edgeTypesKey === 'null' ? undefined : JSON.parse(edgeTypesKey) as string[], [edgeTypesKey]); |
| 39 | const actualDirection = filePath ? 'both' : direction; |
| 40 | const limitNodes = limits?.nodes, limitEdges = limits?.edges; |
| 41 | const scopeKey = JSON.stringify([transportKey, project, scope, actualDirection, edgeTypesKey, reload, limitNodes ?? null, limitEdges ?? null]); |
| 42 | const key = JSON.stringify([scopeKey, actualDepth]); |
| 43 | useEffect(() => { setChosen(undefined); setDepth(0); setDirection('both'); setResult(undefined); setFailure(undefined); }, [project]); |
| 44 | useEffect(() => { |
| 45 | if (!scope || !project) { setResult(undefined); return; } |
| 46 | const abort = new AbortController(); |
| 47 | const client = new RpcIntelligenceClient({ fetch: fetchImpl, signal: abort.signal }); |
| 48 | setPendingPhase({ key, validating: true }); |
| 49 | const warmCache = peekGraphNeighborhoodCache(project, fetchImpl); |
| 50 | let checkedGeneration: string | undefined, checked = false; |
| 51 | if (warmCache && (warmCache.size.nodes || warmCache.size.roots)) { |
| 52 | // Paint retained evidence while revalidating, never label it current yet. |
| 53 | void loadGraphScope(project, scope, actualDepth, actualDirection, layout, { |
| 54 | signal: abort.signal, edgeTypes: selectedTypes, cache: warmCache.begin(), |
| 55 | client: { queryGraph: async () => { throw new Error('Uncached neighborhood'); } }, |
| 56 | }).then(value => { |
| 57 | if (!abort.signal.aborted && (!checked || checkedGeneration === warmCache.generation)) setCachedPreview({ key, value }); |
| 58 | }).catch(() => { /* A partial cache is not a complete preview. */ }); |
| 59 | } |
| 60 | const generation = async (): Promise<string | undefined> => { |
| 61 | try { |
| 62 | const raw = await client.indexStatusPayload(project) as Record<string, unknown>; |
| 63 | return typeof raw.indexed_at === 'string' && raw.indexed_at ? raw.indexed_at : undefined; |
| 64 | } catch { return undefined; } |
| 65 | }; |
| 66 | void (async () => { |
| 67 | const before = await generation(); |
| 68 | checked = true; checkedGeneration = before; |
| 69 | abort.signal.throwIfAborted(); |
| 70 | if (warmCache?.generation !== before) setCachedPreview(undefined); |
| 71 | const neighborhood = before ? graphNeighborhoodCache(project, before, fetchImpl) : undefined; |
| 72 | // Legacy servers without a publication token never reuse cached evidence. |