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
| 1 | import { useMemo, useRef, useState } from "react"; |
| 2 | import type { NativeSyntheticEvent } from "react-native"; |
| 3 | |
| 4 | import { createNativeReviewDiffTheme, type NativeReviewDiffData } from "./nativeReviewDiffAdapter"; |
| 5 | import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface"; |
| 6 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 7 | import { useNativeReviewDiffHighlighting } from "./useNativeReviewDiffHighlighting"; |
| 8 | import { buildNativeReviewTokensResetKey } from "./reviewDiffBridgeKeys"; |
| 9 | import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 10 | |
| 11 | export function useNativeReviewDiffBridge(input: { |
| 12 | readonly threadKey: string | null; |
| 13 | readonly sectionId: string | null; |
| 14 | readonly diff: string | null | undefined; |
| 15 | readonly data: NativeReviewDiffData; |
| 16 | readonly collapsedFileIds: ReadonlyArray<string>; |
| 17 | readonly viewedFileIds: ReadonlyArray<string>; |
| 18 | readonly selectedRowIds: ReadonlyArray<string>; |
| 19 | readonly canHighlight: boolean; |
| 20 | }) { |
| 21 | const { |
| 22 | canHighlight, |
| 23 | collapsedFileIds, |
| 24 | data, |
| 25 | diff, |
| 26 | sectionId, |
| 27 | selectedRowIds, |
| 28 | threadKey, |
| 29 | viewedFileIds, |
| 30 | } = input; |
| 31 | const { nativeReviewDiffStyle } = useAppearanceCodeSurface(); |
| 32 | const { themeAppearance: scheme, themeId } = useAppearancePreferences(); |
| 33 | const appTheme = useUniwindTheme(); |
| 34 | const [surfaceGeneration, setSurfaceGeneration] = useState(0); |
| 35 | const hasMountedContentRef = useRef(false); |
| 36 | const [collapsedCommentIds, setCollapsedCommentIds] = useState<ReadonlySet<string>>( |
| 37 | () => new Set(), |
| 38 | ); |
| 39 | |
| 40 | const theme = useMemo( |
| 41 | () => createNativeReviewDiffTheme(scheme, themeId, appTheme), |
| 42 | [appTheme, scheme, themeId], |
| 43 | ); |
| 44 | const collapsedFileIdsSet = useMemo(() => new Set(collapsedFileIds), [collapsedFileIds]); |
| 45 | const rowsJson = useMemo(() => JSON.stringify(data.rows), [data.rows]); |
| 46 | const collapsedFileIdsJson = useMemo(() => JSON.stringify(collapsedFileIds), [collapsedFileIds]); |
| 47 | const viewedFileIdsJson = useMemo(() => JSON.stringify(viewedFileIds), [viewedFileIds]); |
| 48 | const selectedRowIdsJson = useMemo(() => JSON.stringify(selectedRowIds), [selectedRowIds]); |
| 49 | const collapsedCommentIdsJson = useMemo( |
| 50 | () => JSON.stringify(Array.from(collapsedCommentIds)), |
| 51 | [collapsedCommentIds], |
| 52 | ); |
| 53 | const themeJson = useMemo(() => JSON.stringify(theme), [theme]); |
| 54 | const styleJson = JSON.stringify(nativeReviewDiffStyle); |
| 55 | const tokensResetKey = useMemo( |
| 56 | () => |
| 57 | `${buildNativeReviewTokensResetKey({ |
| 58 | threadKey, |
| 59 | sectionId, |
| 60 | scheme, |
| 61 | diff, |
| 62 | fileCount: data.files.length, |
| 63 | rowCount: data.rows.length, |
| 64 | })}:${surfaceGeneration}`, |
| 65 | [data.files.length, data.rows.length, diff, scheme, sectionId, surfaceGeneration, threadKey], |
| 66 | ); |
| 67 | const { acknowledgeTokenPatch, rejectTokenPatch, tokensPatchJson, updateVisibleRange } = |
| 68 | useNativeReviewDiffHighlighting({ |
| 69 | files: data.files, |
| 70 | rows: data.rows, |
| 71 | collapsedFileIds: collapsedFileIdsSet, |
| 72 | scheme, |
| 73 | resetKey: tokensResetKey, |
| 74 | enabled: canHighlight, |
| 75 | }); |
| 76 | |
| 77 | const onDebug = (event: NativeSyntheticEvent<Record<string, unknown>>) => { |
| 78 | const payload = event.nativeEvent; |
| 79 | const message = payload.message; |
| 80 | if (message === "surface-content-mounted") { |
| 81 | // The initial content already has a fresh hook; replacements lose accepted tokens. |
| 82 | if (hasMountedContentRef.current) setSurfaceGeneration((generation) => generation + 1); |
| 83 | hasMountedContentRef.current = true; |
| 84 | } |
| 85 | if (message === "tokens-patch-delivered") acknowledgeTokenPatch(payload); |
| 86 | if (message === "tokens-patch-failed") rejectTokenPatch(payload); |
| 87 | if ( |
| 88 | (message === "draw-metrics" || message === "visible-range") && |
| 89 | typeof payload.firstRowIndex === "number" && |
| 90 | typeof payload.lastRowIndex === "number" |
| 91 | ) { |
| 92 | updateVisibleRange({ |
| 93 | firstRowIndex: payload.firstRowIndex, |
| 94 | lastRowIndex: payload.lastRowIndex, |
| 95 | }); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | const onToggleComment = (event: NativeSyntheticEvent<{ readonly commentId?: string }>) => { |
| 100 | const { commentId } = event.nativeEvent; |
| 101 | if (!commentId) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | setCollapsedCommentIds((current) => { |
| 106 | const next = new Set(current); |
| 107 | if (next.has(commentId)) { |
| 108 | next.delete(commentId); |
| 109 | } else { |
| 110 | next.add(commentId); |
| 111 | } |
| 112 | return next; |
| 113 | }); |
| 114 | }; |
| 115 | |
| 116 | return { |
| 117 | themeId, |
| 118 | theme, |
| 119 | rowsJson, |
| 120 | collapsedFileIdsJson, |
| 121 | collapsedCommentIdsJson, |
| 122 | viewedFileIdsJson, |
| 123 | selectedRowIdsJson, |
| 124 | themeJson, |
| 125 | styleJson, |
| 126 | tokensPatchJson, |
| 127 | tokensResetKey, |
| 128 | onDebug, |
| 129 | onToggleComment, |
| 130 | }; |
| 131 | } |