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