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

useNativeReviewDiffBridge.ts

152 lines4.4 KB

1import { useCallback, useMemo, useState } from "react";
2import type { NativeSyntheticEvent } from "react-native";
3​
4import { type NativeReviewDiffHighlightScheme } from "../diffs/nativeReviewDiffHighlighter";
5import {
6 createNativeReviewDiffTheme,
7 NATIVE_REVIEW_DIFF_STYLE,
8 type NativeReviewDiffData,
9} from "./nativeReviewDiffAdapter";
10import { useNativeReviewDiffHighlighting } from "./useNativeReviewDiffHighlighting";
11​
12export function hashReviewDiffKey(diff: string | null | undefined): string {
13 if (!diff) {
14 return "empty";
15 }
16​
17 let hash = 5381;
18 for (let index = 0; index < diff.length; index += 1) {
19 hash = (hash * 33) ^ diff.charCodeAt(index);
20 }
21​
22 return `${diff.length}:${(hash >>> 0).toString(36)}`;
24​
25export function buildNativeReviewTokensResetKey(input: {
26 readonly threadKey: string | null;
27 readonly sectionId: string | null;
28 readonly scheme: NativeReviewDiffHighlightScheme;
29 readonly diff: string | null | undefined;
30 readonly fileCount: number;
31 readonly rowCount: number;
32}): string {
33 return [
34 input.threadKey ?? "none",
35 input.sectionId ?? "none",
36 input.scheme,
37 hashReviewDiffKey(input.diff),
38 input.fileCount,
39 input.rowCount,
40 ].join(":");
42​
43export function useNativeReviewDiffBridge(input: {
44 readonly threadKey: string | null;
45 readonly sectionId: string | null;
46 readonly diff: string | null | undefined;
47 readonly data: NativeReviewDiffData;
48 readonly scheme: NativeReviewDiffHighlightScheme;
49 readonly collapsedFileIds: ReadonlyArray<string>;
50 readonly viewedFileIds: ReadonlyArray<string>;
51 readonly selectedRowIds: ReadonlyArray<string>;
52 readonly canHighlight: boolean;
53}) {
54 const {
55 canHighlight,
56 collapsedFileIds,
57 data,
58 diff,
59 scheme,
60 sectionId,
61 selectedRowIds,
62 threadKey,
63 viewedFileIds,
64 } = input;
65 const [collapsedCommentIds, setCollapsedCommentIds] = useState<ReadonlySet<string>>(
66 () => new Set(),
67 );
68​
69 const theme = useMemo(() => createNativeReviewDiffTheme(scheme), [scheme]);
70 const rowsJson = useMemo(() => JSON.stringify(data.rows), [data.rows]);
71 const collapsedFileIdsJson = useMemo(() => JSON.stringify(collapsedFileIds), [collapsedFileIds]);
72 const viewedFileIdsJson = useMemo(() => JSON.stringify(viewedFileIds), [viewedFileIds]);
73 const selectedRowIdsJson = useMemo(() => JSON.stringify(selectedRowIds), [selectedRowIds]);
74 const collapsedCommentIdsJson = useMemo(
75 () => JSON.stringify(Array.from(collapsedCommentIds)),
76 [collapsedCommentIds],
77 );
78 const themeJson = useMemo(() => JSON.stringify(theme), [theme]);
79 const styleJson = useMemo(() => JSON.stringify(NATIVE_REVIEW_DIFF_STYLE), []);
80 const tokensResetKey = useMemo(
81 () =>
82 buildNativeReviewTokensResetKey({
83 threadKey,
84 sectionId,
85 scheme,
86 diff,
87 fileCount: data.files.length,
88 rowCount: data.rows.length,
89 }),
90 [data.files.length, data.rows.length, diff, scheme, sectionId, threadKey],
91 );
92 const { tokensPatchJson, updateVisibleRange } = useNativeReviewDiffHighlighting({
93 files: data.files,
94 rows: data.rows,
95 scheme,
96 resetKey: tokensResetKey,
97 enabled: canHighlight,
98 });
99​
100 const onDebug = useCallback(
101 (event: NativeSyntheticEvent<Record<string, unknown>>) => {
102 const payload = event.nativeEvent;
103 const message = payload.message;
104 if (
105 (message === "draw-metrics" || message === "visible-range") &&
106 typeof payload.firstRowIndex === "number" &&
107 typeof payload.lastRowIndex === "number"
108 ) {
109 updateVisibleRange({
110 firstRowIndex: payload.firstRowIndex,
111 lastRowIndex: payload.lastRowIndex,
112 });
113 }
114 },
115 [updateVisibleRange],
116 );
117​
118 const onToggleComment = useCallback(
119 (event: NativeSyntheticEvent<{ readonly commentId?: string }>) => {
120 const { commentId } = event.nativeEvent;
121 if (!commentId) {
122 return;
123 }
124​
125 setCollapsedCommentIds((current) => {
126 const next = new Set(current);
127 if (next.has(commentId)) {
128 next.delete(commentId);
129 } else {
130 next.add(commentId);
131 }
132 return next;
133 });
134 },
135 [],
136 );
137​
138 return {
139 theme,
140 rowsJson,
141 collapsedFileIdsJson,
142 collapsedCommentIdsJson,
143 viewedFileIdsJson,
144 selectedRowIdsJson,
145 themeJson,
146 styleJson,
147 tokensPatchJson,
148 tokensResetKey,
149 onDebug,
150 onToggleComment,
151 };