diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx index 2f9f76c941da8..9d91faf7a456b 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx @@ -192,7 +192,6 @@ const detail: PullRequestDetailView = { }; function setup({ paged }: { paged: boolean }) { - window.localStorage.removeItem("t3code.diffHideTestFiles"); __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); mocks.paged = paged; mocks.shortPages = false; @@ -201,6 +200,7 @@ function setup({ paged }: { paged: boolean }) { mocks.diffCommits = []; mocks.viewedPaths = null; mocks.viewedListeners.clear(); + localStorage.clear(); } const tab = ( @@ -208,12 +208,13 @@ const tab = ( revealRequest: { readonly path: string; readonly nonce: number } | null = null, revealUrl?: string, onRevealSettled?: (url: string) => void, + tabDetail: PullRequestDetailView = detail, ) => ( {}} environmentId={EnvironmentId.make("environment")} - reference={detail} - detail={detail} + reference={tabDetail} + detail={tabDetail} selectedCommitOid={null} onSelectedCommitChange={() => {}} onRefresh={() => {}} @@ -343,7 +344,6 @@ it("pages forward to a file asked for that is not loaded yet, then scrolls to it __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: true }); using _preferences = { [Symbol.dispose]() { - window.localStorage.removeItem("t3code.diffHideTestFiles"); __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); }, }; @@ -366,6 +366,80 @@ it("pages forward to a file asked for that is not loaded yet, then scrolls to it await expectFileHeaderShown(screen, container, "late.test.ts"); }); +it("hides viewed files and shows them again", async () => { + setup({ paged: true }); + mocks.viewedPaths = new Set(["early-0.ts"]); + await using screen = await render(tab(0), { container: pagedContainer() }); + await expect.element(screen.getByText("early-0.ts", { exact: true }).first()).toBeVisible(); + const hideViewed = screen.getByRole("button", { name: "Hide viewed files" }); + + await hideViewed.click(); + + await expect.element(screen.getByText("early-0.ts", { exact: true })).not.toBeInTheDocument(); + await expect.element(screen.getByText("early-2.ts", { exact: true }).first()).toBeVisible(); + await hideViewed.click(); + await expect.element(screen.getByText("early-0.ts", { exact: true }).first()).toBeVisible(); +}); + +it("keeps the Hide viewed files toggle while its saved filter is on and nothing is viewed", async () => { + setup({ paged: true }); + mocks.viewedPaths = new Set(); + localStorage.setItem("t3code.pullRequestHideViewedFiles", "true"); + await using screen = await render(tab(0), { container: pagedContainer() }); + await screen.getByRole("button", { name: "Hide viewed files" }).click(); + + await vi.waitFor(() => + expect(localStorage.getItem("t3code.pullRequestHideViewedFiles")).toBe("false"), + ); +}); + +it("marks a Shift-clicked range viewed after the first file of it was hidden", async () => { + setup({ paged: true }); + mocks.viewedPaths = new Set(["early-0.ts"]); + const container = document.createElement("div"); + container.className = "h-[500px] w-[400px]"; + await using screen = await render(tab(0), { container }); + await screen.getByRole("button", { name: "Hide viewed files" }).click(); + await expect.element(screen.getByText("early-10.ts", { exact: true }).first()).toBeVisible(); + + await screen.getByRole("checkbox", { name: "Viewed" }).first().click(); + await expect.element(screen.getByText("early-10.ts", { exact: true })).not.toBeInTheDocument(); + await screen + .getByRole("checkbox", { name: "Viewed" }) + .nth(1) + .click({ modifiers: ["Shift"] }); + + await expect.element(screen.getByText("early-11.ts", { exact: true })).not.toBeInTheDocument(); +}); + +it("keeps conversations reachable when every file is hidden", async () => { + setup({ paged: false }); + mocks.viewedPaths = new Set(["a.ts"]); + const threadDetail: PullRequestDetailView = { + ...detail, + reviewThreads: [ + { + id: "thread", + path: "a.ts", + line: 1, + side: "right", + isResolved: false, + isOutdated: false, + comments: [], + }, + ], + }; + const container = document.createElement("div"); + container.className = "h-[500px] w-[400px]"; + await using screen = await render(tab(0, null, undefined, undefined, threadDetail), { + container, + }); + await screen.getByRole("button", { name: "Hide viewed files" }).click(); + await expect.element(screen.getByText("All files are hidden.")).toBeVisible(); + + await expect.element(screen.getByText("Conversations not on the current diff")).toBeVisible(); +}); + it("keeps a linked test file expanded when a refresh changes the rest of the diff", async () => { setup({ paged: false }); mocks.withTestFile = true; diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index d06cb1efeaacd..4d94d21560bd5 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -36,6 +36,7 @@ import { ChevronDownIcon, ChevronRightIcon, Columns2Icon, + EyeOffIcon, FlaskConicalOffIcon, FolderTreeIcon, InfoIcon, @@ -191,6 +192,7 @@ type ReviewAnnotation = DiffLineAnnotation; const COMMIT_PAGE_SIZE = 10; const PULL_REQUEST_FILE_TREE_STORAGE_KEY = "t3code.pullRequestFileTreeOpen"; +const PULL_REQUEST_HIDE_VIEWED_FILES_STORAGE_KEY = "t3code.pullRequestHideViewedFiles"; interface DiffSlice { readonly source: PullRequestDiffResult; @@ -483,6 +485,11 @@ function PullRequestCodeTab({ Schema.Boolean, ); const [hideTestFiles, setHideTestFiles] = useHideTestFiles(); + const [hideViewedFiles, setHideViewedFiles] = useLocalStorage( + PULL_REQUEST_HIDE_VIEWED_FILES_STORAGE_KEY, + false, + Schema.Boolean, + ); const [selectedLines, setSelectedLines] = useState<{ id: string; range: SelectedLineRange; @@ -775,15 +782,28 @@ function PullRequestCodeTab({ const testFileCount = loadedFiles.filter((file) => isTestChangedFile(resolveFileDiffPath(file)), ).length; - // A requested file stays visible so a jump to a hidden test still lands. - const files = hideTestFiles + // A filter never hides the file being jumped to or one holding unsent review comments. + const isPinned = (path: string) => + path === revealRequest?.path || + path === linkPath || + path === draft?.path || + pendingComments.some((comment) => comment.path === path); + const reviewFiles = hideTestFiles ? loadedFiles.filter((file) => { const path = resolveFileDiffPath(file); - return path === revealRequest?.path || path === linkPath || !isTestChangedFile(path); + return isPinned(path) || !isTestChangedFile(path); }) : loadedFiles; - const hiddenTestFileCount = loadedFiles.length - files.length; - const filePaths = files.map((file) => resolveFileDiffPath(file)); + const hiddenTestFileCount = loadedFiles.length - reviewFiles.length; + const viewedFileCount = reviewFiles.filter((file) => + isFileViewed(resolveFileDiffPath(file)), + ).length; + const files = hideViewedFiles + ? reviewFiles.filter((file) => { + const path = resolveFileDiffPath(file); + return isPinned(path) || !isFileViewed(path); + }) + : reviewFiles; const omittedFileStats = new Map( loadedSlices.flatMap((slice) => slice.omittedFileStats.map((file) => [file.path, file] as const), @@ -1023,11 +1043,14 @@ function PullRequestCodeTab({ }; const setFileViewed = (fileKey: string, viewed: boolean, extendRange: boolean) => { - const range = getFileViewedRange( - annotatedFiles, - fileKey, - extendRange ? lastViewedFileKey : null, - ); + // Include files hidden as viewed: the Shift-click anchor is usually one of them. + const rangeFiles = reviewFiles.map((file) => ({ + fileKey: buildFileDiffRenderKey(file), + foldKey: buildFileDiffFoldKey(file), + path: resolveFileDiffPath(file), + fileDiff: file, + })); + const range = getFileViewedRange(rangeFiles, fileKey, extendRange ? lastViewedFileKey : null); setLastViewedFileKey(fileKey); for (const file of range) { // A commit's diff names that commit's version of the file, not the head's. @@ -1481,10 +1504,10 @@ function PullRequestCodeTab({ )} ) : null} - {filesViewed.enabled && files.length > 0 ? ( + {filesViewed.enabled && reviewFiles.length > 0 ? ( - {filePaths.filter(isFileViewed).length} / {files.length} + {viewedFileCount} / {reviewFiles.length} {viewedFilesStore === "environment" @@ -1731,6 +1754,26 @@ function PullRequestCodeTab({ ) : null} + {viewedFileCount > 0 || hideViewedFiles ? ( + + setHideViewedFiles(Boolean(pressed))} + /> + } + > + + + + {hideViewedFiles ? gt("Show viewed files") : gt("Hide viewed files")} + + + ) : null} {fileKeys.length > 0 ? ( !placedThreadIds.has(thread.id)); + const orphanFiles = new Map(); + for (const thread of orphanThreads) { + const existing = orphanFiles.get(thread.path); + if (existing) existing.push(thread); + else orphanFiles.set(thread.path, [thread]); + } + const orphanConversations = + orphanFiles.size > 0 ? ( +
+ +

+ + + {nextCursor === null + ? gt("Conversations not on the current diff") + : gt("Conversations not on the diff loaded so far")} + + + + {orphanThreads.length} + + + {gt("{count, plural, one {{count} conversation} other {{count} conversations}}", { + count: orphanThreads.length, + })} + + +

+ +
+ {[...orphanFiles].map(([path, threads]) => ( +
+ + {path}

} + /> + {path} +
+
+ {threads.map((thread) => ( +
+ {thread.line === null ? null : ( +

+ + Line {thread.line} + +

+ )} + {renderThreadCard(thread)} +
+ ))} +
+
+ ))} +
+
+
+
+ ) : null; + if (items.length === 0 && nextCursor === null) { + const noFilesMessage = + commit === null + ? gt( + "This {term, select, mr {merge request} cr {change request} other {pull request}} has no file changes.", + { term }, + ) + : gt("This commit has no file changes."); return withToolbar( -

- {commit === null - ? gt( - "This {term, select, mr {merge request} cr {change request} other {pull request}} has no file changes.", - { term }, - ) - : gt("This commit has no file changes.")} -

, + <> + {orphanConversations} +

+ {loadedFiles.length > 0 ? gt("All files are hidden.") : noFilesMessage} +

+ , ); } @@ -1885,74 +1997,10 @@ function PullRequestCodeTab({ return withToolbar(diffLoading); } - const orphanThreads = detail.reviewThreads.filter((thread) => !placedThreadIds.has(thread.id)); - const orphanFiles = new Map(); - for (const thread of orphanThreads) { - const existing = orphanFiles.get(thread.path); - if (existing) existing.push(thread); - else orphanFiles.set(thread.path, [thread]); - } - return (
{toolbar} - {orphanFiles.size > 0 ? ( -
- -

- - - {nextCursor === null - ? gt("Conversations not on the current diff") - : gt("Conversations not on the diff loaded so far")} - - - - {orphanThreads.length} - - - {gt("{count, plural, one {{count} conversation} other {{count} conversations}}", { - count: orphanThreads.length, - })} - - -

- -
- {[...orphanFiles].map(([path, threads]) => ( -
- - {path}

- } - /> - {path} -
-
- {threads.map((thread) => ( -
- {thread.line === null ? null : ( -

- - Line {thread.line} - -

- )} - {renderThreadCard(thread)} -
- ))} -
-
- ))} -
-
-
-
- ) : null} + {orphanConversations}