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

VideoPreviewModal.ios.tsx

129 lines4.9 KB

1import * as Dialog from "./ui/Dialog";
2import { useIsFocused } from "@react-navigation/native";
3import { requireNativeModule } from "expo";
4import { useEffect, useEffectEvent, useId, useState } from "react";
5import { Keyboard } from "react-native";
6​
7import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
8import { fullscreenVideoClosed, fullscreenVideoOpened } from "../lib/videoAudioSession";
9import { mediaVideoPreviewUri, type VideoPreviewSource } from "../lib/videoPreviewSource";
10import { useAssetUrlState, useRefreshAssetUrl } from "../state/assets";
11import { usePreparedConnection } from "../state/session";
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: VideoPreviewSource;
27 readonly onRequestClose: () => void;
28}) {
29 const { source } = props;
30 const localAttachment = source.type === "local" ? source.attachment : null;
31 const identifier = useId();
32 const onRequestClose = useEffectEvent(props.onRequestClose);
33 const environmentId =
34 source.type === "media" && "environmentId" in source ? source.environmentId : null;
35 const resource = source.type === "media" && "resource" in source ? source.resource : null;
36 const preparedConnection = usePreparedConnection(environmentId);
37 const assetUrl = useAssetUrlState(environmentId, resource);
38 const refreshAssetUrl = useEffectEvent(useRefreshAssetUrl(environmentId, resource));
39 const name = source.type === "media" ? source.name : source.attachment.name;
40 // The first minted URL is kept so a background refresh does not restart playback.
41 const resolvedUrl =
42 source.type === "media"
43 ? mediaVideoPreviewUri(source, assetUrl._tag === "Success" ? assetUrl.url : null)
44 : null;
45 const [playbackUrl, setPlaybackUrl] = useState<string | null>(resolvedUrl);
46 const loadError =
47 resource !== null && playbackUrl === null
48 ? preparedConnection._tag === "None"
49 ? "Reconnect to this environment and open the video again."
50 : assetUrl._tag === "Failure"
51 ? "Could not load this video. Check the connection and try again."
52 : null
53 : null;
54​
55 useEffect(() => Keyboard.dismiss(), []);
56 useEffect(() => {
57 fullscreenVideoOpened();
58 return () => fullscreenVideoClosed();
59 }, []);
60 useEffect(() => {
61 if (playbackUrl === null && resolvedUrl !== null) setPlaybackUrl(resolvedUrl);
62 }, [playbackUrl, resolvedUrl]);
63 useEffect(() => {
64 if (!loadError) return;
65 Dialog.alert("Could not open video", loadError);
66 onRequestClose();
67 }, [loadError]);
68​
69 useEffect(() => {
70 if (localAttachment === null && playbackUrl === null) return;
71 const controller = new AbortController();
72 let ready = false;
73 void (async () => {
74 const file =
75 localAttachment !== null
76 ? await loadLocalAttachmentPreview(localAttachment, controller.signal)
77 : null;
78 if (localAttachment !== null && !file) return;
79 try {
80 if (controller.signal.aborted) return;
81 ready = true;
82 await NativeControls.presentVideo(
83 file?.uri ?? playbackUrl!,
84 name,
85 source.sourceIdentifier ?? "",
86 identifier,
87 );
88 if (!controller.signal.aborted) onRequestClose();
89 } finally {
90 // Native completion follows dismissal, so local playback keeps its file lease.
91 file?.dispose();
92 }
93 })().catch((error: unknown) => {
94 if (controller.signal.aborted) return;
95 // AVKit gives no retry, so re-mint now; the cached URL may simply have expired.
96 if (ready) void refreshAssetUrl();
97 Dialog.alert(
98 "Could not open video",
99 ready
100 ? "This video couldn't be loaded or played. Check the connection, or touch and hold the video to save or share the original."
101 : error instanceof Error
102 ? error.message
103 : "Could not load this video.",
104 );
105 onRequestClose();
106 });
107 return () => {
108 controller.abort();
109 void NativeControls.dismissVideo(identifier).catch(() => undefined);
110 };
111 }, [localAttachment, name, source.sourceIdentifier, playbackUrl, identifier]);
112​
113 return null;
115​
116export function VideoPreviewModal(props: {
117 readonly source: VideoPreviewSource | null;
118 readonly onRequestClose: () => void;
119}) {
120 const isFocused = useIsFocused();
121 const hasSource = props.source !== null;
122 const onRequestClose = useEffectEvent(props.onRequestClose);
123 useEffect(() => {
124 if (!isFocused && hasSource) onRequestClose();
125 }, [isFocused, hasSource]);
126​
127 if (!props.source || !isFocused) return null;
128 return <NativeVideoPreview source={props.source} onRequestClose={props.onRequestClose} />;