Open History to see the last commit
1import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2import { T, useGT } from "gt-react-native";
3import { m } from "../i18n/messages";
4
5import { Button } from "./ui/Button";
6import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
7import { AsyncResult } from "effect/reactivity";
8import * as Dialog from "./ui/Dialog";
9import type { ComposerContextRecord, EnvironmentId } from "@t3tools/contracts";
10import { Pressable, View } from "react-native";
11import { Image } from "expo-image";
12import { useId, useState } from "react";
13import type { DraftComposerAttachment } from "../lib/composerImages";
14import { composerAttachmentInlineUri, isFileBackedComposerAttachment } from "../lib/composerImages";
15import {
16 loadLocalAttachmentPreview,
17 useLocalAttachmentPreview,
18} from "../lib/localAttachmentPreview";
19import { downloadAndShareAttachment } from "../lib/attachmentDownload";
20import { assetImageSource } from "../lib/assetImageSource";
21import { useAssetUrlState, useRefreshAssetUrl } from "../state/assets";
22import { AppText as Text } from "./ui/AppText";
23import { FilePreviewModal } from "./ui/FilePreviewModal";
24import { PresentationSource } from "./ui/NativePresentation";
25
26export 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 ? (errorDisplayMessage(cause, m) ?? cause.message) : gt`Try again.`,
83 );
84 }
85 };
86 return (
87 <View className="gap-3">
88 {record.kind === "image" && uri ? (
89 <Pressable
90 accessibilityRole="button"
91 accessibilityLabel={gt`Preview ${{ name: record.name }}`}
92 onPress={() => setPreviewOpen(true)}
93 >
94 <Image
95 source={
96 remoteEnvironmentId === null
97 ? { uri }
98 : assetImageSource(remoteEnvironmentId, resource, uri)
99 }
100 style={{ width: "100%", height: 280 }}
101 contentFit="contain"
102 accessibilityLabel={record.name}
103 />
104 </Pressable>
105 ) : null}
106 {error || (!local && asset._tag === "Failure") ? (
107 <Text className="text-foreground-muted">
108 {error ?? <T>Attachment unavailable. Reconnect and try again.</T>}
109 </Text>
110 ) : null}
111 <PresentationSource identifier={shareSourceIdentifier}>
112 <Button
113 label={sharing ? gt`Opening attachment…` : gt`Open or share attachment`}
114 tone="secondary"
115 disabled={sharing}
116 onPress={() => void share()}
117 fullWidth
118 loading={sharing}
119 />
120 </PresentationSource>
121 {previewOpen && record.kind === "image" && uri ? (
122 <FilePreviewModal
123 source={{
124 kind: "image",
125 name: record.name,
126 uri,
127 actionsSource: {
128 name: record.name,
129 mimeType: record.mimeType,
130 ...(attachment && isFileBackedComposerAttachment(attachment)
131 ? { attachment }
132 : inlineUri === undefined && props.environmentId
133 ? { environmentId: props.environmentId, resource }
134 : { uri }),
135 },
136 }}
137 onRequestClose={() => setPreviewOpen(false)}
138 />
139 ) : null}
140 </View>
141 );
142}