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 { useGT } from "gt-react-native"; |
| 2 | import { FilePreviewGallery, useFilePreviewGallery } from "./ui/FilePreviewGallery"; |
| 3 | import { composerImageGallery } from "../lib/composerImageGallery"; |
| 4 | import { SymbolView } from "./ui/AppSymbol"; |
| 5 | import { imageMimeType } from "@t3tools/shared/image"; |
| 6 | import { videoMimeType } from "@t3tools/shared/video"; |
| 7 | import { useAtomValue } from "@effect/atom-react"; |
| 8 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 9 | import { Paths } from "expo-file-system"; |
| 10 | import { Image, Pressable, ScrollView, View } from "react-native"; |
| 11 | |
| 12 | import { AppText as Text } from "./ui/AppText"; |
| 13 | import { MaterializedPreviewKey, materializedPreviewAtom } from "../lib/composerImagePreview"; |
| 14 | import { PierreEntryIcon } from "./PierreEntryIcon"; |
| 15 | import { |
| 16 | isFileBackedComposerAttachment, |
| 17 | type DraftComposerAttachment, |
| 18 | type DraftComposerFileAttachment, |
| 19 | type DraftComposerImageAttachment, |
| 20 | } from "../lib/composerImages"; |
| 21 | import { resolveOwnedComposerAttachmentFileUri } from "../lib/composerAttachmentFiles"; |
| 22 | import { rememberDraftImage } from "../lib/sentImageHandoff"; |
| 23 | import { VideoAttachmentTile } from "./VideoAttachmentTile"; |
| 24 | import { type MediaActionsSource } from "../lib/mediaActionsSource"; |
| 25 | import { PresentationSource } from "./ui/NativePresentation"; |
| 26 | import type { FilePreviewSource } from "./ui/FilePreviewModal"; |
| 27 | import { isPdfFile } from "../lib/filePreview"; |
| 28 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 29 | import { |
| 30 | retryComposerAttachmentUpload, |
| 31 | useComposerAttachmentUploadState, |
| 32 | } from "../state/composer-attachment-uploads"; |
| 33 | |
| 34 | export interface ComposerAttachmentStripProps { |
| 35 | readonly environmentId?: EnvironmentId; |
| 36 | readonly attachments: ReadonlyArray<DraftComposerAttachment>; |
| 37 | readonly onRemove: (imageId: string) => void; |
| 38 | readonly onPressPreview?: (source: FilePreviewSource) => void; |
| 39 | readonly onPressVideo?: ( |
| 40 | attachment: DraftComposerFileAttachment, |
| 41 | sourceIdentifier: string, |
| 42 | ) => void; |
| 43 | readonly onPressDocument?: (attachment: DraftComposerFileAttachment) => void; |
| 44 | readonly imageSize?: number; |
| 45 | readonly imageBorderRadius?: number; |
| 46 | readonly removeButtonPlacement?: "overlay" | "gutter"; |
| 47 | } |
| 48 | |
| 49 | type ComposerAttachmentThumbnailProps = { |
| 50 | readonly environmentId?: EnvironmentId; |
| 51 | readonly attachment: DraftComposerAttachment; |
| 52 | readonly size: number; |
| 53 | readonly borderRadius: number; |
| 54 | readonly compact?: boolean; |
| 55 | readonly onPressPreview?: (source: FilePreviewSource) => void; |
| 56 | readonly onPressVideo?: ( |
| 57 | attachment: DraftComposerFileAttachment, |
| 58 | sourceIdentifier: string, |
| 59 | ) => void; |
| 60 | readonly onPressDocument?: (attachment: DraftComposerFileAttachment) => void; |
| 61 | }; |
| 62 | |
| 63 | export function ComposerAttachmentThumbnail(props: ComposerAttachmentThumbnailProps) { |
| 64 | const gt = useGT(); |
| 65 | const upload = useComposerAttachmentUploadState(props.environmentId, props.attachment.id); |
| 66 | return ( |
| 67 | <View style={{ width: props.size, height: props.size }}> |
| 68 | <ComposerAttachmentContent {...props} /> |
| 69 | {upload?.status === "failed" ? ( |
| 70 | <View |
| 71 | pointerEvents="none" |
| 72 | className="absolute inset-0 border-2 border-danger-foreground" |
| 73 | style={{ borderRadius: props.borderRadius }} |
| 74 | /> |
| 75 | ) : null} |
| 76 | {upload && upload.status !== "ready" ? ( |
| 77 | <Pressable |
| 78 | accessibilityRole={upload.status === "failed" ? "button" : "text"} |
| 79 | accessibilityLabel={ |
| 80 | upload.status === "failed" |
| 81 | ? gt("Retry uploading {name}", { name: props.attachment.name }) |
| 82 | : gt("Uploading {name}, {percent}%", { |
| 83 | name: props.attachment.name, |
| 84 | percent: Math.floor(upload.progress * 100), |
| 85 | }) |
| 86 | } |
| 87 | accessibilityHint={upload.status === "failed" ? upload.reason : undefined} |
| 88 | disabled={upload.status !== "failed"} |
| 89 | onPress={() => |
| 90 | props.environmentId && |
| 91 | retryComposerAttachmentUpload(props.environmentId, props.attachment.id) |
| 92 | } |
| 93 | className="absolute bottom-0.5 left-0.5 flex-row items-center gap-0.5 rounded-full bg-black/70 px-1 py-0.5" |
| 94 | > |
| 95 | <SymbolView |
| 96 | name={upload.status === "failed" ? "arrow.clockwise" : "arrow.up"} |
| 97 | size={props.compact ? 8 : 10} |
| 98 | tintColor="#ffffff" |
| 99 | type="monochrome" |
| 100 | /> |
| 101 | {!props.compact ? ( |
| 102 | <Text className="text-2xs text-white"> |
| 103 | {upload.status === "failed" ? gt("Retry") : `${Math.floor(upload.progress * 100)}%`} |
| 104 | </Text> |
| 105 | ) : null} |
| 106 | </Pressable> |
| 107 | ) : null} |
| 108 | </View> |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** Fabric re-parses URLs each layout; large data URLs can stall and abort commits. */ |
| 113 | const INLINE_PREVIEW_FALLBACK_MAX_CHARS = 256_000; |
| 114 | |
| 115 | const noMaterializedPreview = Atom.make(AsyncResult.initial<string | null>()).pipe( |
| 116 | Atom.withLabel("composer-image-preview:none"), |
| 117 | ); |
| 118 | |
| 119 | function useComposerImagePreviewUri(attachment: DraftComposerImageAttachment): string | null { |
| 120 | const { fileUri, previewUri } = attachment; |
| 121 | const inlinePreview = fileUri === undefined && previewUri.startsWith("data:"); |
| 122 | const materialized = useAtomValue( |
| 123 | inlinePreview |
| 124 | ? materializedPreviewAtom(new MaterializedPreviewKey(attachment)) |
| 125 | : noMaterializedPreview, |
| 126 | ); |
| 127 | if (fileUri !== undefined) { |
| 128 | // Reinstalls move the app container, so a stored file URI may point at the old one. |
| 129 | const owned = resolveOwnedComposerAttachmentFileUri(fileUri, Paths.document.uri); |
| 130 | if (owned !== null) return owned; |
| 131 | return previewUri.startsWith("data:") ? fileUri : previewUri; |
| 132 | } |
| 133 | if (inlinePreview) { |
| 134 | if (!AsyncResult.isSuccess(materialized)) return null; |
| 135 | return ( |
| 136 | materialized.value ?? |
| 137 | (previewUri.length <= INLINE_PREVIEW_FALLBACK_MAX_CHARS ? previewUri : null) |
| 138 | ); |
| 139 | } |
| 140 | return previewUri; |
| 141 | } |
| 142 | |
| 143 | function ComposerImageAttachment( |
| 144 | props: ComposerAttachmentThumbnailProps & { readonly attachment: DraftComposerImageAttachment }, |
| 145 | ) { |
| 146 | const gt = useGT(); |
| 147 | const { attachment } = props; |
| 148 | const withGallery = useFilePreviewGallery(); |
| 149 | const style = { width: props.size, height: props.size, borderRadius: props.borderRadius }; |
| 150 | const previewUri = useComposerImagePreviewUri(attachment); |
| 151 | const sourceIdentifier = `draft-image:${attachment.id}`; |
| 152 | return ( |
| 153 | <PresentationSource identifier={sourceIdentifier}> |
| 154 | <Pressable |
| 155 | accessibilityRole="imagebutton" |
| 156 | accessibilityLabel={gt("Open {name}", { name: attachment.name })} |
| 157 | disabled={!props.onPressPreview} |
| 158 | onPress={() => |
| 159 | props.onPressPreview?.( |
| 160 | withGallery( |
| 161 | isFileBackedComposerAttachment(attachment) |
| 162 | ? { kind: "image", attachment, name: attachment.name, sourceIdentifier } |
| 163 | : { |
| 164 | kind: "image", |
| 165 | uri: attachment.dataUrl ?? attachment.previewUri, |
| 166 | name: attachment.name, |
| 167 | sourceIdentifier, |
| 168 | }, |
| 169 | ), |
| 170 | ) |
| 171 | } |
| 172 | > |
| 173 | <Image |
| 174 | source={previewUri === null ? undefined : { uri: previewUri }} |
| 175 | style={style} |
| 176 | className="bg-subtle" |
| 177 | resizeMode="cover" |
| 178 | onLoad={({ nativeEvent: { source } }) => { |
| 179 | if (previewUri !== null) |
| 180 | rememberDraftImage(attachment.id, { |
| 181 | uri: previewUri, |
| 182 | aspectRatio: source.width / source.height, |
| 183 | }); |
| 184 | }} |
| 185 | /> |
| 186 | </Pressable> |
| 187 | </PresentationSource> |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | function ComposerAttachmentContent(props: ComposerAttachmentThumbnailProps) { |
| 192 | const { attachment } = props; |
| 193 | // The document picker types every pick as a plain file, so a picture arrives here as one. |
| 194 | if (attachment.type === "image" || imageMimeType(attachment) !== null) { |
| 195 | const { source: _droppedSource, ...rest } = attachment; |
| 196 | return ( |
| 197 | <ComposerImageAttachment |
| 198 | {...props} |
| 199 | attachment={ |
| 200 | attachment.type === "image" |
| 201 | ? attachment |
| 202 | : { ...rest, type: "image", previewUri: attachment.fileUri } |
| 203 | } |
| 204 | /> |
| 205 | ); |
| 206 | } |
| 207 | const onPressVideo = props.onPressVideo; |
| 208 | if (onPressVideo && videoMimeType(attachment) !== null) { |
| 209 | return ( |
| 210 | <ComposerVideoAttachment {...props} attachment={attachment} onPressVideo={onPressVideo} /> |
| 211 | ); |
| 212 | } |
| 213 | return <ComposerFileAttachment {...props} attachment={attachment} />; |
| 214 | } |
| 215 | |
| 216 | function ComposerFileAttachment( |
| 217 | props: ComposerAttachmentThumbnailProps & { readonly attachment: DraftComposerFileAttachment }, |
| 218 | ) { |
| 219 | const gt = useGT(); |
| 220 | const { attachment } = props; |
| 221 | const style = { width: props.size, height: props.size, borderRadius: props.borderRadius }; |
| 222 | const canPreview = isPdfFile(attachment) && props.onPressPreview !== undefined; |
| 223 | const sourceIdentifier = `draft-file:${attachment.id}`; |
| 224 | const onPressDocument = props.onPressDocument; |
| 225 | const upload = useComposerAttachmentUploadState(props.environmentId, attachment.id); |
| 226 | const showsUploadBadge = upload !== undefined && upload.status !== "ready"; |
| 227 | return ( |
| 228 | <> |
| 229 | <PresentationSource identifier={sourceIdentifier}> |
| 230 | <Pressable |
| 231 | accessibilityRole="button" |
| 232 | accessibilityLabel={gt("Open {name}", { name: attachment.name })} |
| 233 | disabled={!canPreview && onPressDocument === undefined} |
| 234 | onPress={() => |
| 235 | canPreview |
| 236 | ? props.onPressPreview?.({ |
| 237 | kind: "pdf", |
| 238 | name: attachment.name, |
| 239 | attachment, |
| 240 | sourceIdentifier, |
| 241 | }) |
| 242 | : onPressDocument?.(attachment) |
| 243 | } |
| 244 | className={ |
| 245 | props.compact |
| 246 | ? "items-center justify-center bg-subtle" |
| 247 | : `items-center justify-center gap-1 bg-subtle px-2${showsUploadBadge ? " pb-4" : ""}` |
| 248 | } |
| 249 | style={style} |
| 250 | > |
| 251 | <PierreEntryIcon path={attachment.name} kind="file" size={props.compact ? 15 : 22} /> |
| 252 | {!props.compact ? ( |
| 253 | <Text className="w-full text-center text-2xs text-foreground" numberOfLines={1}> |
| 254 | {attachment.name} |
| 255 | </Text> |
| 256 | ) : null} |
| 257 | </Pressable> |
| 258 | </PresentationSource> |
| 259 | </> |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | function ComposerVideoAttachment(props: { |
| 264 | readonly attachment: DraftComposerFileAttachment; |
| 265 | readonly size: number; |
| 266 | readonly borderRadius: number; |
| 267 | readonly compact?: boolean; |
| 268 | readonly onPressVideo: ( |
| 269 | attachment: DraftComposerFileAttachment, |
| 270 | sourceIdentifier: string, |
| 271 | ) => void; |
| 272 | }) { |
| 273 | const { attachment } = props; |
| 274 | const sourceIdentifier = `draft:${attachment.id}`; |
| 275 | const style = { width: props.size, height: props.size, borderRadius: props.borderRadius }; |
| 276 | const actionsSource: MediaActionsSource = { |
| 277 | name: attachment.name, |
| 278 | mimeType: videoMimeType(attachment) ?? attachment.mimeType, |
| 279 | sourceIdentifier, |
| 280 | attachment, |
| 281 | }; |
| 282 | |
| 283 | return ( |
| 284 | <VideoAttachmentTile |
| 285 | name={attachment.name} |
| 286 | sourceIdentifier={sourceIdentifier} |
| 287 | thumbnailSource={attachment} |
| 288 | compact={props.compact} |
| 289 | onPress={() => props.onPressVideo(attachment, sourceIdentifier)} |
| 290 | actionsSource={actionsSource} |
| 291 | style={style} |
| 292 | /> |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) { |
| 297 | const gt = useGT(); |
| 298 | const gallery = composerImageGallery(props.attachments); |
| 299 | const size = props.imageSize ?? 72; |
| 300 | const radius = props.imageBorderRadius ?? 16; |
| 301 | const removeButtonPlacement = props.removeButtonPlacement ?? "overlay"; |
| 302 | const removeButtonGutter = removeButtonPlacement === "gutter" ? 10 : 0; |
| 303 | |
| 304 | if (props.attachments.length === 0) { |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | return ( |
| 309 | <FilePreviewGallery value={gallery}> |
| 310 | <ScrollView |
| 311 | horizontal |
| 312 | showsHorizontalScrollIndicator={false} |
| 313 | keyboardShouldPersistTaps="always" |
| 314 | className="grow-0" |
| 315 | > |
| 316 | <View className="flex-row gap-2.5"> |
| 317 | {props.attachments.map((attachment) => ( |
| 318 | <View |
| 319 | key={attachment.id} |
| 320 | className="relative" |
| 321 | style={{ |
| 322 | paddingTop: removeButtonGutter, |
| 323 | paddingRight: removeButtonGutter, |
| 324 | }} |
| 325 | > |
| 326 | <ComposerAttachmentThumbnail |
| 327 | environmentId={props.environmentId} |
| 328 | attachment={attachment} |
| 329 | size={size} |
| 330 | borderRadius={radius} |
| 331 | onPressPreview={props.onPressPreview} |
| 332 | onPressVideo={props.onPressVideo} |
| 333 | onPressDocument={props.onPressDocument} |
| 334 | /> |
| 335 | <Pressable |
| 336 | accessibilityRole="button" |
| 337 | accessibilityLabel={gt("Remove {name}", { name: attachment.name })} |
| 338 | className="absolute h-[22px] w-[22px] items-center justify-center rounded-[11px] bg-black/55" |
| 339 | style={{ |
| 340 | top: removeButtonPlacement === "gutter" ? 0 : 4, |
| 341 | right: removeButtonPlacement === "gutter" ? 0 : 4, |
| 342 | }} |
| 343 | hitSlop={6} |
| 344 | onPress={() => props.onRemove(attachment.id)} |
| 345 | > |
| 346 | <SymbolView |
| 347 | name="xmark" |
| 348 | size={9} |
| 349 | tintColor="#ffffff" |
| 350 | type="monochrome" |
| 351 | weight="bold" |
| 352 | /> |
| 353 | </Pressable> |
| 354 | </View> |
| 355 | ))} |
| 356 | </View> |
| 357 | </ScrollView> |
| 358 | </FilePreviewGallery> |
| 359 | ); |
| 360 | } |