Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import type { PullRequestDiffSide, PullRequestOmittedFileStat } from "@t3tools/contracts";
3
4/**
5 * A thread naming a file is not the same as a thread the diff can show: its line may have moved
6 * out of the change, or sit in a hunk the host withheld. Pinning it anyway would put the remark
7 * against whatever code now occupies that line number, and silently dropping it would lose the
8 * conversation, so the answer decides which of the two lists it belongs in.
9 */
10export function isLineInFileDiff(
11 file: FileDiffMetadata,
12 side: PullRequestDiffSide,
13 line: number,
14): boolean {
15 return file.hunks.some((hunk) =>
16 side === "left"
17 ? line >= hunk.deletionStart && line < hunk.deletionStart + hunk.deletionCount
18 : line >= hunk.additionStart && line < hunk.additionStart + hunk.additionCount,
19 );
20}
21
22export type DiffFoldOverride = "expanded" | "folded" | null;
23
24/**
25 * A diff arrives a slice at a time, so the reader's own choices are kept as the difference from
26 * what the toolbar last said rather than as the set of folded files: a file that has not loaded
27 * yet cannot be in a set, and would otherwise land expanded moments after the reader folded
28 * everything.
29 */
30export function isFileDiffCollapsed(
31 fileKey: string,
32 foldOverride: DiffFoldOverride,
33 toggledFileKeys: ReadonlySet<string>,
34): boolean {
35 const foldedByDefault = foldOverride === "folded";
36 return toggledFileKeys.has(fileKey) ? !foldedByDefault : foldedByDefault;
37}
38
39export function toggleFileDiffFoldForViewed(
40 fileKey: string,
41 viewed: boolean,
42 foldOverride: DiffFoldOverride,
43 toggledFileKeys: ReadonlySet<string>,
44): ReadonlySet<string> {
45 if (isFileDiffCollapsed(fileKey, foldOverride, toggledFileKeys) === viewed)
46 return toggledFileKeys;
47 const next = new Set(toggledFileKeys);
48 if (next.has(fileKey)) next.delete(fileKey);
49 else next.add(fileKey);
50 return next;
51}
52
53/** One answer from the host: a whole number of files, and where the next one carries on. */
54export interface DiffSlice {
55 /** What was asked for, null being the first slice. Identifies the slice among the loaded ones. */
56 readonly cursor: string | null;
57 readonly patch: string;
58 readonly truncated: boolean;
59 readonly nextCursor: string | null;
60 readonly omittedFileStats: ReadonlyArray<PullRequestOmittedFileStat>;
61}
62
63/**
64 * Folds one answer into the loaded slices. A new cursor appends. An unchanged answer keeps every
65 * slice and moves the cursor to the next loaded one, so a background refresh re-reads them in
66 * turn. A changed answer replaces its slice and drops the ones after it.
67 */
68export function reconcileDiffSlices(
69 slices: ReadonlyArray<DiffSlice>,
70 answer: DiffSlice,
71): { readonly cursor: string | null; readonly slices: ReadonlyArray<DiffSlice> } {
72 const index = slices.findIndex((slice) => slice.cursor === answer.cursor);
73 if (index === -1) return { cursor: answer.cursor, slices: [...slices, answer] };
74 const existing = slices[index];
75 if (existing !== undefined && isSameDiffSlice(existing, answer)) {
76 return { cursor: slices[index + 1]?.cursor ?? answer.cursor, slices };
77 }
78 return { cursor: answer.cursor, slices: [...slices.slice(0, index), answer] };
79}
80
81function isSameDiffSlice(existing: DiffSlice, next: DiffSlice): boolean {
82 return (
83 existing.patch === next.patch &&
84 existing.truncated === next.truncated &&
85 existing.nextCursor === next.nextCursor &&
86 existing.omittedFileStats.length === next.omittedFileStats.length &&
87 existing.omittedFileStats.every((file, index) => {
88 const refreshed = next.omittedFileStats[index];
89 return (
90 refreshed !== undefined &&
91 refreshed.path === file.path &&
92 refreshed.additions === file.additions &&
93 refreshed.deletions === file.deletions
94 );
95 })
96 );
97}