Open History to see the last commit
1import { useNavigation } from "@react-navigation/native";
2import type { MediaActionId } from "@t3tools/client-runtime/media-actions";
3import type { MediaReference } from "@t3tools/client-runtime/media-reference";
4import type { AssetResource, EnvironmentId, ThreadId } from "@t3tools/contracts";
5import { normalizeNativeMarkdownUrl } from "@t3tools/mobile-markdown-text/links";
6import { useEffect, useRef, useState } from "react";
7import { Alert } from "react-native";
8
9import { useRefreshAssetUrl } from "../state/assets";
10import { downloadAndShareAttachment, shareLocalAttachment } from "./attachmentDownload";
11import type { FileBackedComposerAttachment } from "./composerImages";
12import { copyTextWithHaptic } from "./copyTextWithHaptic";
13import { loadLocalAttachmentPreview } from "./localAttachmentPreview";
14
15/** Authored source metadata is kept separate from temporary preview/download URLs. */
16export type MediaActionsSource = {
17 readonly reference?: MediaReference;
18 readonly name: string;
19 readonly mimeType: string;
20 /** Anchors the iOS share sheet to the view that opened the menu. */
21 readonly sourceIdentifier?: string;
22} & (
23 | { readonly uri: string }
24 | { readonly attachment: FileBackedComposerAttachment }
25 | {
26 readonly environmentId: EnvironmentId;
27 readonly threadId?: ThreadId;
28 readonly resource: AssetResource;
29 }
30);
31
32export function useMediaActions(source: MediaActionsSource | undefined, onOpenFile?: () => void) {
33 const navigation = useNavigation();
34 const refresh = useRefreshAssetUrl(
35 source && "environmentId" in source ? source.environmentId : null,
36 source && "resource" in source ? source.resource : null,
37 );
38 const controller = useRef<AbortController | null>(null);
39 const [sharing, setSharing] = useState(false);
40 useEffect(() => () => controller.current?.abort(), []);
41
42 const share = () => {
43 if (!source || controller.current) return;
44 const request = new AbortController();
45 controller.current = request;
46 setSharing(true);
47 void (async () => {
48 if ("attachment" in source) {
49 const preview = await loadLocalAttachmentPreview(source.attachment, request.signal);
50 if (!preview) return;
51 try {
52 await preview.share(request.signal, source.sourceIdentifier);
53 } finally {
54 preview.dispose();
55 }
56 return;
57 }
58 const uri = "uri" in source ? normalizeNativeMarkdownUrl(source.uri) : await refresh();
59 if (request.signal.aborted) return;
60 if (uri === null) throw new Error("The file could not be loaded. Reconnect and try again.");
61 const input = {
62 attachment: { name: source.name, mimeType: source.mimeType },
63 signal: request.signal,
64 sourceIdentifier: source.sourceIdentifier,
65 };
66 if (/^(file|content):/i.test(uri)) await shareLocalAttachment({ ...input, uri });
67 else await downloadAndShareAttachment({ ...input, url: uri });
68 })()
69 .catch((error: unknown) => {
70 if (!request.signal.aborted) {
71 Alert.alert(
72 "Could not share file",
73 error instanceof Error ? error.message : "Try again.",
74 );
75 }
76 })
77 .finally(() => {
78 if (controller.current === request) {
79 controller.current = null;
80 if (!request.signal.aborted) setSharing(false);
81 }
82 });
83 };
84
85 const reference = source?.reference;
86 const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
87 const threadId = source && "threadId" in source ? source.threadId : undefined;
88 const actions: { id: MediaActionId; title: string; run: () => void; disabled?: boolean }[] =
89 source
90 ? [
91 ...(reference?.kind === "file"
92 ? [
93 {
94 id: "copy-full-path" as const,
95 title: "Copy full path",
96 run: () => copyTextWithHaptic(reference.path),
97 },
98 ]
99 : []),
100 ...(relativePath
101 ? [
102 {
103 id: "copy-relative-path" as const,
104 title: "Copy relative path",
105 run: () => copyTextWithHaptic(relativePath),
106 },
107 ]
108 : []),
109 ...(reference?.kind === "url"
110 ? [
111 {
112 id: "copy-url" as const,
113 title: "Copy URL",
114 run: () => copyTextWithHaptic(reference.url),
115 },
116 ]
117 : []),
118 ...(relativePath && "environmentId" in source && threadId !== undefined
119 ? [
120 {
121 id: "open-file" as const,
122 title: "Open in file viewer",
123 run: () => {
124 onOpenFile?.();
125 navigation.navigate("ThreadFile", {
126 environmentId: String(source.environmentId),
127 threadId: String(threadId),
128 path: relativePath.split("/"),
129 });
130 },
131 },
132 ]
133 : []),
134 {
135 id: "save" as const,
136 title: sharing ? "Opening share sheet…" : "Save or share",
137 run: share,
138 disabled: sharing,
139 },
140 ]
141 : [];
142 return {
143 title: reference?.kind === "file" ? reference.path : reference?.url,
144 actions,
145 sharing,
146 share,
147 };
148}