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

VideoThumbnailImage.tsx

45 lines1.6 KB

1import { Image } from "expo-image";
2import { useIsFocused } from "@react-navigation/native";
3import type { VideoThumbnail } from "expo-video";
4import { useEffect, useState } from "react";
5import { StyleSheet } from "react-native";
6​
7import type { DraftComposerFileAttachment } from "../lib/composerImages";
8import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
9import { cachedVideoThumbnail, loadVideoThumbnail } from "../lib/videoThumbnails";
10​
11export function VideoThumbnailImage(props: {
12 readonly cacheKey: string;
13 readonly source: string | DraftComposerFileAttachment | null;
14}) {
15 const { cacheKey, source } = props;
16 const isFocused = useIsFocused();
17 const [loaded, setLoaded] = useState<{ key: string; thumbnail: VideoThumbnail } | null>(null);
18 const thumbnail = loaded?.key === cacheKey ? loaded.thumbnail : cachedVideoThumbnail(cacheKey);
19​
20 useEffect(() => {
21 if (!source || !isFocused) return;
22 const controller = new AbortController();
23 void loadVideoThumbnail(
24 cacheKey,
25 async (signal) =>
26 typeof source === "string"
27 ? { uri: source, dispose: () => undefined }
28 : loadLocalAttachmentPreview(source, signal),
29 controller.signal,
30 ).then((thumbnail) => {
31 if (thumbnail && !controller.signal.aborted) setLoaded({ key: cacheKey, thumbnail });
32 });
33 return () => controller.abort();
34 }, [cacheKey, source, isFocused]);
35​
36 return thumbnail ? (
37 <Image
38 source={thumbnail}
39 style={StyleSheet.absoluteFill}
40 contentFit="cover"
41 recyclingKey={cacheKey}
42 accessible={false}
43 />
44 ) : null;