@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 70 lines 2.46 KB
Raw
1 import type { RefObject } from "react";
2
3 import {
4 isNativeReviewDiffDrawEvent,
5 type NativeReviewDiffViewHandle,
6 type NativeReviewDiffViewProps,
7 } from "../diffs/nativeReviewDiffSurface";
8
9 export 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 };
18 }
19
20 export function handlePullRequestDiffDebugEvent(
21 event: Parameters<NonNullable<NativeReviewDiffViewProps["onDebug"]>>[0],
22 input: {
23 readonly rowCount: number;
24 /** The last row native can draw: a folded final file ends at its header. */
25 readonly lastShownRowIndex: number;
26 readonly targetFileId: string | undefined;
27 readonly focusIndex: number;
28 readonly isIos: boolean;
29 readonly nativeView: RefObject<NativeReviewDiffViewHandle | null>;
30 readonly jumpStarted: () => boolean;
31 readonly setJumpStarted: (started: boolean) => void;
32 readonly onDebug: NonNullable<NativeReviewDiffViewProps["onDebug"]>;
33 readonly loadMore: () => void;
34 },
35 ) {
36 const drawn = isNativeReviewDiffDrawEvent(event.nativeEvent);
37 // Delivery outcomes and content mounts must reach the bridge even before rows decode.
38 if (!drawn) input.onDebug(event);
39 const decoded =
40 event.nativeEvent.message === "rows-decoded" && event.nativeEvent.rows === input.rowCount;
41 if (!drawn && !decoded) return;
42 // Filtering replaces the native row payload asynchronously. Old draw events
43 // must not consume the file jump or highlight the new payload's row indices.
44 if (
45 typeof event.nativeEvent.totalRows === "number" &&
46 event.nativeEvent.totalRows !== input.rowCount
47 )
48 return;
49 if (drawn) input.onDebug(event);
50 if (
51 input.targetFileId &&
52 !input.jumpStarted() &&
53 input.nativeView.current &&
54 (!input.isIos || decoded)
55 ) {
56 input.setJumpStarted(true);
57 if (input.focusIndex >= 0) return;
58 void input.nativeView.current.scrollToFile(input.targetFileId, false).catch(() => {
59 input.setJumpStarted(false);
60 });
61 return;
62 }
63 if (!drawn) return;
64 const lastRow = event.nativeEvent.lastRowIndex;
65 if (
66 typeof lastRow === "number" &&
67 lastRow >= Math.min(input.rowCount - 40, input.lastShownRowIndex)
68 )
69 input.loadMore();
70 }