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 { useMessages } from "gt-react-native"; |
| 2 | import { memo, useMemo, useState } from "react"; |
| 3 | import { getFiletypeFromFileName } from "@pierre/diffs/utils/getFiletypeFromFileName"; |
| 4 | import { ScrollView, StyleSheet, Text as NativeText, View, type ColorValue } from "react-native"; |
| 5 | import { SelectableMarkdownText } from "../../native/SelectableMarkdownText"; |
| 6 | import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview"; |
| 7 | import { AppText as Text } from "../../components/ui/AppText"; |
| 8 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 9 | import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 10 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 11 | import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface"; |
| 12 | import { resolveNativeReviewDiffView } from "../diffs/nativeReviewDiffSurface"; |
| 13 | import { |
| 14 | buildNativeReviewDiffData, |
| 15 | buildNativeReviewSnippetRows, |
| 16 | buildReviewCommentPatch, |
| 17 | createNativeReviewDiffTheme, |
| 18 | NATIVE_REVIEW_DIFF_CONTENT_WIDTH, |
| 19 | reviewCommentDiffText, |
| 20 | } from "./nativeReviewDiffAdapter"; |
| 21 | import { buildReviewParsedDiff } from "./reviewModel"; |
| 22 | import { REVIEW_MONO_FONT_FAMILY } from "./reviewDiffRendering"; |
| 23 | import type { ReviewInlineComment } from "./reviewCommentSelection"; |
| 24 | import { useMarkdownCodeHighlight } from "../threads/markdownCodeHighlightState"; |
| 25 | |
| 26 | export interface ReviewCommentColors { |
| 27 | readonly background: ColorValue; |
| 28 | readonly border: ColorValue; |
| 29 | readonly mutedBackground: ColorValue; |
| 30 | readonly text: ColorValue; |
| 31 | readonly mutedText: ColorValue; |
| 32 | readonly codeBackground: ColorValue; |
| 33 | } |
| 34 | |
| 35 | export function useReviewCommentColors(): ReviewCommentColors { |
| 36 | const theme = useUniwindTheme(); |
| 37 | |
| 38 | return { |
| 39 | background: theme["--color-card"], |
| 40 | border: theme["--color-border"], |
| 41 | mutedBackground: theme["--color-subtle"], |
| 42 | text: theme["--color-foreground"], |
| 43 | mutedText: theme["--color-foreground-muted"], |
| 44 | codeBackground: theme["--color-md-code-bg"], |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | export const ReviewCommentCard = memo(function ReviewCommentCard(props: { |
| 49 | readonly comment: ReviewInlineComment; |
| 50 | readonly colors: ReviewCommentColors; |
| 51 | }) { |
| 52 | const { codeSurface, nativeReviewDiffStyle } = useAppearanceCodeSurface(); |
| 53 | const messages = useMessages(); |
| 54 | const markdownStyles = useMarkdownPreviewStyles(); |
| 55 | const { themeAppearance: appearanceScheme, themeId } = useAppearancePreferences(); |
| 56 | const appTheme = useUniwindTheme(); |
| 57 | const [NativeReviewDiffView] = useState(() => resolveNativeReviewDiffView()); |
| 58 | const patch = useMemo(() => buildReviewCommentPatch(props.comment), [props.comment]); |
| 59 | const parsedDiff = useMemo( |
| 60 | () => buildReviewParsedDiff(patch, `thread-review-comment:${props.comment.id}`, messages), |
| 61 | [patch, props.comment.id, messages], |
| 62 | ); |
| 63 | const nativeReviewDiffData = useMemo(() => buildNativeReviewDiffData(parsedDiff), [parsedDiff]); |
| 64 | const compactNativeRows = useMemo(() => { |
| 65 | const rows = nativeReviewDiffData.rows.filter((row) => row.kind !== "file"); |
| 66 | return rows.length > 0 ? rows : buildNativeReviewSnippetRows(props.comment); |
| 67 | }, [nativeReviewDiffData.rows, props.comment]); |
| 68 | const language = |
| 69 | props.comment.fenceLanguage && props.comment.fenceLanguage !== "diff" |
| 70 | ? props.comment.fenceLanguage |
| 71 | : getFiletypeFromFileName(props.comment.filePath); |
| 72 | const addedRows = useMemo( |
| 73 | () => compactNativeRows.filter((row) => row.kind === "line" && row.change !== "delete"), |
| 74 | [compactNativeRows], |
| 75 | ); |
| 76 | const deletedRows = useMemo( |
| 77 | () => compactNativeRows.filter((row) => row.kind === "line" && row.change === "delete"), |
| 78 | [compactNativeRows], |
| 79 | ); |
| 80 | const addedTokens = useMarkdownCodeHighlight({ |
| 81 | code: addedRows.map((row) => row.content ?? "").join("\n"), |
| 82 | language, |
| 83 | enabled: addedRows.length > 0, |
| 84 | theme: appearanceScheme, |
| 85 | }); |
| 86 | const deletedTokens = useMarkdownCodeHighlight({ |
| 87 | code: deletedRows.map((row) => row.content ?? "").join("\n"), |
| 88 | language, |
| 89 | enabled: deletedRows.length > 0, |
| 90 | theme: appearanceScheme, |
| 91 | }); |
| 92 | const snippetTokens = useMarkdownCodeHighlight({ |
| 93 | code: reviewCommentDiffText(props.comment.diff), |
| 94 | language: props.comment.fenceLanguage ?? language, |
| 95 | enabled: compactNativeRows.length === 0, |
| 96 | theme: appearanceScheme, |
| 97 | }); |
| 98 | const nativeTokensJson = useMemo( |
| 99 | () => |
| 100 | JSON.stringify( |
| 101 | Object.fromEntries([ |
| 102 | ...addedRows.map((row, index) => [row.id, addedTokens?.[index] ?? []]), |
| 103 | ...deletedRows.map((row, index) => [row.id, deletedTokens?.[index] ?? []]), |
| 104 | ]), |
| 105 | ), |
| 106 | [addedRows, deletedRows, addedTokens, deletedTokens], |
| 107 | ); |
| 108 | const nativeReviewDiffTheme = useMemo( |
| 109 | () => createNativeReviewDiffTheme(appearanceScheme, themeId, appTheme), |
| 110 | [appearanceScheme, appTheme, themeId], |
| 111 | ); |
| 112 | const nativeRowsJson = useMemo(() => JSON.stringify(compactNativeRows), [compactNativeRows]); |
| 113 | const nativeThemeJson = useMemo( |
| 114 | () => JSON.stringify(nativeReviewDiffTheme), |
| 115 | [nativeReviewDiffTheme], |
| 116 | ); |
| 117 | const nativeStyleJson = JSON.stringify({ ...nativeReviewDiffStyle, wordWrap: false }); |
| 118 | const nativeDiffHeight = useMemo( |
| 119 | () => |
| 120 | Math.min( |
| 121 | 360, |
| 122 | Math.max( |
| 123 | 48, |
| 124 | compactNativeRows.length * nativeReviewDiffStyle.rowHeight + |
| 125 | nativeReviewDiffStyle.fileHeaderVerticalMargin, |
| 126 | ), |
| 127 | ), |
| 128 | [compactNativeRows.length, nativeReviewDiffStyle], |
| 129 | ); |
| 130 | const shouldRenderNativeDiff = NativeReviewDiffView != null && compactNativeRows.length > 0; |
| 131 | |
| 132 | return ( |
| 133 | <View |
| 134 | className="w-full overflow-hidden rounded-[16px] border border-continuous" |
| 135 | style={{ |
| 136 | backgroundColor: props.colors.background, |
| 137 | borderColor: props.colors.border, |
| 138 | }} |
| 139 | > |
| 140 | <View |
| 141 | className="flex-row items-center gap-2 border-b px-3 py-2" |
| 142 | style={{ borderColor: props.colors.border }} |
| 143 | > |
| 144 | <View |
| 145 | className="size-6 items-center justify-center rounded-[7px] border-continuous" |
| 146 | style={{ backgroundColor: props.colors.mutedBackground }} |
| 147 | > |
| 148 | <SymbolView |
| 149 | name="doc.text" |
| 150 | size={13} |
| 151 | tintColor={props.colors.mutedText} |
| 152 | type="monochrome" |
| 153 | /> |
| 154 | </View> |
| 155 | <View className="min-w-0 flex-1"> |
| 156 | <Text |
| 157 | className="font-mono text-xs" |
| 158 | numberOfLines={1} |
| 159 | style={{ color: props.colors.text }} |
| 160 | > |
| 161 | {props.comment.filePath} |
| 162 | </Text> |
| 163 | <Text className="text-xs" style={{ color: props.colors.mutedText }}> |
| 164 | {props.comment.sectionTitle} · {props.comment.rangeLabel} |
| 165 | </Text> |
| 166 | </View> |
| 167 | </View> |
| 168 | {props.comment.text.length > 0 ? ( |
| 169 | <View className="px-3 py-3"> |
| 170 | <SelectableMarkdownText |
| 171 | markdown={props.comment.text} |
| 172 | textStyle={markdownStyles.nativeTextStyle} |
| 173 | /> |
| 174 | </View> |
| 175 | ) : null} |
| 176 | {shouldRenderNativeDiff ? ( |
| 177 | <View |
| 178 | className="border-t" |
| 179 | collapsable={false} |
| 180 | style={{ |
| 181 | backgroundColor: nativeReviewDiffTheme.background, |
| 182 | borderColor: props.colors.border, |
| 183 | height: nativeDiffHeight, |
| 184 | }} |
| 185 | > |
| 186 | <NativeReviewDiffView |
| 187 | collapsable={false} |
| 188 | style={StyleSheet.absoluteFill} |
| 189 | appearanceScheme={appearanceScheme} |
| 190 | contentWidth={NATIVE_REVIEW_DIFF_CONTENT_WIDTH} |
| 191 | rowHeight={nativeReviewDiffStyle.rowHeight} |
| 192 | rowsJson={nativeRowsJson} |
| 193 | tokensJson={nativeTokensJson} |
| 194 | styleJson={nativeStyleJson} |
| 195 | themeJson={nativeThemeJson} |
| 196 | /> |
| 197 | </View> |
| 198 | ) : props.comment.diff.trim().length > 0 ? ( |
| 199 | <ScrollView |
| 200 | horizontal |
| 201 | nestedScrollEnabled |
| 202 | directionalLockEnabled |
| 203 | showsHorizontalScrollIndicator={false} |
| 204 | bounces={false} |
| 205 | className="border-t" |
| 206 | style={{ backgroundColor: props.colors.codeBackground, borderColor: props.colors.border }} |
| 207 | contentContainerStyle={{ padding: 10 }} |
| 208 | > |
| 209 | <NativeText |
| 210 | selectable |
| 211 | className="font-mono" |
| 212 | style={{ |
| 213 | color: props.colors.text, |
| 214 | fontFamily: REVIEW_MONO_FONT_FAMILY, |
| 215 | fontSize: codeSurface.fontSize, |
| 216 | lineHeight: codeSurface.rowHeight, |
| 217 | }} |
| 218 | > |
| 219 | {snippetTokens |
| 220 | ? snippetTokens.map( |
| 221 | (line, lineIndex) => ( |
| 222 | /* oxlint-disable react/no-array-index-key -- Highlight tokens are stateless positions within this immutable snippet. */ <NativeText |
| 223 | key={lineIndex} |
| 224 | > |
| 225 | {lineIndex > 0 ? "\n" : ""} |
| 226 | {line.map((token, tokenIndex) => ( |
| 227 | <NativeText |
| 228 | key={tokenIndex} |
| 229 | style={{ |
| 230 | color: token.color ?? props.colors.text, |
| 231 | fontStyle: (token.fontStyle ?? 0) & 1 ? "italic" : "normal", |
| 232 | fontWeight: (token.fontStyle ?? 0) & 2 ? "700" : "400", |
| 233 | }} |
| 234 | > |
| 235 | {token.content} |
| 236 | </NativeText> |
| 237 | ))} |
| 238 | </NativeText> |
| 239 | ) /* oxlint-enable react/no-array-index-key */, |
| 240 | ) |
| 241 | : reviewCommentDiffText(props.comment.diff)} |
| 242 | </NativeText> |
| 243 | </ScrollView> |
| 244 | ) : null} |
| 245 | </View> |
| 246 | ); |
| 247 | }); |