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

pullRequestDiffDebug.ts

63 lines2.3 KB

1import type { RefObject } from "react";
2​
3import {
4 isNativeReviewDiffDrawEvent,
5 type NativeReviewDiffViewHandle,
6 type NativeReviewDiffViewProps,
7} from "../diffs/nativeReviewDiffSurface";
8​
9export function pullRequestDiffInitialPosition(
10 targetFileId: string | undefined,
11 focusIndex: number,
12): Pick<NativeReviewDiffViewProps, "initialRowIndex" | "initialFileId"> {
13 // Explicit empty values also clear a previous destination on a reused native view.
14 return {
15 initialRowIndex: focusIndex >= 0 ? Math.max(0, focusIndex - 3) : -1,
16 initialFileId: focusIndex >= 0 ? "" : (targetFileId ?? ""),
17 };
19​
20export function handlePullRequestDiffDebugEvent(
21 event: Parameters<NonNullable<NativeReviewDiffViewProps["onDebug"]>>[0],
22 input: {
23 readonly rowCount: number;
24 readonly targetFileId: string | undefined;
25 readonly focusIndex: number;
26 readonly isIos: boolean;
27 readonly nativeView: RefObject<NativeReviewDiffViewHandle | null>;
28 readonly jumpStarted: RefObject<boolean>;
29 readonly onDebug: NonNullable<NativeReviewDiffViewProps["onDebug"]>;
30 readonly loadMore: () => void;
31 },
32) {
33 const drawn = isNativeReviewDiffDrawEvent(event.nativeEvent);
34 // Delivery outcomes and content mounts must reach the bridge even before rows decode.
35 if (!drawn) input.onDebug(event);
36 const decoded =
37 event.nativeEvent.message === "rows-decoded" && event.nativeEvent.rows === input.rowCount;
38 if (!drawn && !decoded) return;
39 // Filtering replaces the native row payload asynchronously. Old draw events
40 // must not consume the file jump or highlight the new payload's row indices.
41 if (
42 typeof event.nativeEvent.totalRows === "number" &&
43 event.nativeEvent.totalRows !== input.rowCount
44 )
45 return;
46 if (drawn) input.onDebug(event);
47 if (
48 input.targetFileId &&
49 !input.jumpStarted.current &&
50 input.nativeView.current &&
51 (!input.isIos || decoded)
52 ) {
53 input.jumpStarted.current = true;
54 if (input.focusIndex >= 0) return;
55 void input.nativeView.current.scrollToFile(input.targetFileId, false).catch(() => {
56 input.jumpStarted.current = false;
57 });
58 return;
59 }
60 if (!drawn) return;
61 const lastRow = event.nativeEvent.lastRowIndex;
62 if (typeof lastRow === "number" && lastRow >= input.rowCount - 40) input.loadMore();