Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestDiff.logic.ts

51 lines1.9 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import type { PullRequestDiffSide } 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 );
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;
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;