@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 144 lines 5.4 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import { T, useGT } from "gt-react-native";
3 import { m } from "../i18n/messages";
4
5 import { Button } from "./ui/Button";
6 import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
7 import { AsyncResult } from "effect/reactivity";
8 import * as Dialog from "./ui/Dialog";
9 import type { ComposerContextRecord, EnvironmentId } from "@t3tools/contracts";
10 import { Pressable, View } from "react-native";
11 import { Image } from "expo-image";
12 import { useId, useState } from "react";
13 import type { DraftComposerAttachment } from "../lib/composerImages";
14 import { composerAttachmentInlineUri, isFileBackedComposerAttachment } from "../lib/composerImages";
15 import {
16 loadLocalAttachmentPreview,
17 useLocalAttachmentPreview,
18 } from "../lib/localAttachmentPreview";
19 import { downloadAndShareAttachment } from "../lib/attachmentDownload";
20 import { assetImageSource } from "../lib/assetImageSource";
21 import { useAssetUrlState, useRefreshAssetUrl } from "../state/assets";
22 import { AppText as Text } from "./ui/AppText";
23 import { FilePreviewModal } from "./ui/FilePreviewModal";
24 import { PresentationSource } from "./ui/NativePresentation";
25
26 export function ComposerContextAttachment(props: {
27 record: Extract<ComposerContextRecord, { attachmentId: string }>;
28 environmentId?: EnvironmentId;
29 attachment?: DraftComposerAttachment;
30 }) {
31 const gt = useGT();
32 const { record, attachment } = props;
33 const shareSourceIdentifier = useId();
34 const resource = {
35 _tag: "attachment" as const,
36 attachmentId: record.attachmentId,
37 fileName: record.name,
38 };
39 const local = attachment?.fileUri
40 ? (attachment as DraftComposerAttachment & { fileUri: string })
41 : undefined;
42 const inlineUri = composerAttachmentInlineUri(attachment);
43 const remoteEnvironmentId = local || inlineUri ? null : (props.environmentId ?? null);
44 const asset = useAssetUrlState(remoteEnvironmentId, resource);
45 const refresh = useRefreshAssetUrl(remoteEnvironmentId, resource);
46 const localPreview = useLocalAttachmentPreview(local ?? null);
47 const error = localPreview.error ? gt("The local file is unavailable. Attach it again.") : null;
48 const [sharing, setSharing] = useState(false);
49 const [previewOpen, setPreviewOpen] = useState(false);
50 const uri = local
51 ? localPreview.uri
52 : (inlineUri ?? (asset._tag === "Success" ? asset.url : null));
53 const share = async () => {
54 if (sharing) return;
55 setSharing(true);
56 const controller = new AbortController();
57 const result = await settlePromise(async () => {
58 if (local) {
59 const preview = await loadLocalAttachmentPreview(local, controller.signal);
60 if (!preview) return;
61 const shared = await settlePromise(() =>
62 preview.share(controller.signal, shareSourceIdentifier),
63 );
64 preview.dispose();
65 if (AsyncResult.isFailure(shared)) throw squashAtomCommandFailure(shared);
66 return;
67 }
68 const url = await refresh();
69 if (!url) throw new Error(gt("Reconnect to the environment and try again."));
70 await downloadAndShareAttachment({
71 url,
72 attachment: record,
73 signal: controller.signal,
74 sourceIdentifier: shareSourceIdentifier,
75 });
76 });
77 setSharing(false);
78 if (AsyncResult.isFailure(result)) {
79 const cause = squashAtomCommandFailure(result);
80 Dialog.alert(
81 gt("Could not open attachment"),
82 cause instanceof Error
83 ? (errorDisplayMessage(cause, m) ?? cause.message)
84 : gt("Try again."),
85 );
86 }
87 };
88 return (
89 <View className="gap-3">
90 {record.kind === "image" && uri ? (
91 <Pressable
92 accessibilityRole="button"
93 accessibilityLabel={gt("Preview {name}", { name: record.name })}
94 onPress={() => setPreviewOpen(true)}
95 >
96 <Image
97 source={
98 remoteEnvironmentId === null
99 ? { uri }
100 : assetImageSource(remoteEnvironmentId, resource, uri)
101 }
102 style={{ width: "100%", height: 280 }}
103 contentFit="contain"
104 accessibilityLabel={record.name}
105 />
106 </Pressable>
107 ) : null}
108 {error || (!local && asset._tag === "Failure") ? (
109 <Text className="text-foreground-muted">
110 {error ?? <T>Attachment unavailable. Reconnect and try again.</T>}
111 </Text>
112 ) : null}
113 <PresentationSource identifier={shareSourceIdentifier}>
114 <Button
115 label={sharing ? gt("Opening attachment…") : gt("Open or share attachment")}
116 tone="secondary"
117 disabled={sharing}
118 onPress={() => void share()}
119 fullWidth
120 loading={sharing}
121 />
122 </PresentationSource>
123 {previewOpen && record.kind === "image" && uri ? (
124 <FilePreviewModal
125 source={{
126 kind: "image",
127 name: record.name,
128 uri,
129 actionsSource: {
130 name: record.name,
131 mimeType: record.mimeType,
132 ...(attachment && isFileBackedComposerAttachment(attachment)
133 ? { attachment }
134 : inlineUri === undefined && props.environmentId
135 ? { environmentId: props.environmentId, resource }
136 : { uri }),
137 },
138 }}
139 onRequestClose={() => setPreviewOpen(false)}
140 />
141 ) : null}
142 </View>
143 );
144 }