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 { T, useGT, Var } from "gt-react-native"; |
| 2 | |
| 3 | import { Button } from "./ui/Button"; |
| 4 | import * as Dialog from "./ui/Dialog"; |
| 5 | import { useOpenPullRequest } from "../native/useOpenPullRequest"; |
| 6 | import { SourceFileSurface } from "../features/files/SourceFileSurface"; |
| 7 | import { filePreviewKind } from "@t3tools/shared/filePreview"; |
| 8 | import type { |
| 9 | ComposerContextRecord, |
| 10 | ElementContextSource, |
| 11 | EnvironmentId, |
| 12 | ProjectId, |
| 13 | ScopedThreadRef, |
| 14 | } from "@t3tools/contracts"; |
| 15 | import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments"; |
| 16 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 17 | import { changeRequestTermKind } from "../lib/changeRequestTerm"; |
| 18 | import { videoMimeType } from "@t3tools/shared/video"; |
| 19 | import { Pressable, ScrollView, View } from "react-native"; |
| 20 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 21 | import { REVIEW_MONO_FONT_FAMILY } from "../features/review/reviewDiffRendering"; |
| 22 | import { ReviewCommentCard, useReviewCommentColors } from "../features/review/ReviewCommentCard"; |
| 23 | import { |
| 24 | composerAttachmentInlineUri, |
| 25 | isFileBackedComposerAttachment, |
| 26 | type DraftComposerAttachment, |
| 27 | } from "../lib/composerImages"; |
| 28 | import { contextImageGallery } from "../lib/contextImageGallery"; |
| 29 | import { FilePreviewModal } from "./ui/FilePreviewModal"; |
| 30 | import { VideoPreviewModal } from "./ui/VideoPreviewModal"; |
| 31 | import { ComposerContextAttachment } from "./ComposerContextAttachment"; |
| 32 | import { AppText as Text } from "./ui/AppText"; |
| 33 | import { Sheet } from "./ui/Sheet"; |
| 34 | import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider"; |
| 35 | import { getMobileTerminalTheme } from "../features/terminal/terminalTheme"; |
| 36 | |
| 37 | function ContextField(props: { |
| 38 | label: string; |
| 39 | value: string | null | undefined; |
| 40 | code?: boolean; |
| 41 | language?: "html" | "css"; |
| 42 | }) { |
| 43 | if (!props.value) return null; |
| 44 | return ( |
| 45 | <View className="gap-1"> |
| 46 | <Text className="text-xs text-foreground-muted">{props.label}</Text> |
| 47 | {props.code && props.language ? ( |
| 48 | <View |
| 49 | className="overflow-hidden rounded-xl border border-border" |
| 50 | style={{ height: Math.min(260, Math.max(100, props.value.split("\n").length * 22 + 36)) }} |
| 51 | > |
| 52 | <SourceFileSurface |
| 53 | embedded |
| 54 | contents={props.value} |
| 55 | path={props.language === "html" ? "element.html" : "styles.css"} |
| 56 | /> |
| 57 | </View> |
| 58 | ) : ( |
| 59 | <Text |
| 60 | selectable |
| 61 | className={props.code ? "text-sm text-foreground" : "text-base text-foreground"} |
| 62 | style={props.code ? { fontFamily: REVIEW_MONO_FONT_FAMILY } : undefined} |
| 63 | > |
| 64 | {props.value} |
| 65 | </Text> |
| 66 | )} |
| 67 | </View> |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | function ContextSource(props: { source: ElementContextSource | null }) { |
| 72 | const gt = useGT(); |
| 73 | const source = props.source; |
| 74 | if (!source) return null; |
| 75 | const location = source.fileName |
| 76 | ? `${source.fileName}${source.lineNumber !== null ? `:${source.lineNumber}${source.columnNumber !== null ? `:${source.columnNumber}` : ""}` : ""}` |
| 77 | : null; |
| 78 | return ( |
| 79 | <ContextField |
| 80 | label={gt("Source", { $context: "where an element is defined in source code" })} |
| 81 | value={[source.functionName, location].filter(Boolean).join("\n")} |
| 82 | code |
| 83 | /> |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | export function ComposerContextSheet(props: { |
| 88 | readonly label: string; |
| 89 | readonly record: ComposerContextRecord | undefined; |
| 90 | readonly onClose: () => void; |
| 91 | readonly onRemove?: () => void; |
| 92 | readonly onOpenAttachment?: () => void; |
| 93 | readonly onOpenPullRequest?: () => void; |
| 94 | readonly skillDescription?: string; |
| 95 | readonly onOpenSkill?: () => void; |
| 96 | readonly onOpenThread?: (thread: ScopedThreadRef) => void; |
| 97 | readonly environmentId?: EnvironmentId; |
| 98 | readonly projectId?: ProjectId; |
| 99 | readonly records?: ReadonlyArray<ComposerContextRecord>; |
| 100 | readonly attachments?: ReadonlyArray<DraftComposerAttachment>; |
| 101 | }) { |
| 102 | const openPullRequest = useOpenPullRequest({ |
| 103 | context: |
| 104 | props.environmentId && props.projectId |
| 105 | ? { environmentId: props.environmentId, projectId: props.projectId } |
| 106 | : undefined, |
| 107 | }); |
| 108 | const gt = useGT(); |
| 109 | const reviewColors = useReviewCommentColors(); |
| 110 | const insets = useSafeAreaInsets(); |
| 111 | const { themeId, themeAppearance } = useAppearancePreferences(); |
| 112 | const terminalTheme = getMobileTerminalTheme(themeId, themeAppearance); |
| 113 | const record = props.record; |
| 114 | const localAttachment = |
| 115 | record && "attachmentId" in record |
| 116 | ? props.attachments?.find((entry) => entry.id === record.attachmentId) |
| 117 | : undefined; |
| 118 | const localFile = |
| 119 | localAttachment && isFileBackedComposerAttachment(localAttachment) |
| 120 | ? localAttachment |
| 121 | : undefined; |
| 122 | if (record && !("payload" in record) && (record.kind === "image" || record.kind === "file")) { |
| 123 | const mimeType = videoMimeType(record) ?? record.mimeType; |
| 124 | const previewKind = filePreviewKind(record, { richDocuments: false }); |
| 125 | const resource = { |
| 126 | _tag: "attachment" as const, |
| 127 | attachmentId: record.attachmentId, |
| 128 | fileName: record.name, |
| 129 | mimeType, |
| 130 | }; |
| 131 | const remoteSource = props.environmentId |
| 132 | ? { environmentId: props.environmentId, resource } |
| 133 | : null; |
| 134 | if (videoMimeType(record)) { |
| 135 | if (localFile?.type === "file") { |
| 136 | return ( |
| 137 | <VideoPreviewModal |
| 138 | source={{ type: "local", attachment: localFile }} |
| 139 | onRequestClose={props.onClose} |
| 140 | /> |
| 141 | ); |
| 142 | } |
| 143 | if (remoteSource && !localFile) { |
| 144 | return ( |
| 145 | <VideoPreviewModal |
| 146 | source={{ |
| 147 | type: "media", |
| 148 | name: record.name, |
| 149 | mimeType, |
| 150 | ...remoteSource, |
| 151 | actionsSource: { name: record.name, mimeType, ...remoteSource }, |
| 152 | }} |
| 153 | onRequestClose={props.onClose} |
| 154 | /> |
| 155 | ); |
| 156 | } |
| 157 | } else if (record.kind === "image" || previewKind === "image" || previewKind === "pdf") { |
| 158 | const inlineUri = composerAttachmentInlineUri(localAttachment); |
| 159 | const source = localFile |
| 160 | ? { attachment: localFile } |
| 161 | : inlineUri |
| 162 | ? { uri: inlineUri } |
| 163 | : remoteSource; |
| 164 | if (source) { |
| 165 | return ( |
| 166 | <FilePreviewModal |
| 167 | source={{ |
| 168 | kind: record.kind === "image" || previewKind === "image" ? "image" : "pdf", |
| 169 | name: record.name, |
| 170 | ...source, |
| 171 | gallery: contextImageGallery( |
| 172 | props.records ?? [], |
| 173 | props.attachments ?? [], |
| 174 | props.environmentId, |
| 175 | ), |
| 176 | actionsSource: { name: record.name, mimeType, ...source }, |
| 177 | }} |
| 178 | onRequestClose={props.onClose} |
| 179 | /> |
| 180 | ); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | const attachmentRecord = |
| 185 | record && "attachmentId" in record |
| 186 | ? record |
| 187 | : record?.kind === "preview-annotation" && "screenshotContextId" in record |
| 188 | ? props.records?.find( |
| 189 | (entry) => entry.contextId === record.screenshotContextId && "attachmentId" in entry, |
| 190 | ) |
| 191 | : undefined; |
| 192 | const pullRequestUrl = |
| 193 | record?.kind === "review-comment" && "pullRequest" in record |
| 194 | ? record.pullRequest?.url |
| 195 | : undefined; |
| 196 | const term = changeRequestTermKind( |
| 197 | ( |
| 198 | (pullRequestUrl ? sourceControlClients.findByChangeRequestUrl(pullRequestUrl) : undefined) ?? |
| 199 | sourceControlClients.get(undefined) |
| 200 | ).changeRequest, |
| 201 | ); |
| 202 | const terminal = record?.kind === "terminal" && !("payload" in record) ? record : null; |
| 203 | return ( |
| 204 | <Sheet title={terminal?.terminalLabel ?? props.label} onClose={props.onClose}> |
| 205 | <ScrollView |
| 206 | nestedScrollEnabled |
| 207 | className="flex-1" |
| 208 | contentInsetAdjustmentBehavior="automatic" |
| 209 | contentContainerStyle={{ |
| 210 | paddingHorizontal: 20, |
| 211 | paddingTop: 12, |
| 212 | gap: 16, |
| 213 | paddingBottom: Math.max(20, insets.bottom), |
| 214 | }} |
| 215 | > |
| 216 | {terminal ? ( |
| 217 | <Text className="text-sm text-foreground-muted"> |
| 218 | <T> |
| 219 | Lines <Var>{terminal.lineStart}</Var>–<Var>{terminal.lineEnd}</Var> |
| 220 | </T> |
| 221 | </Text> |
| 222 | ) : null} |
| 223 | {!record ? ( |
| 224 | <Text className="text-foreground"> |
| 225 | <T> |
| 226 | Context unavailable. The reference was copied without its payload. Copy it again from |
| 227 | the original message or remove it. |
| 228 | </T> |
| 229 | </Text> |
| 230 | ) : "payload" in record ? ( |
| 231 | <Text className="text-foreground"> |
| 232 | <T> |
| 233 | This context type is not supported by this version of the app. Its payload will be |
| 234 | preserved when sent. |
| 235 | </T> |
| 236 | </Text> |
| 237 | ) : ( |
| 238 | <> |
| 239 | {record.kind === "terminal" ? ( |
| 240 | <View |
| 241 | className="overflow-hidden rounded-xl" |
| 242 | style={{ backgroundColor: terminalTheme.background }} |
| 243 | > |
| 244 | <ScrollView |
| 245 | horizontal |
| 246 | showsHorizontalScrollIndicator |
| 247 | contentContainerStyle={{ padding: 12 }} |
| 248 | > |
| 249 | <Text |
| 250 | selectable |
| 251 | className="text-sm text-foreground" |
| 252 | style={{ |
| 253 | fontFamily: REVIEW_MONO_FONT_FAMILY, |
| 254 | fontSize: 13, |
| 255 | lineHeight: 20, |
| 256 | color: terminalTheme.foreground, |
| 257 | }} |
| 258 | > |
| 259 | {record.text} |
| 260 | </Text> |
| 261 | </ScrollView> |
| 262 | </View> |
| 263 | ) : null} |
| 264 | {record.kind === "review-comment" ? ( |
| 265 | <> |
| 266 | {record.pullRequest ? ( |
| 267 | <ContextField |
| 268 | label={`#${record.pullRequest.number} · ${ |
| 269 | record.pullRequest.isDraft |
| 270 | ? gt("draft", { $context: "pull request state" }) |
| 271 | : record.pullRequest.state === "open" |
| 272 | ? gt("open", { $context: "pull request state" }) |
| 273 | : record.pullRequest.state === "closed" |
| 274 | ? gt("closed", { $context: "pull request state" }) |
| 275 | : gt("merged", { $context: "pull request state" }) |
| 276 | }`} |
| 277 | value={`${record.pullRequest.title}\n${record.pullRequest.headBranch} → ${record.pullRequest.baseBranch}`} |
| 278 | /> |
| 279 | ) : null} |
| 280 | {!record.sectionId.startsWith("pull-request:") ? ( |
| 281 | <> |
| 282 | <ReviewCommentCard |
| 283 | comment={{ ...record, id: record.contextId }} |
| 284 | colors={reviewColors} |
| 285 | /> |
| 286 | </> |
| 287 | ) : null} |
| 288 | </> |
| 289 | ) : null} |
| 290 | {record.kind === "preview-annotation" ? ( |
| 291 | <> |
| 292 | <ContextField |
| 293 | label={gt("Page", { $context: "web page" })} |
| 294 | value={record.pageTitle ?? record.pageUrl} |
| 295 | /> |
| 296 | <ContextField label={gt("URL")} value={record.pageUrl} /> |
| 297 | <ContextField label={gt("Comment", { $context: "noun" })} value={record.comment} /> |
| 298 | <ContextField |
| 299 | label={gt("Selection", { $context: "selected part of a web page" })} |
| 300 | value={record.targetSummary} |
| 301 | /> |
| 302 | <ContextField |
| 303 | label={gt("Requested changes")} |
| 304 | value={record.styleChanges.join("\n")} |
| 305 | /> |
| 306 | {record.elements?.map( |
| 307 | (element, index) => ( |
| 308 | /* oxlint-disable react/no-array-index-key -- Captured DOM elements are a fixed snapshot without persistent element IDs. */ <View |
| 309 | key={`${element.selector ?? element.tagName}:${index}`} |
| 310 | className="gap-3" |
| 311 | > |
| 312 | <ContextField |
| 313 | label={gt("Element", { $context: "HTML element on a web page" })} |
| 314 | value={element.componentName ?? element.tagName} |
| 315 | /> |
| 316 | <ContextField |
| 317 | label={gt("Selector", { $context: "CSS selector" })} |
| 318 | value={element.selector} |
| 319 | code |
| 320 | /> |
| 321 | <ContextSource source={element.source} /> |
| 322 | <ContextField |
| 323 | label={gt("HTML")} |
| 324 | value={element.htmlPreview} |
| 325 | code |
| 326 | language="html" |
| 327 | /> |
| 328 | <ContextField |
| 329 | label={gt("Styles", { $context: "CSS styles" })} |
| 330 | value={element.styles} |
| 331 | code |
| 332 | language="css" |
| 333 | /> |
| 334 | </View> |
| 335 | ) /* oxlint-enable react/no-array-index-key */, |
| 336 | )} |
| 337 | </> |
| 338 | ) : null} |
| 339 | {record.kind === "element" ? ( |
| 340 | <> |
| 341 | <ContextField label={gt("Page", { $context: "web page" })} value={record.pageUrl} /> |
| 342 | <ContextField |
| 343 | label={gt("Element", { $context: "HTML element on a web page" })} |
| 344 | value={record.componentName ?? record.tagName} |
| 345 | /> |
| 346 | <ContextField |
| 347 | label={gt("Selector", { $context: "CSS selector" })} |
| 348 | value={record.selector} |
| 349 | code |
| 350 | /> |
| 351 | <ContextSource source={record.source} /> |
| 352 | <ContextField label={gt("HTML")} value={record.htmlPreview} code language="html" /> |
| 353 | <ContextField |
| 354 | label={gt("Styles", { $context: "CSS styles" })} |
| 355 | value={record.styles} |
| 356 | code |
| 357 | language="css" |
| 358 | /> |
| 359 | </> |
| 360 | ) : null} |
| 361 | {record.kind === "image" ? ( |
| 362 | <ContextField |
| 363 | label={gt("File", { $context: "noun" })} |
| 364 | value={`${record.mimeType} · ${formatAttachmentSize(record.sizeBytes)}`} |
| 365 | /> |
| 366 | ) : null} |
| 367 | {record.kind === "mention" ? ( |
| 368 | <ContextField |
| 369 | label={gt("Path", { $context: "file path" })} |
| 370 | value={record.path} |
| 371 | code |
| 372 | /> |
| 373 | ) : null} |
| 374 | {record.kind === "thread" ? ( |
| 375 | <View className="gap-3"> |
| 376 | <ContextField label={gt("Thread", { $context: "noun" })} value={record.title} /> |
| 377 | {props.onOpenThread ? ( |
| 378 | <Button |
| 379 | label={gt("Open thread")} |
| 380 | tone="secondary" |
| 381 | onPress={() => |
| 382 | props.onOpenThread?.({ |
| 383 | environmentId: record.environmentId, |
| 384 | threadId: record.threadId, |
| 385 | }) |
| 386 | } |
| 387 | fullWidth |
| 388 | /> |
| 389 | ) : null} |
| 390 | </View> |
| 391 | ) : null} |
| 392 | {record.kind === "environment" ? ( |
| 393 | <ContextField label={gt("Machine", { $context: "computer" })} value={record.label} /> |
| 394 | ) : null} |
| 395 | {record.kind === "skill" ? ( |
| 396 | <View className="gap-3"> |
| 397 | <ContextField |
| 398 | label={gt("Skill", { $context: "agent skill" })} |
| 399 | value={record.name} |
| 400 | /> |
| 401 | <ContextField |
| 402 | label={gt("Description")} |
| 403 | value={ |
| 404 | props.skillDescription ?? gt("No description is available for this skill.") |
| 405 | } |
| 406 | /> |
| 407 | {props.onOpenSkill ? ( |
| 408 | <Button |
| 409 | label={gt("View instructions")} |
| 410 | tone="secondary" |
| 411 | onPress={props.onOpenSkill} |
| 412 | fullWidth |
| 413 | /> |
| 414 | ) : null} |
| 415 | </View> |
| 416 | ) : null} |
| 417 | </> |
| 418 | )} |
| 419 | {attachmentRecord && "attachmentId" in attachmentRecord ? ( |
| 420 | <ComposerContextAttachment |
| 421 | key={JSON.stringify([ |
| 422 | props.environmentId, |
| 423 | attachmentRecord.attachmentId, |
| 424 | props.attachments?.find((entry) => entry.id === attachmentRecord.attachmentId) |
| 425 | ?.fileUri, |
| 426 | ])} |
| 427 | record={attachmentRecord} |
| 428 | environmentId={props.environmentId} |
| 429 | attachment={props.attachments?.find( |
| 430 | (entry) => entry.id === attachmentRecord.attachmentId, |
| 431 | )} |
| 432 | /> |
| 433 | ) : null} |
| 434 | {pullRequestUrl && /^https?:\/\//i.test(pullRequestUrl) ? ( |
| 435 | <Pressable |
| 436 | accessibilityRole="link" |
| 437 | onPress={() => { |
| 438 | props.onClose(); |
| 439 | void openPullRequest(pullRequestUrl).catch(() => |
| 440 | Dialog.alert( |
| 441 | gt( |
| 442 | "Could not open {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 443 | { term }, |
| 444 | ), |
| 445 | gt("Try again when connected."), |
| 446 | ), |
| 447 | ); |
| 448 | }} |
| 449 | className="rounded-xl bg-subtle p-4" |
| 450 | > |
| 451 | <Text className="text-foreground"> |
| 452 | {gt( |
| 453 | "Open {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 454 | { term }, |
| 455 | )} |
| 456 | </Text> |
| 457 | </Pressable> |
| 458 | ) : null} |
| 459 | {props.onOpenAttachment ? ( |
| 460 | <Button |
| 461 | label={gt("Open attachment")} |
| 462 | tone="secondary" |
| 463 | onPress={props.onOpenAttachment} |
| 464 | fullWidth |
| 465 | /> |
| 466 | ) : null} |
| 467 | {props.onOpenPullRequest && !pullRequestUrl ? ( |
| 468 | <Button |
| 469 | label={gt( |
| 470 | "Open {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 471 | { term }, |
| 472 | )} |
| 473 | tone="secondary" |
| 474 | onPress={props.onOpenPullRequest} |
| 475 | fullWidth |
| 476 | /> |
| 477 | ) : null} |
| 478 | {props.onRemove ? ( |
| 479 | <Button |
| 480 | label={gt("Remove from draft")} |
| 481 | tone="secondary" |
| 482 | onPress={props.onRemove} |
| 483 | fullWidth |
| 484 | /> |
| 485 | ) : null} |
| 486 | </ScrollView> |
| 487 | </Sheet> |
| 488 | ); |
| 489 | } |