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 { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 2 | import { AsyncResult } from "effect/reactivity"; |
| 3 | import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 4 | import { formatDiffCount } from "@t3tools/client-runtime/diff-count"; |
| 5 | import { LegendList } from "@legendapp/list/react-native"; |
| 6 | import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 7 | import { AppBar } from "../../components/ui/AppBar"; |
| 8 | import type { AppBarMenuItem } from "../../components/ui/AppBar.types"; |
| 9 | import type { ReviewSectionItem } from "./reviewModel"; |
| 10 | import { useReviewHeaderPresentation } from "./useReviewHeaderPresentation"; |
| 11 | import { T, useGT } from "gt-react-native"; |
| 12 | import { type Ref, type ReactElement, useImperativeHandle, useRef, useState } from "react"; |
| 13 | import { Pressable, ScrollView, type NativeSyntheticEvent, StyleSheet, View } from "react-native"; |
| 14 | import { RefreshControl } from "../../components/ui/RefreshControl"; |
| 15 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 16 | import { useReduceMotionEnabled } from "../../lib/appState"; |
| 17 | import { useContentTopInset, useSheetContentBottomInset } from "../../components/ui/useBarInset"; |
| 18 | import { NativeScrollView } from "../../native/NativeScrollViewMarker"; |
| 19 | |
| 20 | import { AppText as Text } from "../../components/ui/AppText"; |
| 21 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 22 | import { |
| 23 | ContentBand, |
| 24 | ContentStatus, |
| 25 | contentStatusContainerStyle, |
| 26 | } from "../../components/ui/ContentBand"; |
| 27 | import { PaneListItem } from "../../components/ui/PaneListItem"; |
| 28 | import { ScreenContent } from "../../components/ui/ScreenContent"; |
| 29 | import { SheetPane } from "../../components/ui/SheetPane"; |
| 30 | import { useEnvironmentPresentation } from "../../state/presentation"; |
| 31 | import { useThreadDraftForThread } from "../../state/use-thread-composer-state"; |
| 32 | import { EnvironmentConnectionNotice } from "../connection/EnvironmentConnectionNotice"; |
| 33 | import { useRetryEnvironment } from "../connection/useRetryEnvironment"; |
| 34 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 35 | import { |
| 36 | Inspector, |
| 37 | useAdaptiveWorkspaceLayout, |
| 38 | useWorkspacePaneState, |
| 39 | } from "../layout/AdaptiveWorkspaceLayout"; |
| 40 | import { useSelectedThreadWorktree } from "../../state/use-selected-thread-worktree"; |
| 41 | import { useReviewCacheForThread } from "./reviewState"; |
| 42 | import { |
| 43 | isNativeReviewDiffDrawEvent, |
| 44 | type NativeReviewDiffViewHandle, |
| 45 | resolveNativeReviewDiffView, |
| 46 | } from "../diffs/nativeReviewDiffSurface"; |
| 47 | import { NATIVE_REVIEW_DIFF_CONTENT_WIDTH } from "./nativeReviewDiffAdapter"; |
| 48 | import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface"; |
| 49 | import { useReviewDiffData } from "./useReviewDiffData"; |
| 50 | import { useReviewDiffPrewarming } from "./useReviewDiffPrewarming"; |
| 51 | import { useReviewFileVisibility } from "./reviewFileVisibility"; |
| 52 | import { useReviewSections } from "./useReviewSections"; |
| 53 | import { useNativeReviewDiffBridge } from "./useNativeReviewDiffBridge"; |
| 54 | import { useReviewCommentSelectionController } from "./useReviewCommentSelectionController"; |
| 55 | import { resolveReviewAvailability } from "./reviewAvailability"; |
| 56 | import { resolveSelectedReviewFileId } from "./reviewPaneSelection"; |
| 57 | import { buildReviewSectionMenu } from "./review-section-menu"; |
| 58 | import { reportShowcaseSceneRendered } from "../showcase/showcaseRenderSignal"; |
| 59 | import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail"; |
| 60 | |
| 61 | function ReviewHeader( |
| 62 | props: Parameters<typeof useReviewHeaderPresentation>[0] & { |
| 63 | readonly sectionMenu: ReturnType<typeof buildReviewSectionMenu>; |
| 64 | readonly showSectionToolbar: boolean; |
| 65 | readonly showChangedFilesToggle: boolean; |
| 66 | readonly onSelectSection: (sectionId: string) => void; |
| 67 | readonly onReturnToThread: () => void; |
| 68 | }, |
| 69 | ) { |
| 70 | const { toggleAuxiliaryPane } = useAdaptiveWorkspaceLayout(); |
| 71 | const { panes } = useWorkspacePaneState(); |
| 72 | const presentation = useReviewHeaderPresentation(props); |
| 73 | const gt = useGT(); |
| 74 | const sectionAction = (section: ReviewSectionItem | null, title: string): AppBarMenuItem => ({ |
| 75 | id: section ? `section:${section.id}` : `unavailable:${title}`, |
| 76 | title, |
| 77 | disabled: section === null, |
| 78 | selected: section !== null && section.id === props.selectedSection?.id, |
| 79 | onPress: () => { |
| 80 | if (section) props.onSelectSection(section.id); |
| 81 | }, |
| 82 | }); |
| 83 | return ( |
| 84 | <AppBar |
| 85 | title={presentation.title} |
| 86 | subtitle={presentation.subtitle} |
| 87 | onBack={props.onReturnToThread} |
| 88 | hideBottomBorder |
| 89 | backInSplitView={{ accessibilityLabel: gt("Back to chat"), icon: "chevron.left" }} |
| 90 | actions={ |
| 91 | props.showChangedFilesToggle |
| 92 | ? [ |
| 93 | { |
| 94 | accessibilityLabel: panes.auxiliaryPaneVisible |
| 95 | ? gt("Hide changed files") |
| 96 | : gt("Show changed files"), |
| 97 | icon: "sidebar.right", |
| 98 | selected: panes.auxiliaryPaneVisible, |
| 99 | onPress: toggleAuxiliaryPane, |
| 100 | }, |
| 101 | ] |
| 102 | : undefined |
| 103 | } |
| 104 | menus={[ |
| 105 | ...(presentation.gitMenu ? [presentation.gitMenu] : []), |
| 106 | ...(props.showSectionToolbar |
| 107 | ? [ |
| 108 | { |
| 109 | title: gt("Select diff"), |
| 110 | icon: presentation.menuIcon, |
| 111 | items: [ |
| 112 | { |
| 113 | id: "sections", |
| 114 | inline: true, |
| 115 | items: [ |
| 116 | sectionAction( |
| 117 | props.sectionMenu.branchChanges, |
| 118 | gt("Changes", { $context: "diff section: branch changes" }), |
| 119 | ), |
| 120 | sectionAction( |
| 121 | props.sectionMenu.workingTree, |
| 122 | gt("Uncommitted", { $context: "diff section: working tree" }), |
| 123 | ), |
| 124 | sectionAction(props.sectionMenu.latestTurn, gt("Latest turn")), |
| 125 | ], |
| 126 | }, |
| 127 | ...(props.sectionMenu.turns.length > 0 |
| 128 | ? [ |
| 129 | { |
| 130 | id: "turns", |
| 131 | title: gt("Turn", { $context: "agent turn, diff section menu" }), |
| 132 | items: props.sectionMenu.turns.map((section) => ({ |
| 133 | id: `section:${section.id}`, |
| 134 | title: section.title, |
| 135 | subtitle: section.subtitle ?? undefined, |
| 136 | selected: section.id === props.selectedSection?.id, |
| 137 | onPress: () => props.onSelectSection(section.id), |
| 138 | })), |
| 139 | }, |
| 140 | ] |
| 141 | : []), |
| 142 | ...(presentation.refreshAction ? [presentation.refreshAction] : []), |
| 143 | ], |
| 144 | }, |
| 145 | ] |
| 146 | : []), |
| 147 | ]} |
| 148 | /> |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | const SHOWCASE_ENABLED = process.env.EXPO_PUBLIC_SHOWCASE === "1"; |
| 153 | |
| 154 | const ReviewNotice = function ReviewNotice(props: { readonly notice: string }) { |
| 155 | return ( |
| 156 | <ContentBand tone="warning"> |
| 157 | <Text className="text-xs font-t3-bold uppercase text-warning-foreground"> |
| 158 | <T>Partial diff</T> |
| 159 | </Text> |
| 160 | <Text className="text-xs leading-normal text-warning-foreground">{props.notice}</Text> |
| 161 | </ContentBand> |
| 162 | ); |
| 163 | }; |
| 164 | |
| 165 | function ReviewSelectionActionBar(props: { |
| 166 | readonly bottomInset: number; |
| 167 | readonly title: string | null; |
| 168 | readonly onOpenComment: (() => void) | null; |
| 169 | readonly onClear: () => void; |
| 170 | }) { |
| 171 | if (!props.title) { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | const content = ( |
| 176 | <> |
| 177 | <SymbolView |
| 178 | name={props.onOpenComment ? "text.bubble" : "line.3.horizontal.decrease.circle"} |
| 179 | size={16} |
| 180 | tintColorClassName="accent-primary-foreground" |
| 181 | type="monochrome" |
| 182 | /> |
| 183 | <Text className="text-base font-t3-bold text-primary-foreground">{props.title}</Text> |
| 184 | </> |
| 185 | ); |
| 186 | |
| 187 | return ( |
| 188 | <View |
| 189 | pointerEvents="box-none" |
| 190 | style={{ |
| 191 | position: "absolute", |
| 192 | left: 18, |
| 193 | right: 18, |
| 194 | bottom: Math.max(props.bottomInset, 10) + 18, |
| 195 | flexDirection: "row", |
| 196 | justifyContent: "center", |
| 197 | gap: 10, |
| 198 | }} |
| 199 | > |
| 200 | {props.onOpenComment ? ( |
| 201 | <Pressable |
| 202 | className="h-12 flex-1 flex-row items-center justify-center gap-2 rounded-full bg-primary px-5" |
| 203 | onPress={props.onOpenComment} |
| 204 | > |
| 205 | {content} |
| 206 | </Pressable> |
| 207 | ) : ( |
| 208 | <View className="h-12 flex-1 flex-row items-center justify-center gap-2 rounded-full bg-primary px-5"> |
| 209 | {content} |
| 210 | </View> |
| 211 | )} |
| 212 | |
| 213 | <Pressable |
| 214 | className="h-12 w-12 items-center justify-center rounded-full bg-primary" |
| 215 | onPress={props.onClear} |
| 216 | > |
| 217 | <SymbolView |
| 218 | name="xmark" |
| 219 | size={16} |
| 220 | tintColorClassName="accent-primary-foreground" |
| 221 | type="monochrome" |
| 222 | /> |
| 223 | </Pressable> |
| 224 | </View> |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | interface ReviewNavigatorFile { |
| 229 | readonly id: string; |
| 230 | readonly path: string; |
| 231 | readonly additions: number; |
| 232 | readonly deletions: number; |
| 233 | } |
| 234 | |
| 235 | const ReviewFileNavigatorRow = function ReviewFileNavigatorRow(props: { |
| 236 | readonly file: ReviewNavigatorFile; |
| 237 | readonly selected: boolean; |
| 238 | readonly onSelectFile: (fileId: string | null) => void; |
| 239 | }) { |
| 240 | const { file, selected, onSelectFile } = props; |
| 241 | const handlePress = () => { |
| 242 | onSelectFile(selected ? null : file.id); |
| 243 | }; |
| 244 | |
| 245 | return ( |
| 246 | <PaneListItem selected={selected} onPress={handlePress}> |
| 247 | <Text |
| 248 | className={ |
| 249 | selected |
| 250 | ? "text-xs font-t3-bold text-foreground" |
| 251 | : "text-xs font-t3-medium text-foreground-secondary" |
| 252 | } |
| 253 | numberOfLines={2} |
| 254 | > |
| 255 | {file.path} |
| 256 | </Text> |
| 257 | <View className="mt-1 flex-row gap-2"> |
| 258 | <Text className="text-2xs font-t3-bold text-adaptive-emerald-700-300"> |
| 259 | +{formatDiffCount(file.additions)} |
| 260 | </Text> |
| 261 | <Text className="text-2xs font-t3-bold text-adaptive-rose-700-300"> |
| 262 | -{formatDiffCount(file.deletions)} |
| 263 | </Text> |
| 264 | </View> |
| 265 | </PaneListItem> |
| 266 | ); |
| 267 | }; |
| 268 | |
| 269 | interface ReviewFileNavigatorHandle { |
| 270 | readonly setVisibleFile: (fileId: string | null) => void; |
| 271 | } |
| 272 | |
| 273 | interface ReviewFileNavigatorProps { |
| 274 | readonly files: ReadonlyArray<ReviewNavigatorFile>; |
| 275 | readonly sectionId: string | null; |
| 276 | readonly onSelectFile: (fileId: string | null) => void; |
| 277 | readonly ref?: Ref<ReviewFileNavigatorHandle>; |
| 278 | } |
| 279 | |
| 280 | function ReviewFileNavigator({ files, sectionId, onSelectFile, ref }: ReviewFileNavigatorProps) { |
| 281 | const [fileSelection, setFileSelection] = useState<{ |
| 282 | readonly sectionId: string | null; |
| 283 | readonly fileId: string | null; |
| 284 | }>({ sectionId: null, fileId: null }); |
| 285 | const availableFileIds = files.map((file) => file.id); |
| 286 | const selectedFileId = resolveSelectedReviewFileId({ |
| 287 | selection: fileSelection, |
| 288 | sectionId, |
| 289 | availableFileIds, |
| 290 | }); |
| 291 | |
| 292 | useImperativeHandle( |
| 293 | ref, |
| 294 | () => ({ |
| 295 | setVisibleFile: (fileId) => { |
| 296 | if (fileId !== null && !availableFileIds.includes(fileId)) { |
| 297 | return; |
| 298 | } |
| 299 | setFileSelection((current) => { |
| 300 | if (current.sectionId === sectionId && current.fileId === fileId) { |
| 301 | return current; |
| 302 | } |
| 303 | return { sectionId, fileId }; |
| 304 | }); |
| 305 | }, |
| 306 | }), |
| 307 | [availableFileIds, sectionId], |
| 308 | ); |
| 309 | |
| 310 | const handleSelectFile = (fileId: string | null) => { |
| 311 | setFileSelection({ sectionId, fileId }); |
| 312 | onSelectFile(fileId); |
| 313 | }; |
| 314 | const gt = useGT(); |
| 315 | |
| 316 | const renderFile = ({ item }: { readonly item: ReviewNavigatorFile }) => ( |
| 317 | <ReviewFileNavigatorRow |
| 318 | file={item} |
| 319 | selected={selectedFileId === item.id} |
| 320 | onSelectFile={handleSelectFile} |
| 321 | /> |
| 322 | ); |
| 323 | |
| 324 | return ( |
| 325 | <SheetPane> |
| 326 | <AppBar |
| 327 | title={gt("Changed files")} |
| 328 | subtitle={gt("{count, plural, one {{count} file} other {{count} files}}", { |
| 329 | count: files.length, |
| 330 | })} |
| 331 | hideBottomBorder |
| 332 | /> |
| 333 | <ScreenContent insetHorizontal> |
| 334 | <LegendList |
| 335 | renderScrollComponent={NativeScrollView} |
| 336 | data={files} |
| 337 | // Rows keep their callback until extraData changes; it reads collapsed files. |
| 338 | extraData={[selectedFileId, handleSelectFile]} |
| 339 | keyExtractor={(file) => file.id} |
| 340 | estimatedItemSize={64} |
| 341 | recycleItems |
| 342 | contentInsetAdjustmentBehavior="automatic" |
| 343 | contentContainerStyle={{ paddingHorizontal: 8, paddingVertical: 8 }} |
| 344 | renderItem={renderFile} |
| 345 | /> |
| 346 | </ScreenContent> |
| 347 | </SheetPane> |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | type ReviewSheetProps = StaticScreenProps<{ |
| 352 | readonly environmentId: EnvironmentId; |
| 353 | readonly threadId: ThreadId; |
| 354 | }>; |
| 355 | |
| 356 | export function ReviewSheet(props: ReviewSheetProps) { |
| 357 | const gt = useGT(); |
| 358 | const reduceMotion = useReduceMotionEnabled(); |
| 359 | const { nativeReviewDiffStyle } = useAppearanceCodeSurface(); |
| 360 | const { panes } = useWorkspacePaneState(); |
| 361 | const navigation = useNavigation(); |
| 362 | const selectDetail = useSelectWorkspaceDetail(); |
| 363 | const insets = useSafeAreaInsets(); |
| 364 | const { themeAppearance: selectedTheme } = useAppearancePreferences(); |
| 365 | const { environmentId, threadId } = props.route.params; |
| 366 | const environment = useEnvironmentPresentation(environmentId); |
| 367 | const retryEnvironment = useRetryEnvironment(); |
| 368 | const isEnvironmentReady = environment.presentation?.connection.phase === "connected"; |
| 369 | const { draftMessage, draftContext } = useThreadDraftForThread({ environmentId, threadId }); |
| 370 | const reviewCache = useReviewCacheForThread({ environmentId, threadId }); |
| 371 | const { selectedThreadCwd } = useSelectedThreadWorktree(); |
| 372 | const topContentInset = useContentTopInset(); |
| 373 | const bottomContentInset = useSheetContentBottomInset(); |
| 374 | |
| 375 | const { |
| 376 | error, |
| 377 | reviewSections, |
| 378 | selectedSection, |
| 379 | refreshSelectedSection, |
| 380 | selectSection, |
| 381 | isSelectedSectionPending, |
| 382 | } = useReviewSections({ |
| 383 | enabled: isEnvironmentReady, |
| 384 | environmentId, |
| 385 | threadId, |
| 386 | reviewCache, |
| 387 | }); |
| 388 | useReviewDiffPrewarming({ |
| 389 | threadKey: reviewCache.threadKey, |
| 390 | sections: reviewSections, |
| 391 | selectedSectionId: selectedSection?.id ?? null, |
| 392 | }); |
| 393 | const { |
| 394 | headerDiffSummary, |
| 395 | nativeReviewDiffData, |
| 396 | parsedDiff, |
| 397 | pendingReviewCommentCount, |
| 398 | loadVisibleFile, |
| 399 | isPending: areFilePatchesPending, |
| 400 | } = useReviewDiffData({ |
| 401 | threadKey: reviewCache.threadKey, |
| 402 | environmentId, |
| 403 | cwd: selectedThreadCwd, |
| 404 | selectedSection, |
| 405 | draftMessage, |
| 406 | draftContext, |
| 407 | }); |
| 408 | // Expo may return null before registration or in older binaries; use the fallback. |
| 409 | const NativeReviewDiffView = resolveNativeReviewDiffView(); |
| 410 | const nativeReviewDiffViewRef = useRef<NativeReviewDiffViewHandle>(null); |
| 411 | const showcasedReviewDrawRef = useRef<string | null>(null); |
| 412 | const [isPullRefreshing, setIsPullRefreshing] = useState(false); |
| 413 | const handlePullToRefresh = async () => { |
| 414 | setIsPullRefreshing(true); |
| 415 | await refreshSelectedSection(); |
| 416 | setIsPullRefreshing(false); |
| 417 | }; |
| 418 | const isRefreshing = isPullRefreshing || isSelectedSectionPending || areFilePatchesPending; |
| 419 | // A refresh that starts programmatically adds its height to the native top inset and |
| 420 | // subtracts it when it ends. If topContentInset changes in between, the inset ends up |
| 421 | // short and the content sits under the header, so the fallback ScrollView starts over. |
| 422 | const [fallbackInset, setFallbackInset] = useState(topContentInset); |
| 423 | const [fallbackScrollViewKey, setFallbackScrollViewKey] = useState(0); |
| 424 | if (fallbackInset !== topContentInset) { |
| 425 | setFallbackInset(topContentInset); |
| 426 | if (isRefreshing) setFallbackScrollViewKey((key) => key + 1); |
| 427 | } |
| 428 | const reviewFileNavigatorRef = useRef<ReviewFileNavigatorHandle>(null); |
| 429 | const reviewFiles = parsedDiff.kind === "files" ? parsedDiff.files : []; |
| 430 | const fileVisibility = useReviewFileVisibility({ |
| 431 | threadKey: reviewCache.threadKey, |
| 432 | sectionId: selectedSection?.id ?? null, |
| 433 | files: reviewFiles, |
| 434 | cachedExpandedFileIds: selectedSection?.id |
| 435 | ? reviewCache.expandedFileIdsBySection[selectedSection.id] |
| 436 | : undefined, |
| 437 | cachedViewedFileIds: selectedSection?.id |
| 438 | ? reviewCache.viewedFileIdsBySection[selectedSection.id] |
| 439 | : undefined, |
| 440 | }); |
| 441 | const { collapsedFileIds, toggleExpandedFile, toggleViewedFile, viewedFileIds } = fileVisibility; |
| 442 | const commentSelection = useReviewCommentSelectionController({ |
| 443 | environmentId, |
| 444 | threadId, |
| 445 | selectedSection, |
| 446 | nativeReviewDiffData, |
| 447 | }); |
| 448 | const nativeBridge = useNativeReviewDiffBridge({ |
| 449 | threadKey: reviewCache.threadKey, |
| 450 | sectionId: selectedSection?.id ?? null, |
| 451 | diff: selectedSection?.diff, |
| 452 | data: nativeReviewDiffData, |
| 453 | collapsedFileIds, |
| 454 | viewedFileIds, |
| 455 | selectedRowIds: commentSelection.selectedRowIds, |
| 456 | canHighlight: parsedDiff.kind === "files", |
| 457 | }); |
| 458 | const showcaseReviewKey = |
| 459 | SHOWCASE_ENABLED && parsedDiff.kind === "files" && selectedSection |
| 460 | ? `${reviewCache.threadKey}:${selectedSection.id}:${nativeBridge.tokensResetKey}:${nativeBridge.themeId}` |
| 461 | : null; |
| 462 | const handleNativeDebug = (event: NativeSyntheticEvent<Record<string, unknown>>) => { |
| 463 | nativeBridge.onDebug(event); |
| 464 | if ( |
| 465 | showcaseReviewKey === null || |
| 466 | showcasedReviewDrawRef.current === showcaseReviewKey || |
| 467 | !isNativeReviewDiffDrawEvent(event.nativeEvent) |
| 468 | ) { |
| 469 | return; |
| 470 | } |
| 471 | showcasedReviewDrawRef.current = showcaseReviewKey; |
| 472 | reportShowcaseSceneRendered({ scene: "review", themeId: nativeBridge.themeId }); |
| 473 | }; |
| 474 | |
| 475 | const handleSelectFile = async (fileId: string | null) => { |
| 476 | loadVisibleFile(fileId, true); |
| 477 | commentSelection.clearSelection(); |
| 478 | if (fileId !== null && collapsedFileIds.includes(fileId)) { |
| 479 | toggleExpandedFile(fileId); |
| 480 | } |
| 481 | const nativeView = nativeReviewDiffViewRef.current; |
| 482 | if (!nativeView) return; |
| 483 | const result = await settlePromise(() => |
| 484 | fileId === null |
| 485 | ? nativeView.scrollToTop(!reduceMotion) |
| 486 | : nativeView.scrollToFile(fileId, !reduceMotion), |
| 487 | ); |
| 488 | if (AsyncResult.isFailure(result)) { |
| 489 | console.error("[review] Failed to navigate to diff file", squashAtomCommandFailure(result)); |
| 490 | } |
| 491 | }; |
| 492 | const handleVisibleFileChange = ( |
| 493 | event: NativeSyntheticEvent<{ readonly fileId?: string | null }>, |
| 494 | ) => { |
| 495 | loadVisibleFile(event.nativeEvent.fileId ?? null); |
| 496 | reviewFileNavigatorRef.current?.setVisibleFile(event.nativeEvent.fileId ?? null); |
| 497 | }; |
| 498 | const renderInspector = () => ( |
| 499 | <ReviewFileNavigator |
| 500 | ref={reviewFileNavigatorRef} |
| 501 | files={nativeReviewDiffData.files} |
| 502 | sectionId={selectedSection?.id ?? null} |
| 503 | onSelectFile={(fileId) => void handleSelectFile(fileId)} |
| 504 | /> |
| 505 | ); |
| 506 | |
| 507 | const handleNativeToggleFile = (event: NativeSyntheticEvent<{ readonly fileId?: string }>) => { |
| 508 | const { fileId } = event.nativeEvent; |
| 509 | if (fileId) { |
| 510 | loadVisibleFile(fileId, true); |
| 511 | toggleExpandedFile(fileId); |
| 512 | } |
| 513 | }; |
| 514 | |
| 515 | const handleNativeToggleViewedFile = ( |
| 516 | event: NativeSyntheticEvent<{ readonly fileId?: string }>, |
| 517 | ) => { |
| 518 | const { fileId } = event.nativeEvent; |
| 519 | if (fileId) { |
| 520 | toggleViewedFile(fileId); |
| 521 | } |
| 522 | }; |
| 523 | |
| 524 | const parsedDiffNotice = |
| 525 | parsedDiff.kind === "files" || parsedDiff.kind === "raw" ? parsedDiff.notice : null; |
| 526 | const hasCachedSelectedDiff = selectedSection?.diff != null; |
| 527 | const hasAnyCachedDiff = reviewSections.some((section) => section.diff != null); |
| 528 | const sectionMenu = buildReviewSectionMenu(reviewSections); |
| 529 | const { showConnectionNotice, showSectionToolbar } = resolveReviewAvailability({ |
| 530 | hasEnvironmentPresentation: environment.isReady, |
| 531 | isEnvironmentConnected: isEnvironmentReady, |
| 532 | hasCachedSelectedDiff, |
| 533 | hasAnyCachedDiff, |
| 534 | }); |
| 535 | const handleRetryEnvironment = () => { |
| 536 | void retryEnvironment(environmentId); |
| 537 | }; |
| 538 | const handleReturnToThread = () => { |
| 539 | if (navigation.canGoBack()) { |
| 540 | navigation.goBack(); |
| 541 | return; |
| 542 | } |
| 543 | selectDetail({ |
| 544 | name: "Thread", |
| 545 | params: { environmentId: String(environmentId), threadId: String(threadId) }, |
| 546 | }); |
| 547 | }; |
| 548 | const androidHeaderSubtitle = [ |
| 549 | selectedSection?.title, |
| 550 | headerDiffSummary.additions, |
| 551 | headerDiffSummary.deletions, |
| 552 | ] |
| 553 | .filter((part): part is string => Boolean(part)) |
| 554 | .join(" · "); |
| 555 | |
| 556 | const showChangedFilesPane = |
| 557 | !showConnectionNotice && |
| 558 | selectedSection !== null && |
| 559 | parsedDiff.kind === "files" && |
| 560 | NativeReviewDiffView !== null; |
| 561 | const showChangedFilesToggle = panes.supportsAuxiliaryPane && showChangedFilesPane; |
| 562 | |
| 563 | const listHeader = (() => { |
| 564 | const children: ReactElement[] = []; |
| 565 | |
| 566 | if (error) { |
| 567 | children.push( |
| 568 | <ContentBand key="review-error"> |
| 569 | <Text className="text-sm font-t3-bold text-foreground"> |
| 570 | <T>Review unavailable</T> |
| 571 | </Text> |
| 572 | <Text className="text-xs leading-normal text-foreground-muted">{error}</Text> |
| 573 | </ContentBand>, |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | if (parsedDiffNotice) { |
| 578 | children.push(<ReviewNotice key="review-notice" notice={parsedDiffNotice} />); |
| 579 | } |
| 580 | |
| 581 | if (children.length === 0) { |
| 582 | return null; |
| 583 | } |
| 584 | |
| 585 | return <>{children}</>; |
| 586 | })(); |
| 587 | const headerSubtitle = [ |
| 588 | headerDiffSummary.additions, |
| 589 | headerDiffSummary.deletions, |
| 590 | pendingReviewCommentCount > 0 |
| 591 | ? gt("{count, plural, one {{count} comment} other {{count} comments}}", { |
| 592 | count: pendingReviewCommentCount, |
| 593 | }) |
| 594 | : null, |
| 595 | ] |
| 596 | .filter(Boolean) |
| 597 | .join(" · "); |
| 598 | const headerTitleText = selectedSection?.title ?? gt("Review changes"); |
| 599 | |
| 600 | return ( |
| 601 | <> |
| 602 | {showChangedFilesPane ? ( |
| 603 | <Inspector |
| 604 | revealKey={`${environmentId}:${threadId}`} |
| 605 | content={{ active: "changes", panes: [{ key: "changes", render: renderInspector }] }} |
| 606 | /> |
| 607 | ) : null} |
| 608 | <ReviewHeader |
| 609 | environmentId={environmentId} |
| 610 | threadId={threadId} |
| 611 | title={headerTitleText} |
| 612 | subtitle={headerSubtitle} |
| 613 | androidSubtitle={androidHeaderSubtitle} |
| 614 | selectedThreadCwd={selectedThreadCwd} |
| 615 | sectionMenu={sectionMenu} |
| 616 | selectedSection={selectedSection} |
| 617 | showSectionToolbar={showSectionToolbar} |
| 618 | showChangedFilesToggle={showChangedFilesToggle} |
| 619 | onRefresh={handlePullToRefresh} |
| 620 | onSelectSection={selectSection} |
| 621 | onReturnToThread={handleReturnToThread} |
| 622 | /> |
| 623 | |
| 624 | <ScreenContent> |
| 625 | <View className="flex-1 bg-sheet"> |
| 626 | {showConnectionNotice ? ( |
| 627 | <View className="flex-1" style={{ paddingTop: topContentInset }}> |
| 628 | <EnvironmentConnectionNotice |
| 629 | environmentLabel={ |
| 630 | environment.presentation?.entry.target.label ?? |
| 631 | gt("Environment", { $context: "fallback name for a server" }) |
| 632 | } |
| 633 | connection={ |
| 634 | environment.presentation?.connection ?? { |
| 635 | phase: "available", |
| 636 | error: null, |
| 637 | traceId: null, |
| 638 | } |
| 639 | } |
| 640 | resource="review" |
| 641 | onRetry={handleRetryEnvironment} |
| 642 | /> |
| 643 | </View> |
| 644 | ) : selectedSection && parsedDiff.kind === "files" && NativeReviewDiffView ? ( |
| 645 | <View |
| 646 | className="flex-1" |
| 647 | style={{ |
| 648 | backgroundColor: nativeBridge.theme.background, |
| 649 | }} |
| 650 | > |
| 651 | <View |
| 652 | className="min-w-0 flex-1" |
| 653 | style={listHeader ? { paddingTop: topContentInset } : undefined} |
| 654 | > |
| 655 | {listHeader} |
| 656 | <View className="min-w-0 flex-1" collapsable={false}> |
| 657 | {/* oxlint-disable-next-line react/static-components -- The resolver returns a cached native component with stable identity. */} |
| 658 | <NativeReviewDiffView |
| 659 | collapsable={false} |
| 660 | testID="review-native-diff-view" |
| 661 | refreshing={ |
| 662 | isPullRefreshing || isSelectedSectionPending || areFilePatchesPending |
| 663 | } |
| 664 | onPullToRefresh={() => void handlePullToRefresh()} |
| 665 | style={StyleSheet.absoluteFill} |
| 666 | appearanceScheme={selectedTheme} |
| 667 | collapsedFileIdsJson={nativeBridge.collapsedFileIdsJson} |
| 668 | collapsedCommentIdsJson={nativeBridge.collapsedCommentIdsJson} |
| 669 | contentResetKey={`${reviewCache.threadKey}:${selectedSection.id}`} |
| 670 | contentWidth={NATIVE_REVIEW_DIFF_CONTENT_WIDTH} |
| 671 | contentInsetTop={listHeader ? 0 : topContentInset} |
| 672 | contentInsetBottom={bottomContentInset} |
| 673 | nativeViewRef={nativeReviewDiffViewRef} |
| 674 | rowHeight={nativeReviewDiffStyle.rowHeight} |
| 675 | rowsJson={nativeBridge.rowsJson} |
| 676 | selectedRowIdsJson={nativeBridge.selectedRowIdsJson} |
| 677 | styleJson={nativeBridge.styleJson} |
| 678 | themeJson={nativeBridge.themeJson} |
| 679 | tokensPatchJson={nativeBridge.tokensPatchJson} |
| 680 | tokensResetKey={nativeBridge.tokensResetKey} |
| 681 | viewedFileIdsJson={nativeBridge.viewedFileIdsJson} |
| 682 | onDebug={handleNativeDebug} |
| 683 | onPressLine={commentSelection.onPressLine} |
| 684 | onVisibleFileChange={handleVisibleFileChange} |
| 685 | onToggleComment={nativeBridge.onToggleComment} |
| 686 | onToggleFile={handleNativeToggleFile} |
| 687 | onToggleViewedFile={handleNativeToggleViewedFile} |
| 688 | /> |
| 689 | </View> |
| 690 | </View> |
| 691 | </View> |
| 692 | ) : ( |
| 693 | <ScrollView |
| 694 | key={fallbackScrollViewKey} |
| 695 | contentContainerStyle={ |
| 696 | parsedDiff.kind === "empty" || !selectedSection |
| 697 | ? contentStatusContainerStyle |
| 698 | : undefined |
| 699 | } |
| 700 | contentInsetAdjustmentBehavior="never" |
| 701 | contentInset={{ top: topContentInset, bottom: bottomContentInset }} |
| 702 | contentOffset={{ x: 0, y: -topContentInset }} |
| 703 | automaticallyAdjustsScrollIndicatorInsets={false} |
| 704 | scrollIndicatorInsets={{ top: topContentInset, bottom: bottomContentInset }} |
| 705 | showsVerticalScrollIndicator={false} |
| 706 | className="flex-1" |
| 707 | refreshControl={ |
| 708 | // Fallback and empty states need refresh controls; only Android has an explicit refresh menu. |
| 709 | <RefreshControl |
| 710 | refreshing={isRefreshing} |
| 711 | onRefresh={() => void handlePullToRefresh()} |
| 712 | /> |
| 713 | } |
| 714 | > |
| 715 | {listHeader} |
| 716 | {!selectedSection ? ( |
| 717 | error ? null : ( |
| 718 | <ContentStatus |
| 719 | title={gt("No review diffs")} |
| 720 | detail={gt( |
| 721 | "This thread has no ready turn diffs and the worktree diff is empty.", |
| 722 | )} |
| 723 | /> |
| 724 | ) |
| 725 | ) : selectedSection.isLoading && selectedSection.diff === null ? ( |
| 726 | <ContentStatus loading detail={gt("Loading diff…")} /> |
| 727 | ) : parsedDiff.kind === "empty" ? ( |
| 728 | <ContentStatus |
| 729 | title={gt("No changes")} |
| 730 | detail={selectedSection.subtitle ?? gt("This diff is empty.")} |
| 731 | /> |
| 732 | ) : parsedDiff.kind === "raw" ? ( |
| 733 | <ContentBand spacing="roomy"> |
| 734 | <Text className="text-xs leading-normal text-foreground-muted"> |
| 735 | {parsedDiff.reason} |
| 736 | </Text> |
| 737 | <ScrollView horizontal showsHorizontalScrollIndicator={false} bounces={false}> |
| 738 | <Text selectable className="font-mono text-xs leading-relaxed text-foreground"> |
| 739 | {parsedDiff.text} |
| 740 | </Text> |
| 741 | </ScrollView> |
| 742 | </ContentBand> |
| 743 | ) : parsedDiff.kind === "files" ? ( |
| 744 | <ContentBand spacing="roomy"> |
| 745 | <Text className="text-xs leading-normal text-foreground-muted"> |
| 746 | <T>Native diff view unavailable. Showing the raw patch.</T> |
| 747 | </Text> |
| 748 | <ScrollView horizontal showsHorizontalScrollIndicator={false} bounces={false}> |
| 749 | <Text selectable className="font-mono text-xs leading-relaxed text-foreground"> |
| 750 | {selectedSection?.diff ?? ""} |
| 751 | </Text> |
| 752 | </ScrollView> |
| 753 | </ContentBand> |
| 754 | ) : null} |
| 755 | </ScrollView> |
| 756 | )} |
| 757 | <ReviewSelectionActionBar |
| 758 | bottomInset={insets.bottom} |
| 759 | title={commentSelection.selectionAction?.title ?? null} |
| 760 | onOpenComment={commentSelection.selectionAction?.onOpenComment ?? null} |
| 761 | onClear={commentSelection.clearSelection} |
| 762 | /> |
| 763 | </View> |
| 764 | </ScreenContent> |
| 765 | </> |
| 766 | ); |
| 767 | } |