Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import type { PullRequestDiffSide } from "@t3tools/contracts";
3
4/**
5 * Whether a conversation's line is really in this file's hunks.
6 *
7 * A thread naming a file is not the same as a thread the diff can show: its line may have moved
8 * out of the change, or sit in a hunk the host withheld. Pinning it anyway would put the remark
9 * against whatever code now occupies that line number, and silently dropping it would lose the
10 * conversation, so the answer decides which of the two lists it belongs in.
11 */
12export function isLineInFileDiff(
13 file: FileDiffMetadata,
14 side: PullRequestDiffSide,
15 line: number,
16): boolean {
17 return file.hunks.some((hunk) =>
18 side === "left"
19 ? line >= hunk.deletionStart && line < hunk.deletionStart + hunk.deletionCount
20 : line >= hunk.additionStart && line < hunk.additionStart + hunk.additionCount,
21 );
22}
23
24/** What the toolbar last asked of every file at once, null being the reader asking nothing yet. */
25export type DiffFoldOverride = "expanded" | "folded" | null;
26
27/**
28 * Whether a file is drawn folded.
29 *
30 * A diff arrives a slice at a time, so the reader's own choices are kept as the difference from
31 * what the toolbar last said rather than as the set of folded files: a file that has not loaded
32 * yet cannot be in a set, and would otherwise land expanded moments after the reader folded
33 * everything. The caller supplies the saved default until the toolbar overrides it; individual
34 * files can still be toggled independently.
35 */
36export function isFileDiffCollapsed(
37 fileKey: string,
38 foldOverride: DiffFoldOverride,
39 toggledFileKeys: ReadonlySet<string>,
40): boolean {
41 const foldedByDefault = foldOverride === "folded";
42 return toggledFileKeys.has(fileKey) ? !foldedByDefault : foldedByDefault;
43}
44
45/**
46 * The reader's fold choices after a file was ticked off, or put back.
47 *
48 * Clearing a file puts it away and un-clearing brings it back, so the tick moves the fold as if
49 * the reader had pressed the chevron themselves, which keeps folding a difference from what the
50 * toolbar last asked, and so keeps "collapse all" from ticking anything off.
51 */
52export function toggleFileDiffFoldForViewed(
53 fileKey: string,
54 viewed: boolean,
55 foldOverride: DiffFoldOverride,
56 toggledFileKeys: ReadonlySet<string>,
57): ReadonlySet<string> {
58 if (isFileDiffCollapsed(fileKey, foldOverride, toggledFileKeys) === viewed)
59 return toggledFileKeys;
60 const next = new Set(toggledFileKeys);
61 if (next.has(fileKey)) next.delete(fileKey);
62 else next.add(fileKey);
63 return next;
64}
65
66/**
67 * Folds, once, the files the reader had already ticked off before this mount of the Code tab,
68 * the way their ticks would have. The fold set is local to the tab and starts empty on every
69 * mount, while the ticks live on the host; without this a ticked file comes back open each time
70 * the reader returns to Code. Every file seen is remembered, so a file ticked or unfolded later
71 * is left to the reader's own choices; both sets come back unchanged when there is nothing to do.
72 */
73export function foldViewedFilesOnce(
74 files: ReadonlyArray<{
75 readonly fileKey: string;
76 /** Survives what `fileKey` does not, such as a whitespace toggle re-keying every diff. */
77 readonly path: string;
78 readonly viewed: boolean;
79 }>,
80 seenPaths: ReadonlySet<string>,
81 foldOverride: DiffFoldOverride,
82 toggledFileKeys: ReadonlySet<string>,
83): {
84 readonly seenPaths: ReadonlySet<string>;
85 readonly toggledFileKeys: ReadonlySet<string>;
86} {
87 const unseen = files.filter((file) => !seenPaths.has(file.path));
88 if (unseen.length === 0) return { seenPaths, toggledFileKeys };
89 const seen = new Set(seenPaths);
90 let toggled = toggledFileKeys;
91 for (const file of unseen) {
92 seen.add(file.path);
93 if (file.viewed)
94 toggled = toggleFileDiffFoldForViewed(file.fileKey, true, foldOverride, toggled);
95 }
96 return { seenPaths: seen, toggledFileKeys: toggled };
97}