Open History to see the last commit
1import { useCallback, useMemo, useState } from "react";
2import type { NativeSyntheticEvent } from "react-native";
3
4import { type NativeReviewDiffHighlightScheme } from "../diffs/nativeReviewDiffHighlighter";
5import { createNativeReviewDiffTheme, type NativeReviewDiffData } from "./nativeReviewDiffAdapter";
6import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface";
7import { useNativeReviewDiffHighlighting } from "./useNativeReviewDiffHighlighting";
8import { buildNativeReviewTokensResetKey } from "./reviewDiffBridgeKeys";
9
10export { buildNativeReviewTokensResetKey, hashReviewDiffKey } from "./reviewDiffBridgeKeys";
11
12export function useNativeReviewDiffBridge(input: {
13 readonly threadKey: string | null;
14 readonly sectionId: string | null;
15 readonly diff: string | null | undefined;
16 readonly data: NativeReviewDiffData;
17 readonly scheme: NativeReviewDiffHighlightScheme;
18 readonly collapsedFileIds: ReadonlyArray<string>;
19 readonly viewedFileIds: ReadonlyArray<string>;
20 readonly selectedRowIds: ReadonlyArray<string>;
21 readonly canHighlight: boolean;
22}) {
23 const {
24 canHighlight,
25 collapsedFileIds,
26 data,
27 diff,
28 scheme,
29 sectionId,
30 selectedRowIds,
31 threadKey,
32 viewedFileIds,
33 } = input;
34 const { nativeReviewDiffStyle } = useAppearanceCodeSurface();
35 const [collapsedCommentIds, setCollapsedCommentIds] = useState<ReadonlySet<string>>(
36 () => new Set(),
37 );
38
39 const theme = useMemo(() => createNativeReviewDiffTheme(scheme), [scheme]);
40 const rowsJson = useMemo(() => JSON.stringify(data.rows), [data.rows]);
41 const collapsedFileIdsJson = useMemo(() => JSON.stringify(collapsedFileIds), [collapsedFileIds]);
42 const viewedFileIdsJson = useMemo(() => JSON.stringify(viewedFileIds), [viewedFileIds]);
43 const selectedRowIdsJson = useMemo(() => JSON.stringify(selectedRowIds), [selectedRowIds]);
44 const collapsedCommentIdsJson = useMemo(
45 () => JSON.stringify(Array.from(collapsedCommentIds)),
46 [collapsedCommentIds],
47 );
48 const themeJson = useMemo(() => JSON.stringify(theme), [theme]);
49 const styleJson = useMemo(() => JSON.stringify(nativeReviewDiffStyle), [nativeReviewDiffStyle]);
50 const tokensResetKey = useMemo(
51 () =>
52 buildNativeReviewTokensResetKey({
53 threadKey,
54 sectionId,
55 scheme,
56 diff,
57 fileCount: data.files.length,
58 rowCount: data.rows.length,
59 }),
60 [data.files.length, data.rows.length, diff, scheme, sectionId, threadKey],
61 );
62 const { tokensPatchJson, updateVisibleRange } = useNativeReviewDiffHighlighting({
63 files: data.files,
64 rows: data.rows,
65 scheme,
66 resetKey: tokensResetKey,
67 enabled: canHighlight,
68 });
69
70 const onDebug = useCallback(
71 (event: NativeSyntheticEvent<Record<string, unknown>>) => {
72 const payload = event.nativeEvent;
73 const message = payload.message;
74 if (
75 (message === "draw-metrics" || message === "visible-range") &&
76 typeof payload.firstRowIndex === "number" &&
77 typeof payload.lastRowIndex === "number"
78 ) {
79 updateVisibleRange({
80 firstRowIndex: payload.firstRowIndex,
81 lastRowIndex: payload.lastRowIndex,
82 });
83 }
84 },
85 [updateVisibleRange],
86 );
87
88 const onToggleComment = useCallback(
89 (event: NativeSyntheticEvent<{ readonly commentId?: string }>) => {
90 const { commentId } = event.nativeEvent;
91 if (!commentId) {
92 return;
93 }
94
95 setCollapsedCommentIds((current) => {
96 const next = new Set(current);
97 if (next.has(commentId)) {
98 next.delete(commentId);
99 } else {
100 next.add(commentId);
101 }
102 return next;
103 });
104 },
105 [],
106 );
107
108 return {
109 theme,
110 rowsJson,
111 collapsedFileIdsJson,
112 collapsedCommentIdsJson,
113 viewedFileIdsJson,
114 selectedRowIdsJson,
115 themeJson,
116 styleJson,
117 tokensPatchJson,
118 tokensResetKey,
119 onDebug,
120 onToggleComment,
121 };
122}