TypeScript
feat(web): mark PR files viewed with shift-click (#26349)
Yash Singhff424802edd1
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsxindex f8d58cfaa02..d1b165c6214 100644--- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx@@ -106,6 +106,7 @@ import {toViewerSide,} from "./PullRequestReviewAnnotation";import {+ getFileViewedRange,isFileDiffCollapsed,isLineInFileDiff,toggleFileDiffFoldForViewed,@@ -175,7 +176,7 @@ interface FileViewedToggleState {readonly enabled: boolean;readonly isViewed: (path: string) => boolean;readonly isStale: (path: string) => boolean;- readonly setFileViewed: (fileKey: string, path: string, viewed: boolean) => void;+ readonly setFileViewed: (fileKey: string, viewed: boolean, extendRange: boolean) => void;}// Context updates viewed toggles without invalidating memoized header portals.@@ -205,7 +206,13 @@ function FileViewedToggle({<Checkboxaria-label={stale ? "Changed" : "Viewed"}checked={viewed}- onCheckedChange={(next) => viewedFiles.setFileViewed(fileKey, path, next === true)}+ onCheckedChange={(next, details) =>+ viewedFiles.setFileViewed(+ fileKey,+ next === true,+ "shiftKey" in details.event && details.event.shiftKey === true,+ )+ }/>{stale ? (<Tooltip>@@ -287,6 +294,7 @@ function PullRequestCodeTab({wordWrap: settings.wordWrap,}));const [toggledFiles, setToggledFiles] = useState<ReadonlySet<string>>(() => new Set());+ const [lastViewedFileKey, setLastViewedFileKey] = useState<string | null>(null);const [visibleCommitCount, setVisibleCommitCount] = useState(COMMIT_PAGE_SIZE);const [foldOverride, setFoldOverride] = useState<DiffFoldOverride>(null);const effectiveFoldOverride =@@ -326,6 +334,7 @@ function PullRequestCodeTab({const [stateScopeKey, setStateScopeKey] = useState(scopeKey);if (stateScopeKey !== scopeKey) {setStateScopeKey(scopeKey);+ setLastViewedFileKey(null);setDraft(null);setSelectedLines(null);setToggledFiles(new Set());@@ -689,10 +698,20 @@ function PullRequestCodeTab({return next;});- const setFileViewed = (fileKey: string, path: string, viewed: boolean) => {- setViewed(path, viewed);+ const setFileViewed = (fileKey: string, viewed: boolean, extendRange: boolean) => {+ const range = getFileViewedRange(+ annotatedFiles,+ fileKey,+ extendRange ? lastViewedFileKey : null,+ );+ setLastViewedFileKey(fileKey);+ for (const file of range) setViewed(file.path, viewed);setToggledFiles((current) =>- toggleFileDiffFoldForViewed(fileKey, viewed, effectiveFoldOverride, current),+ range.reduce(+ (next, file) =>+ toggleFileDiffFoldForViewed(file.fileKey, viewed, effectiveFoldOverride, next),+ current,+ ),);};const fileViewedToggle = {diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.tsindex 4e11e04e1d7..552fa748f07 100644--- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts+++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts@@ -2,6 +2,7 @@ import type { FileDiffMetadata } from "@pierre/diffs";import { describe, expect, it } from "vite-plus/test";import {+ getFileViewedRange,isFileDiffCollapsed,isLineInFileDiff,toggleFileDiffFoldForViewed,@@ -54,3 +55,19 @@ describe("toggleFileDiffFoldForViewed", () => {expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);});});++describe("getFileViewedRange", () => {+ const files = ["c.ts", "a.ts", "b.ts", "d.ts"].map((fileKey) => ({ fileKey }));++ it("includes both endpoints in displayed order in either direction", () => {+ expect(getFileViewedRange(files, "b.ts", "c.ts")).toEqual(files.slice(0, 3));+ expect(getFileViewedRange(files, "c.ts", "b.ts")).toEqual(files.slice(0, 3));+ expect(getFileViewedRange(files, "a.ts", "a.ts")).toEqual([files[1]]);+ });++ it("changes only the clicked file without a current anchor", () => {+ expect(getFileViewedRange(files, "a.ts", null)).toEqual([files[1]]);+ expect(getFileViewedRange(files, "a.ts", "removed.ts")).toEqual([files[1]]);+ expect(getFileViewedRange(files, "removed.ts", "a.ts")).toEqual([]);+ });+});diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.tsindex 37fbfe30e9a..f175c015143 100644--- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts+++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts@@ -39,3 +39,15 @@ export function toggleFileDiffFoldForViewed(else next.add(fileKey);return next;}++export function getFileViewedRange<T extends { readonly fileKey: string }>(+ files: ReadonlyArray<T>,+ fileKey: string,+ anchorFileKey: string | null,+): ReadonlyArray<T> {+ const index = files.findIndex((file) => file.fileKey === fileKey);+ if (index === -1) return [];+ const anchorIndex = files.findIndex((file) => file.fileKey === anchorFileKey);+ if (anchorIndex === -1) return files.slice(index, index + 1);+ return files.slice(Math.min(index, anchorIndex), Math.max(index, anchorIndex) + 1);+}