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