Skip to content

Commit 95e0abf

Browse files
fix(mobile): refresh file metadata after checkpoint changes
1 parent d6a63e5 commit 95e0abf

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

‎apps/mobile/src/features/threads/useThreadFileLinks.ts‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { AuthFilesystemReadScope, type ScopedThreadRef } from "@t3tools/contracts";
2+
import {
3+
AuthFilesystemReadScope,
4+
type OrchestrationV2ThreadProjection,
5+
type ScopedThreadRef,
6+
} from "@t3tools/contracts";
37
import { formatFilePathPosition, splitFilePathPosition } from "@t3tools/shared/fileLinks";
48
import {
59
resolveMarkdownFileLinkTarget,
610
resolveThreadFilePathCandidate,
711
} from "@t3tools/shared/markdownLinks";
12+
import { isAbsolutePath, resolveWorkspaceFilePath } from "@t3tools/shared/path";
813
import { collectThreadFilePaths, createThreadFilePathIndex } from "@t3tools/shared/threadFilePaths";
914
import { Atom } from "effect/reactivity";
10-
import { createContext, useContext, useLayoutEffect, useMemo, useRef, useState } from "react";
15+
import {
16+
createContext,
17+
useContext,
18+
useEffect,
19+
useLayoutEffect,
20+
useMemo,
21+
useRef,
22+
useState,
23+
} from "react";
1124

1225
import { projectEnvironment } from "../../state/projects";
1326
import { useEnvironmentScope, usePreparedConnection } from "../../state/session";
27+
import { environmentThreadDetails } from "../../state/threads";
28+
import { useAtomCommand } from "../../state/use-atom-command";
1429
import { useThreadVisibleTurnItems } from "../../state/use-thread-detail";
1530

1631
/** Encode a literal selected path once before giving it back to markdown parsers. */
@@ -48,6 +63,50 @@ export function useThreadFileLinks(ref: ScopedThreadRef, cwd: string | null) {
4863
const enabled = canRead && connection._tag === "Some";
4964
const { environmentId, threadId } = ref;
5065
useAtomValue(projectEnvironment.fileMetadataThread(ref));
66+
const checkpoints = useAtomValue(
67+
environmentThreadDetails.threadAtom(ref),
68+
(thread) => thread?.projection.checkpoints,
69+
);
70+
const invalidate = useAtomCommand(projectEnvironment.invalidateFileMetadata, {
71+
reportFailure: false,
72+
});
73+
const previous = useRef<{
74+
environmentId: ScopedThreadRef["environmentId"];
75+
threadId: ScopedThreadRef["threadId"];
76+
cwd: string | null;
77+
checkpoints: OrchestrationV2ThreadProjection["checkpoints"];
78+
} | null>(null);
79+
useEffect(() => {
80+
if (!checkpoints) {
81+
previous.current = null;
82+
return;
83+
}
84+
const before = previous.current;
85+
previous.current = { environmentId, threadId, cwd, checkpoints };
86+
if (
87+
!before ||
88+
before.environmentId !== environmentId ||
89+
before.threadId !== threadId ||
90+
before.cwd !== cwd
91+
)
92+
return;
93+
const oldById = new Map(before.checkpoints.map((checkpoint) => [checkpoint.id, checkpoint]));
94+
const paths = new Set<string>();
95+
const addPath = (path: string) => {
96+
if (cwd !== null || isAbsolutePath(path))
97+
paths.add(cwd === null ? path : resolveWorkspaceFilePath(path, cwd));
98+
};
99+
for (const checkpoint of checkpoints) {
100+
const old = oldById.get(checkpoint.id);
101+
oldById.delete(checkpoint.id);
102+
if (old?.files === checkpoint.files && old?.status === checkpoint.status) continue;
103+
for (const file of checkpoint.files) addPath(file.path);
104+
for (const file of old?.files ?? []) addPath(file.path);
105+
}
106+
for (const checkpoint of oldById.values())
107+
for (const file of checkpoint.files) addPath(file.path);
108+
if (paths.size > 0) void invalidate({ environmentId, input: { paths: [...paths] } });
109+
}, [environmentId, threadId, cwd, checkpoints, invalidate]);
51110
const generation = useRef(0);
52111
useLayoutEffect(
53112
() => () => {

0 commit comments

Comments
 (0)