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

VideoPreviewModal.ios.tsx

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