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

useNativeReviewDiffHighlighting.ts

140 lines4.3 KB

1import { useCallback, useEffect, useRef, useState } from "react";
2​
3import {
4 highlightNativeReviewDiffVisibleRows,
5 type NativeReviewDiffHighlightEngine,
6 type NativeReviewDiffHighlightScheme,
7} from "../diffs/nativeReviewDiffHighlighter";
8import type { NativeReviewDiffRow } from "../diffs/nativeReviewDiffSurface";
9import type { NativeReviewDiffFile } from "../diffs/nativeReviewDiffTypes";
10​
11interface NativeReviewVisibleRange {
12 readonly firstRowIndex: number;
13 readonly lastRowIndex: number;
15​
16function createEmptyTokenPatch(resetKey: string): string {
17 return JSON.stringify({ resetKey, tokensByRowId: {} });
19​
20function isReviewDiffDebugLoggingEnabled(): boolean {
21 return typeof __DEV__ !== "undefined" ? __DEV__ : false;
23​
24function logReviewDiffDiagnostic(message: string, details?: Record<string, unknown>): void {
25 if (!isReviewDiffDebugLoggingEnabled()) {
26 return;
27 }
28​
29 if (details) {
30 console.log(`[review-sheet] ${message}`, details);
31 return;
32 }
33​
34 console.log(`[review-sheet] ${message}`);
36​
37export function useNativeReviewDiffHighlighting(input: {
38 readonly files: ReadonlyArray<NativeReviewDiffFile>;
39 readonly rows: ReadonlyArray<NativeReviewDiffRow>;
40 readonly scheme: NativeReviewDiffHighlightScheme;
41 readonly resetKey: string;
42 readonly enabled: boolean;
43}) {
44 const { enabled, files, resetKey, rows, scheme } = input;
45 const highlightedRowIdsRef = useRef<Set<string>>(new Set());
46 const visibleRangeRef = useRef<NativeReviewVisibleRange>({
47 firstRowIndex: 0,
48 lastRowIndex: 80,
49 });
50 const visibleChunkIndexRef = useRef(0);
51 const [tokensPatchJson, setTokensPatchJson] = useState(() => createEmptyTokenPatch(resetKey));
52 const [visibleHighlightRequest, setVisibleHighlightRequest] = useState(0);
53​
54 useEffect(() => {
55 highlightedRowIdsRef.current = new Set();
56 visibleChunkIndexRef.current = 0;
57 visibleRangeRef.current = { firstRowIndex: 0, lastRowIndex: 80 };
58 setTokensPatchJson(createEmptyTokenPatch(resetKey));
59 if (enabled && rows.length > 0) {
60 setVisibleHighlightRequest((request) => request + 1);
61 }
62 }, [enabled, resetKey, rows.length]);
63​
64 useEffect(() => {
65 if (!enabled || rows.length === 0) {
66 return;
67 }
68​
69 const abortController = new AbortController();
70 const requestRange = visibleRangeRef.current;
71 const engine: NativeReviewDiffHighlightEngine = "native";
72​
73 void (async () => {
74 try {
75 const result = await highlightNativeReviewDiffVisibleRows({
76 files,
77 rows,
78 scheme,
79 engine,
80 firstRowIndex: requestRange.firstRowIndex,
81 lastRowIndex: requestRange.lastRowIndex,
82 alreadyHighlightedRowIds: highlightedRowIdsRef.current,
83 signal: abortController.signal,
84 });
85​
86 if (abortController.signal.aborted || result.rowCount === 0) {
87 return;
88 }
89​
90 for (const rowId of Object.keys(result.tokensByRowId)) {
91 highlightedRowIdsRef.current.add(rowId);
92 }
93​
94 const chunkIndex = visibleChunkIndexRef.current;
95 visibleChunkIndexRef.current += 1;
96 setTokensPatchJson(
97 JSON.stringify({
98 resetKey,
99 chunkIndex,
100 fileId: "visible",
101 filePath: "visible rows",
102 language: "diff",
103 lineCount: result.rowCount,
104 durationMs: result.durationMs,
105 tokensByRowId: result.tokensByRowId,
106 }),
107 );
108 } catch (error) {
109 if (!abortController.signal.aborted) {
110 logReviewDiffDiagnostic("native visible highlight failed", {
111 error,
112 resetKey,
113 scheme,
114 firstRowIndex: requestRange.firstRowIndex,
115 lastRowIndex: requestRange.lastRowIndex,
116 });
117 }
118 }
119 })();
120​
121 return () => abortController.abort();
122 }, [enabled, files, resetKey, rows, scheme, visibleHighlightRequest]);
123​
124 const updateVisibleRange = useCallback((nextRange: NativeReviewVisibleRange) => {
125 const previousRange = visibleRangeRef.current;
126 const movedRows =
127 Math.abs(nextRange.firstRowIndex - previousRange.firstRowIndex) +
128 Math.abs(nextRange.lastRowIndex - previousRange.lastRowIndex);
129​
130 visibleRangeRef.current = nextRange;
131 if (movedRows >= 20) {
132 setVisibleHighlightRequest((request) => request + 1);
133 }
134 }, []);
135​
136 return {
137 tokensPatchJson,
138 updateVisibleRange,
139 };