Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

VideoPreviewModal.ios.tsx

121 lines4.2 KB

1import { useIsFocused } from "@react-navigation/native";
2import { videoMimeType } from "@t3tools/shared/video";
3import { requireNativeModule } from "expo";
4import { useEffect, useEffectEvent, useId, useState } from "react";
5import { Alert, Keyboard } from "react-native";
6​
7import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
8import { useAssetUrlState } from "../state/assets";
9import { usePreparedConnection } from "../state/session";
10import type { VideoPreviewSource } from "./VideoPreviewModal";
11​
12export type { VideoPreviewSource } from "./VideoPreviewModal";
13​
14const NativeControls = requireNativeModule<{
15 presentVideo(
16 uri: string,
17 title: string,
18 sourceIdentifier: string,
19 identifier: string,
20 ): Promise<void>;
21 dismissVideo(identifier: string): Promise<void>;
22}>("T3NativeControls");
23​
24function NativeVideoPreview(props: {
25 readonly source: VideoPreviewSource;
26 readonly onRequestClose: () => void;
27}) {
28 const { source } = props;
29 const { attachment } = source;
30 const identifier = useId();
31 const onRequestClose = useEffectEvent(props.onRequestClose);
32 const environmentId = source.type === "remote" ? source.environmentId : null;
33 const preparedConnection = usePreparedConnection(environmentId);
34 const mimeType = videoMimeType(attachment) ?? attachment.mimeType;
35 const assetUrl = useAssetUrlState(
36 environmentId,
37 source.type === "remote"
38 ? { _tag: "attachment", attachmentId: attachment.id, fileName: attachment.name, mimeType }
39 : null,
40 );
41 const [playbackUrl, setPlaybackUrl] = useState<string | null>(() =>
42 assetUrl._tag === "Success" ? assetUrl.url : null,
43 );
44 const loadError =
45 source.type === "remote" && playbackUrl === null
46 ? preparedConnection._tag === "None"
47 ? "Reconnect to this environment and open the video again."
48 : assetUrl._tag === "Failure"
49 ? "Could not load this video. Check the connection and try again."
50 : null
51 : null;
52​
53 useEffect(() => Keyboard.dismiss(), []);
54 useEffect(() => {
55 if (playbackUrl === null && assetUrl._tag === "Success") setPlaybackUrl(assetUrl.url);
56 }, [playbackUrl, assetUrl]);
57 useEffect(() => {
58 if (!loadError) return;
59 Alert.alert("Could not open video", loadError);
60 onRequestClose();
61 }, [loadError]);
62​
63 useEffect(() => {
64 if (source.type === "remote" && playbackUrl === null) return;
65 const controller = new AbortController();
66 let ready = false;
67 void (async () => {
68 const file =
69 source.type === "local"
70 ? await loadLocalAttachmentPreview(source.attachment, controller.signal)
71 : null;
72 if (source.type === "local" && !file) return;
73 try {
74 if (controller.signal.aborted) return;
75 ready = true;
76 await NativeControls.presentVideo(
77 file?.uri ?? playbackUrl!,
78 attachment.name,
79 source.sourceIdentifier ?? "",
80 identifier,
81 );
82 if (!controller.signal.aborted) onRequestClose();
83 } finally {
84 // Native completion follows dismissal, so local playback keeps its file lease.
85 file?.dispose();
86 }
87 })().catch((error: unknown) => {
88 if (controller.signal.aborted) return;
89 Alert.alert(
90 "Could not open video",
91 ready
92 ? "This video couldn't be loaded or played. Check the connection, or touch and hold the attachment to save or share the original."
93 : error instanceof Error
94 ? error.message
95 : "Could not load this video.",
96 );
97 onRequestClose();
98 });
99 return () => {
100 controller.abort();
101 void NativeControls.dismissVideo(identifier).catch(() => undefined);
102 };
103 }, [source, attachment.name, playbackUrl, identifier]);
104​
105 return null;
107​
108export function VideoPreviewModal(props: {
109 readonly source: VideoPreviewSource | null;
110 readonly onRequestClose: () => void;
111}) {
112 const isFocused = useIsFocused();
113 const hasSource = props.source !== null;
114 const onRequestClose = useEffectEvent(props.onRequestClose);
115 useEffect(() => {
116 if (!isFocused && hasSource) onRequestClose();
117 }, [isFocused, hasSource]);
118​
119 if (!props.source || !isFocused) return null;
120 return <NativeVideoPreview source={props.source} onRequestClose={props.onRequestClose} />;