Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { OrchestrationV2ThreadProjection, ScopedThreadRef } from "@t3tools/contracts";
3import { isRepositoryCheckpointScope } from "@t3tools/client-runtime/state/thread-repositories";
4import { resolveWorkspaceFilePath } from "@t3tools/shared/path";
5import { Atom } from "effect/reactivity";
6import { createContext, useEffect, useRef, type ReactNode } from "react";
7
8import { projectEnvironment } from "../state/projects";
9import { useAtomCommand } from "../state/use-atom-command";
10
11export const FileMetadataThreadContext = createContext<ScopedThreadRef | null>(null);
12const EMPTY_RETENTION = Atom.make(undefined);
13
14/** Keeps fetched metadata across virtualized message-row unmounts. */
15export function FileMetadataThreadProvider(props: {
16 threadRef: ScopedThreadRef | null;
17 cwd: string | undefined;
18 checkpoints: OrchestrationV2ThreadProjection["checkpoints"] | undefined;
19 checkpointScopes: OrchestrationV2ThreadProjection["checkpointScopes"] | undefined;
20 children?: ReactNode;
21}) {
22 const { threadRef, cwd, checkpoints, checkpointScopes, children } = props;
23 useAtomValue(threadRef ? projectEnvironment.fileMetadataThread(threadRef) : EMPTY_RETENTION);
24 const invalidate = useAtomCommand(projectEnvironment.invalidateFileMetadata, {
25 reportFailure: false,
26 });
27 const previous = useRef<{
28 environmentId: ScopedThreadRef["environmentId"];
29 threadId: ScopedThreadRef["threadId"];
30 cwd: string;
31 checkpoints: OrchestrationV2ThreadProjection["checkpoints"];
32 } | null>(null);
33
34 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- invalidates the server file metadata cache for files whose checkpoints changed
35 useEffect(() => {
36 if (!threadRef || !cwd || !checkpoints) {
37 previous.current = null;
38 return;
39 }
40 const before = previous.current;
41 previous.current = { ...threadRef, cwd, checkpoints };
42 if (
43 !before ||
44 before.environmentId !== threadRef.environmentId ||
45 before.threadId !== threadRef.threadId ||
46 before.cwd !== cwd
47 )
48 return;
49 const oldById = new Map(before.checkpoints.map((checkpoint) => [checkpoint.id, checkpoint]));
50 // A tracked repository's files are relative to its own folder, not the thread's.
51 const repositoryCwdByScope = new Map(
52 checkpointScopes?.flatMap((scope) =>
53 isRepositoryCheckpointScope(scope) ? [[scope.id, scope.cwd] as const] : [],
54 ),
55 );
56 const paths = new Set<string>();
57 const add = (checkpoint: OrchestrationV2ThreadProjection["checkpoints"][number]) => {
58 const root = repositoryCwdByScope.get(checkpoint.scopeId) ?? cwd;
59 for (const file of checkpoint.files) paths.add(resolveWorkspaceFilePath(file.path, root));
60 };
61 for (const checkpoint of checkpoints) {
62 const old = oldById.get(checkpoint.id);
63 oldById.delete(checkpoint.id);
64 if (old?.files === checkpoint.files && old?.status === checkpoint.status) continue;
65 add(checkpoint);
66 if (old) add(old);
67 }
68 for (const checkpoint of oldById.values()) add(checkpoint);
69 if (paths.size > 0)
70 void invalidate({ environmentId: threadRef.environmentId, input: { paths: [...paths] } });
71 }, [threadRef, cwd, checkpoints, checkpointScopes, invalidate]);
72
73 return <FileMetadataThreadContext value={threadRef}>{children}</FileMetadataThreadContext>;
74}