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

pullRequestDiff.logic.ts

44 lines1.9 KB

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 );
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. Folded is the starting point whatever the change's size, because laying out every
34 * file of it costs the reader the seconds before the tab is usable and buries the file they came
35 * for among the ones they did not.
36 */
37export function isFileDiffCollapsed(
38 fileKey: string,
39 foldOverride: DiffFoldOverride,
40 toggledFileKeys: ReadonlySet<string>,
41): boolean {
42 const foldedByDefault = foldOverride !== "expanded";
43 return toggledFileKeys.has(fileKey) ? !foldedByDefault : foldedByDefault;