fix(web): preserve panel state across workspace refreshes (#8968)
Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
1 parent
8401f4d858
commit
f2a914b858
7 files changed
+202
-12
No files matched your search
@@ -30,7 +30,8 @@ import { cn } from "~/lib/utils";
|
||||
import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore";
|
||||
import { useTheme } from "../hooks/useTheme";
|
||||
import {
|
||||
buildFileDiffRenderKey,
|
||||
buildFileDiffContentVersion,
|
||||
buildFileDiffIdentityKey,
|
||||
getDiffCollapseIconClassName,
|
||||
getDiffLineStat,
|
||||
getRenderablePatch,
|
||||
@@ -300,7 +301,7 @@ export default function DiffPanel({
|
||||
const selectedGitSource = branchDiffPreview.data?.sources.find(
|
||||
(source) => source.kind === (selectedGitScope === "unstaged" ? "working-tree" : "branch-range"),
|
||||
);
|
||||
const loadDiffFiles = useMemo<FileDiffContentsLoader | undefined>(() => {
|
||||
const currentLoadDiffFiles = useMemo<FileDiffContentsLoader | undefined>(() => {
|
||||
const preview = branchDiffPreview.data;
|
||||
if (selectedTurnId !== null || !activeThread || !preview || !selectedGitSource) {
|
||||
return undefined;
|
||||
@@ -321,6 +322,13 @@ export default function DiffPanel({
|
||||
selectedGitSource,
|
||||
selectedTurnId,
|
||||
]);
|
||||
const loadDiffFilesRef = useRef(currentLoadDiffFiles);
|
||||
loadDiffFilesRef.current = currentLoadDiffFiles;
|
||||
const loadDiffFiles = useCallback<FileDiffContentsLoader>(async (fileDiff) => {
|
||||
const loader = loadDiffFilesRef.current;
|
||||
if (!loader) throw new Error("Diff file contents are unavailable for this selection.");
|
||||
return loader(fileDiff);
|
||||
}, []);
|
||||
const localBranchRefs = useEnvironmentQuery(
|
||||
selectedTurnId === null &&
|
||||
selectedGitScope === "branch" &&
|
||||
@@ -401,17 +409,19 @@ export default function DiffPanel({
|
||||
() =>
|
||||
renderableFiles.map((fileDiff) => ({
|
||||
fileDiff,
|
||||
fileKey: buildFileDiffRenderKey(fileDiff),
|
||||
fileKey: buildFileDiffIdentityKey(fileDiff),
|
||||
fileVersion: buildFileDiffContentVersion(fileDiff),
|
||||
})),
|
||||
[renderableFiles],
|
||||
);
|
||||
const codeViewFiles = useMemo(
|
||||
() =>
|
||||
renderableFileEntries.map(({ fileDiff, fileKey }) => {
|
||||
renderableFileEntries.map(({ fileDiff, fileKey, fileVersion }) => {
|
||||
return {
|
||||
fileDiff,
|
||||
filePath: resolveFileDiffPath(fileDiff),
|
||||
fileKey,
|
||||
fileVersion,
|
||||
collapsed: collapsedDiffFileKeys.has(fileKey),
|
||||
};
|
||||
}),
|
||||
@@ -952,7 +962,7 @@ export default function DiffPanel({
|
||||
preferredHighlighter: PREFERRED_HIGHLIGHTER,
|
||||
themeType: resolvedTheme as DiffThemeType,
|
||||
stickyHeaders: true,
|
||||
...(loadDiffFiles ? { loadDiffFiles } : {}),
|
||||
...(currentLoadDiffFiles ? { loadDiffFiles } : {}),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -77,6 +77,7 @@ interface AnnotatableCodeViewProps {
|
||||
fileDiff: FileDiffMetadata;
|
||||
filePath: string;
|
||||
fileKey: string;
|
||||
fileVersion: number;
|
||||
collapsed: boolean;
|
||||
}>;
|
||||
sectionId: string;
|
||||
@@ -125,7 +126,7 @@ export function AnnotatableCodeView({
|
||||
const filesByKey = useMemo(() => new Map(files.map((file) => [file.fileKey, file])), [files]);
|
||||
const items = useMemo<CodeViewDiffItem<DiffCommentAnnotationGroup>[]>(
|
||||
() =>
|
||||
files.map(({ fileDiff, filePath, fileKey, collapsed }) => {
|
||||
files.map(({ fileDiff, filePath, fileKey, fileVersion, collapsed }) => {
|
||||
const persisted = reviewComments
|
||||
.filter(
|
||||
(comment) =>
|
||||
@@ -153,7 +154,7 @@ export function AnnotatableCodeView({
|
||||
annotations,
|
||||
collapsed,
|
||||
version: fnv1a32(
|
||||
`${collapsed ? "1" : "0"}:${annotations
|
||||
`${fileVersion}:${collapsed ? "1" : "0"}:${annotations
|
||||
.flatMap((annotation) =>
|
||||
annotation.metadata.entries.map(
|
||||
(entry) => `${entry.id}:${entry.rangeLabel}:${entry.text}`,
|
||||
|
||||
@@ -22,6 +22,7 @@ import { T3_PIERRE_ICONS } from "~/pierre-icons";
|
||||
|
||||
import { createFileTreeDragMentionController } from "./fileTreeDragMention";
|
||||
import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";
|
||||
import { buildFileTreePathUpdates } from "./fileTreePathReconciliation";
|
||||
import { useProjectEntriesQuery } from "./projectFilesQueryState";
|
||||
|
||||
interface FileBrowserPanelProps {
|
||||
@@ -126,7 +127,7 @@ export default function FileBrowserPanel({
|
||||
() => entries.filter((entry) => entry.kind === "directory").map(treePath),
|
||||
[entries],
|
||||
);
|
||||
const previousTreePathsRef = useRef<readonly string[]>([]);
|
||||
const previousTreePathsRef = useRef<readonly string[] | null>(null);
|
||||
const syncingSelectionRef = useRef(false);
|
||||
const treeSelectionPathRef = useRef<string | null>(null);
|
||||
const handledRevealRef = useRef<{ path: string; revealId: number } | null>(null);
|
||||
@@ -281,11 +282,18 @@ export default function FileBrowserPanel({
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (entriesQuery.data === null) return;
|
||||
if (previousTreePathsRef.current === treePaths) return;
|
||||
entryKindsRef.current = entryKinds;
|
||||
const previousTreePaths = previousTreePathsRef.current;
|
||||
previousTreePathsRef.current = treePaths;
|
||||
model.resetPaths(treePaths);
|
||||
}, [entryKinds, model, treePaths]);
|
||||
if (previousTreePaths === null) {
|
||||
model.resetPaths(treePaths);
|
||||
return;
|
||||
}
|
||||
const updates = buildFileTreePathUpdates(previousTreePaths, treePaths);
|
||||
if (updates.length > 0) model.batch(updates);
|
||||
}, [entriesQuery.data, entryKinds, model, treePaths]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedPath) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { buildFileTreePathUpdates } from "./fileTreePathReconciliation";
|
||||
|
||||
describe("buildFileTreePathUpdates", () => {
|
||||
it("updates only paths that changed", () => {
|
||||
expect(
|
||||
buildFileTreePathUpdates(
|
||||
["src/", "src/kept.ts", "src/removed.ts"],
|
||||
["src/", "src/kept.ts", "src/added.ts"],
|
||||
),
|
||||
).toEqual([
|
||||
{ type: "remove", path: "src/removed.ts" },
|
||||
{ type: "add", path: "src/added.ts" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("removes a missing subtree with one recursive update", () => {
|
||||
expect(
|
||||
buildFileTreePathUpdates(
|
||||
["src/", "src/feature/", "src/feature/index.ts", "src/kept.ts"],
|
||||
["src/", "src/kept.ts"],
|
||||
),
|
||||
).toEqual([{ type: "remove", path: "src/feature/", recursive: true }]);
|
||||
});
|
||||
|
||||
it("does nothing when a refresh returns the same tree", () => {
|
||||
const paths = ["src/", "src/index.ts"];
|
||||
expect(buildFileTreePathUpdates(paths, [...paths])).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { FileTreeBatchOperation } from "@pierre/trees";
|
||||
|
||||
function pathDepth(path: string): number {
|
||||
return path.split("/").filter(Boolean).length;
|
||||
}
|
||||
|
||||
export function buildFileTreePathUpdates(
|
||||
previousPaths: readonly string[],
|
||||
nextPaths: readonly string[],
|
||||
): FileTreeBatchOperation[] {
|
||||
const previous = new Set(previousPaths);
|
||||
const next = new Set(nextPaths);
|
||||
const removedDirectoryRoots: string[] = [];
|
||||
const updates: FileTreeBatchOperation[] = [];
|
||||
|
||||
const removedPaths = previousPaths
|
||||
.filter((path) => !next.has(path))
|
||||
.toSorted((left, right) => pathDepth(left) - pathDepth(right));
|
||||
for (const path of removedPaths) {
|
||||
if (removedDirectoryRoots.some((directory) => path.startsWith(directory))) continue;
|
||||
const recursive = path.endsWith("/");
|
||||
updates.push({ type: "remove", path, ...(recursive ? { recursive: true } : {}) });
|
||||
if (recursive) removedDirectoryRoots.push(path);
|
||||
}
|
||||
|
||||
const addedPaths = nextPaths
|
||||
.filter((path) => !previous.has(path))
|
||||
.toSorted((left, right) => pathDepth(left) - pathDepth(right));
|
||||
for (const path of addedPaths) updates.push({ type: "add", path });
|
||||
|
||||
return updates;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import {
|
||||
buildFileDiffContentVersion,
|
||||
buildFileDiffIdentityKey,
|
||||
buildFileDiffRenderKey,
|
||||
buildPatchCacheKey,
|
||||
getDiffLineStat,
|
||||
@@ -82,8 +84,8 @@ describe("getRenderablePatch", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildFileDiffRenderKey", () => {
|
||||
it("keeps file identity stable when Pierre hydrates a partial diff", () => {
|
||||
describe("diff file reconciliation", () => {
|
||||
it("keeps Pierre's render key stable when a partial diff hydrates", () => {
|
||||
const patch = [
|
||||
"diff --git a/example.ts b/example.ts",
|
||||
"--- a/example.ts",
|
||||
@@ -104,6 +106,48 @@ describe("buildFileDiffRenderKey", () => {
|
||||
|
||||
expect(buildFileDiffRenderKey(file)).toBe(key);
|
||||
});
|
||||
|
||||
it("keeps identities stable and versions local to the changed file", () => {
|
||||
const patch = (secondLine: string) =>
|
||||
[
|
||||
"diff --git a/unchanged.ts b/unchanged.ts",
|
||||
"--- a/unchanged.ts",
|
||||
"+++ b/unchanged.ts",
|
||||
"@@ -1 +1 @@",
|
||||
"-before",
|
||||
"+after",
|
||||
"diff --git a/changed.ts b/changed.ts",
|
||||
"--- a/changed.ts",
|
||||
"+++ b/changed.ts",
|
||||
"@@ -1 +1 @@",
|
||||
"-old",
|
||||
`+${secondLine}`,
|
||||
].join("\n");
|
||||
const before = getRenderablePatch(patch("new"), "before");
|
||||
const after = getRenderablePatch(patch("newer"), "after");
|
||||
expect(before?.kind).toBe("files");
|
||||
expect(after?.kind).toBe("files");
|
||||
if (before?.kind !== "files" || after?.kind !== "files") return;
|
||||
|
||||
const [beforeUnchanged, beforeChanged] = before.files;
|
||||
const [afterUnchanged, afterChanged] = after.files;
|
||||
expect(beforeUnchanged).toBeDefined();
|
||||
expect(beforeChanged).toBeDefined();
|
||||
expect(afterUnchanged).toBeDefined();
|
||||
expect(afterChanged).toBeDefined();
|
||||
if (!beforeUnchanged || !beforeChanged || !afterUnchanged || !afterChanged) return;
|
||||
|
||||
expect(buildFileDiffIdentityKey(afterUnchanged)).toBe(
|
||||
buildFileDiffIdentityKey(beforeUnchanged),
|
||||
);
|
||||
expect(buildFileDiffIdentityKey(afterChanged)).toBe(buildFileDiffIdentityKey(beforeChanged));
|
||||
expect(buildFileDiffContentVersion(afterUnchanged)).toBe(
|
||||
buildFileDiffContentVersion(beforeUnchanged),
|
||||
);
|
||||
expect(buildFileDiffContentVersion(afterChanged)).not.toBe(
|
||||
buildFileDiffContentVersion(beforeChanged),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getDiffLineStat", () => {
|
||||
|
||||
@@ -163,6 +163,10 @@ export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string
|
||||
return raw;
|
||||
}
|
||||
|
||||
export function buildFileDiffIdentityKey(fileDiff: FileDiffMetadata): string {
|
||||
return `${resolveFileDiffPreviousPath(fileDiff)}\u0000${resolveFileDiffPath(fileDiff)}`;
|
||||
}
|
||||
|
||||
export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
|
||||
const cacheKey = fileDiff.cacheKey;
|
||||
if (!cacheKey) return `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
|
||||
@@ -170,6 +174,66 @@ export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
|
||||
return cacheKey.endsWith(":hydrated") ? cacheKey.slice(0, -":hydrated".length) : cacheKey;
|
||||
}
|
||||
|
||||
function hashFileDiffPart(hash: number, value: string | number | boolean | undefined): number {
|
||||
const serialized = value === undefined ? "undefined" : String(value);
|
||||
const withLength = fnv1a32(`${typeof value}:${serialized.length}:`, hash);
|
||||
return fnv1a32(serialized, withLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Content-only version for CodeView reconciliation. Pierre's cache key includes
|
||||
* the whole patch, so using it here would repaint every file when one changes.
|
||||
*/
|
||||
export function buildFileDiffContentVersion(fileDiff: FileDiffMetadata): number {
|
||||
let hash = FNV_OFFSET_BASIS_32;
|
||||
const append = (value: string | number | boolean | undefined) => {
|
||||
hash = hashFileDiffPart(hash, value);
|
||||
};
|
||||
|
||||
append(fileDiff.name);
|
||||
append(fileDiff.prevName);
|
||||
append(fileDiff.lang);
|
||||
append(fileDiff.newObjectId);
|
||||
append(fileDiff.prevObjectId);
|
||||
append(fileDiff.mode);
|
||||
append(fileDiff.prevMode);
|
||||
append(fileDiff.type);
|
||||
append(fileDiff.isPartial);
|
||||
append(fileDiff.splitLineCount);
|
||||
append(fileDiff.unifiedLineCount);
|
||||
|
||||
for (const line of fileDiff.additionLines) append(line);
|
||||
for (const line of fileDiff.deletionLines) append(line);
|
||||
for (const hunk of fileDiff.hunks) {
|
||||
append(hunk.collapsedBefore);
|
||||
append(hunk.additionStart);
|
||||
append(hunk.additionCount);
|
||||
append(hunk.additionLines);
|
||||
append(hunk.additionLineIndex);
|
||||
append(hunk.deletionStart);
|
||||
append(hunk.deletionCount);
|
||||
append(hunk.deletionLines);
|
||||
append(hunk.deletionLineIndex);
|
||||
append(hunk.hunkContext);
|
||||
append(hunk.hunkSpecs);
|
||||
append(hunk.splitLineStart);
|
||||
append(hunk.splitLineCount);
|
||||
append(hunk.unifiedLineStart);
|
||||
append(hunk.unifiedLineCount);
|
||||
append(hunk.noEOFCRAdditions);
|
||||
append(hunk.noEOFCRDeletions);
|
||||
for (const content of hunk.hunkContent) {
|
||||
append(content.type);
|
||||
append(content.additionLineIndex);
|
||||
append(content.deletionLineIndex);
|
||||
append(content.type === "change" ? content.additions : content.lines);
|
||||
append(content.type === "change" ? content.deletions : undefined);
|
||||
}
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
export function getDiffCollapseIconClassName(fileDiff: FileDiffMetadata): string {
|
||||
switch (fileDiff.type) {
|
||||
case "new":
|
||||
|
||||
Reference in new issue
Block a user