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