The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 2 | import { TextInputWrapper } from "expo-paste-input"; |
| 3 | import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 4 | import { T, useGT } from "gt-react-native"; |
| 5 | import { useMemo, useState } from "react"; |
| 6 | import { ScrollView, View, useWindowDimensions } from "react-native"; |
| 7 | import * as Dialog from "../../components/ui/Dialog"; |
| 8 | import { FilePreviewModal, type FilePreviewSource } from "../../components/ui/FilePreviewModal"; |
| 9 | |
| 10 | import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText"; |
| 11 | import { KeyboardSheetBody } from "../../components/ui/KeyboardSheetBody"; |
| 12 | import { SheetHeader } from "../../components/ui/SheetHeader"; |
| 13 | import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStrip"; |
| 14 | import { ControlPill } from "../../components/ui/ControlPill"; |
| 15 | import { cn } from "../../lib/cn"; |
| 16 | import type { DraftComposerImageAttachment } from "../../lib/composerImages"; |
| 17 | import { convertPastedImagesToAttachments, pickComposerImages } from "../../lib/composerImages"; |
| 18 | import { useNativePaste } from "../../lib/useNativePaste"; |
| 19 | import { setPendingConnectionError } from "../../state/use-remote-environment-registry"; |
| 20 | import { appendReviewCommentToDraft } from "../../state/use-thread-composer-state"; |
| 21 | import { |
| 22 | clearReviewCommentTarget, |
| 23 | formatReviewCommentContext, |
| 24 | getReviewUnifiedLineNumber, |
| 25 | getSelectedReviewCommentLines, |
| 26 | useReviewCommentTarget, |
| 27 | } from "./reviewCommentSelection"; |
| 28 | import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface"; |
| 29 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 30 | import { changeTone, DiffTokenText, ReviewChangeBar } from "./reviewDiffRendering"; |
| 31 | import { highlightReviewSelectedLines } from "./highlightReviewSelectedLines"; |
| 32 | |
| 33 | const REVIEW_COMMENT_PREVIEW_MAX_LINES = 5; |
| 34 | const NO_LINES: ReturnType<typeof getSelectedReviewCommentLines> = []; |
| 35 | const NO_HIGHLIGHTS: ReturnType<typeof highlightReviewSelectedLines> = {}; |
| 36 | |
| 37 | type ReviewCommentComposerSheetProps = StaticScreenProps<{ |
| 38 | readonly environmentId: EnvironmentId; |
| 39 | readonly threadId: ThreadId; |
| 40 | }>; |
| 41 | |
| 42 | export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProps) { |
| 43 | const navigation = useNavigation(); |
| 44 | const gt = useGT(); |
| 45 | const { width } = useWindowDimensions(); |
| 46 | const { themeAppearance: selectedTheme } = useAppearancePreferences(); |
| 47 | const target = useReviewCommentTarget(); |
| 48 | const { codeSurface } = useAppearanceCodeSurface(); |
| 49 | const { environmentId, threadId } = props.route.params; |
| 50 | const [commentText, setCommentText] = useState(""); |
| 51 | const [attachments, setAttachments] = useState<ReadonlyArray<DraftComposerImageAttachment>>([]); |
| 52 | const [previewFile, setPreviewFile] = useState<FilePreviewSource | null>(null); |
| 53 | |
| 54 | const { selectedLines, highlightedLinesById } = useMemo(() => { |
| 55 | if (!target) return { selectedLines: NO_LINES, highlightedLinesById: NO_HIGHLIGHTS }; |
| 56 | const lines = getSelectedReviewCommentLines(target); |
| 57 | return { |
| 58 | selectedLines: lines, |
| 59 | highlightedLinesById: highlightReviewSelectedLines({ |
| 60 | filePath: target.filePath, |
| 61 | lines, |
| 62 | theme: selectedTheme, |
| 63 | }), |
| 64 | }; |
| 65 | }, [selectedTheme, target]); |
| 66 | const firstLine = selectedLines[0] ?? null; |
| 67 | const lastLine = selectedLines[selectedLines.length - 1] ?? null; |
| 68 | const firstNumber = firstLine ? getReviewUnifiedLineNumber(firstLine) : null; |
| 69 | const lastNumber = lastLine ? getReviewUnifiedLineNumber(lastLine) : null; |
| 70 | const canSubmit = |
| 71 | commentText.trim().length > 0 && target !== null && !!environmentId && !!threadId; |
| 72 | const selectionLabel = |
| 73 | selectedLines.length === 1 |
| 74 | ? firstNumber !== null |
| 75 | ? gt("Line {line}", { line: firstNumber }) |
| 76 | : gt("File comment") |
| 77 | : firstNumber !== null && lastNumber !== null |
| 78 | ? gt("Lines {first}-{last}", { first: firstNumber, last: lastNumber }) |
| 79 | : gt("{count, plural, one {{count} lines selected} other {{count} lines selected}}", { |
| 80 | count: selectedLines.length, |
| 81 | }); |
| 82 | const previewHeight = Math.max( |
| 83 | Math.min(selectedLines.length, REVIEW_COMMENT_PREVIEW_MAX_LINES) * codeSurface.rowHeight, |
| 84 | codeSurface.rowHeight, |
| 85 | ); |
| 86 | const previewViewportWidth = Math.max(width - 40, 280); |
| 87 | const dismissComposer = () => { |
| 88 | clearReviewCommentTarget(); |
| 89 | navigation.goBack(); |
| 90 | }; |
| 91 | const handleNativePaste = useNativePaste((uris) => { |
| 92 | void (async () => { |
| 93 | try { |
| 94 | const result = await convertPastedImagesToAttachments({ |
| 95 | uris, |
| 96 | existingCount: attachments.length, |
| 97 | }); |
| 98 | if (result.attachments.length > 0) { |
| 99 | setAttachments((current) => [...current, ...result.attachments]); |
| 100 | } |
| 101 | if (result.error) Dialog.alert(gt("Could not paste image"), result.error); |
| 102 | } catch (error) { |
| 103 | console.error("[review comment] error converting pasted images", error); |
| 104 | } |
| 105 | })(); |
| 106 | }); |
| 107 | |
| 108 | async function handlePickImages(): Promise<void> { |
| 109 | const result = await pickComposerImages({ existingCount: attachments.length }); |
| 110 | if (result.images.length > 0) { |
| 111 | setAttachments((current) => [...current, ...result.images]); |
| 112 | } |
| 113 | if (result.error) { |
| 114 | setPendingConnectionError(result.error); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const handleSubmit = () => { |
| 119 | if (!target || !environmentId || !threadId || commentText.trim().length === 0) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | appendReviewCommentToDraft({ |
| 124 | environmentId, |
| 125 | threadId, |
| 126 | text: formatReviewCommentContext(target, commentText), |
| 127 | attachments, |
| 128 | }); |
| 129 | setAttachments([]); |
| 130 | dismissComposer(); |
| 131 | }; |
| 132 | |
| 133 | return ( |
| 134 | <View className="flex-1 bg-sheet"> |
| 135 | <SheetHeader title={gt("Add Comment")} onClose={dismissComposer} /> |
| 136 | <KeyboardSheetBody |
| 137 | toolbar={ |
| 138 | target ? ( |
| 139 | <> |
| 140 | <ControlPill |
| 141 | accessibilityLabel={gt("Add image")} |
| 142 | icon="plus" |
| 143 | onPress={() => void handlePickImages()} |
| 144 | /> |
| 145 | <View className="flex-1" /> |
| 146 | <ControlPill |
| 147 | accessibilityLabel={gt("Comment", { $context: "verb, submit button" })} |
| 148 | icon="arrow.up" |
| 149 | label={gt("Comment", { $context: "verb, submit button" })} |
| 150 | variant="primary" |
| 151 | disabled={!canSubmit} |
| 152 | onPress={handleSubmit} |
| 153 | /> |
| 154 | </> |
| 155 | ) : undefined |
| 156 | } |
| 157 | > |
| 158 | {!target ? ( |
| 159 | <View className="rounded-[22px] border border-border bg-card px-4 py-5"> |
| 160 | <Text className="text-base font-t3-bold text-foreground"> |
| 161 | <T>No selection</T> |
| 162 | </Text> |
| 163 | <Text className="mt-1 text-sm leading-normal text-foreground-muted"> |
| 164 | <T>Select a diff line or range first.</T> |
| 165 | </Text> |
| 166 | </View> |
| 167 | ) : ( |
| 168 | <View className="min-h-0 flex-1 gap-4"> |
| 169 | <View className="gap-1 px-1"> |
| 170 | <Text className="text-2xs font-t3-bold uppercase text-foreground-muted"> |
| 171 | {selectionLabel} |
| 172 | </Text> |
| 173 | <Text |
| 174 | className="font-mono text-xs leading-snug text-foreground-muted" |
| 175 | ellipsizeMode="middle" |
| 176 | numberOfLines={2} |
| 177 | > |
| 178 | {target.filePath} |
| 179 | </Text> |
| 180 | </View> |
| 181 | |
| 182 | <View className="overflow-hidden rounded-[22px] border border-border bg-card"> |
| 183 | <ScrollView |
| 184 | horizontal |
| 185 | bounces={false} |
| 186 | keyboardShouldPersistTaps="always" |
| 187 | showsHorizontalScrollIndicator={false} |
| 188 | > |
| 189 | <ScrollView |
| 190 | bounces={false} |
| 191 | scrollEnabled={selectedLines.length > REVIEW_COMMENT_PREVIEW_MAX_LINES} |
| 192 | nestedScrollEnabled |
| 193 | keyboardShouldPersistTaps="always" |
| 194 | showsVerticalScrollIndicator={ |
| 195 | selectedLines.length > REVIEW_COMMENT_PREVIEW_MAX_LINES |
| 196 | } |
| 197 | style={{ height: previewHeight }} |
| 198 | > |
| 199 | <View style={{ minWidth: previewViewportWidth }}> |
| 200 | {selectedLines.map((line) => { |
| 201 | const lineNumber = getReviewUnifiedLineNumber(line); |
| 202 | |
| 203 | return ( |
| 204 | <View |
| 205 | key={line.id} |
| 206 | className={cn("flex-row items-start", changeTone(line.change))} |
| 207 | style={{ height: codeSurface.rowHeight }} |
| 208 | > |
| 209 | <ReviewChangeBar change={line.change} height={codeSurface.rowHeight} /> |
| 210 | <Text className="w-9 py-1 pr-1 text-right text-2xs font-mono text-foreground-muted"> |
| 211 | {lineNumber ?? ""} |
| 212 | </Text> |
| 213 | <View className="min-w-0 flex-1 shrink-0 px-1 py-1"> |
| 214 | <DiffTokenText |
| 215 | fallback={line.content} |
| 216 | tokens={highlightedLinesById[line.id] ?? null} |
| 217 | change={line.change} |
| 218 | fontSize={codeSurface.fontSize} |
| 219 | lineHeight={codeSurface.rowHeight} |
| 220 | /> |
| 221 | </View> |
| 222 | </View> |
| 223 | ); |
| 224 | })} |
| 225 | </View> |
| 226 | </ScrollView> |
| 227 | </ScrollView> |
| 228 | </View> |
| 229 | |
| 230 | <View className="min-h-0 flex-1 gap-2"> |
| 231 | <Text className="text-sm font-t3-bold text-foreground"> |
| 232 | <T context="noun, field label">Comment</T> |
| 233 | </Text> |
| 234 | <View className="min-h-[132px] flex-1 overflow-hidden rounded-[20px] border border-border bg-card"> |
| 235 | <View className="min-h-0 flex-1 px-4 pt-3.5"> |
| 236 | <TextInputWrapper onPaste={handleNativePaste} style={{ flex: 1, minHeight: 0 }}> |
| 237 | <TextInput |
| 238 | autoFocus |
| 239 | multiline |
| 240 | scrollEnabled |
| 241 | placeholder={gt("Leave a comment...")} |
| 242 | textAlignVertical="top" |
| 243 | value={commentText} |
| 244 | onChangeText={setCommentText} |
| 245 | className="h-full min-h-0 flex-1 border-0 bg-transparent px-0 py-0 font-sans text-base" |
| 246 | /> |
| 247 | </TextInputWrapper> |
| 248 | </View> |
| 249 | {attachments.length > 0 ? ( |
| 250 | <View className="px-4 pb-3 pt-2"> |
| 251 | <ComposerAttachmentStrip |
| 252 | attachments={attachments} |
| 253 | imageBorderRadius={16} |
| 254 | imageSize={60} |
| 255 | onPressPreview={setPreviewFile} |
| 256 | removeButtonPlacement="gutter" |
| 257 | onRemove={(imageId) => { |
| 258 | setAttachments((current) => |
| 259 | current.filter((image) => image.id !== imageId), |
| 260 | ); |
| 261 | }} |
| 262 | /> |
| 263 | </View> |
| 264 | ) : null} |
| 265 | </View> |
| 266 | </View> |
| 267 | </View> |
| 268 | )} |
| 269 | </KeyboardSheetBody> |
| 270 | <FilePreviewModal source={previewFile} onRequestClose={() => setPreviewFile(null)} /> |
| 271 | </View> |
| 272 | ); |
| 273 | } |