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

MediaImagePreview.tsx

69 lines2.6 KB

1import { createContext, useContext, useState } from "react";
2import { Pressable, View } from "react-native";
3import ImageViewing from "react-native-image-viewing";
4import { useSafeAreaInsets } from "react-native-safe-area-context";
5​
6import { useMediaActions } from "../state/mediaActions";
7import { AppText } from "./ui/AppText";
8import { SymbolView } from "./ui/AppSymbol";
9import type { ResolvedFilePreviewSource } from "./FilePreviewModal.types";
10import { MediaActionsMenu } from "./MediaActionsMenu";
11import { MediaSourceCaption } from "./MediaSourceCaption";
12​
13type MediaImagePreviewProps = {
14 readonly source: ResolvedFilePreviewSource;
15 readonly onRequestClose: () => void;
16};
17​
18const ImagePreviewContext = createContext<MediaImagePreviewProps | null>(null);
19​
20function ImagePreviewHeader(header: { readonly imageIndex: number }) {
21 const props = useContext(ImagePreviewContext)!;
22 const source = props.source.gallery?.[header.imageIndex] ?? props.source;
23 const insets = useSafeAreaInsets();
24 const mediaActions = useMediaActions(source.actionsSource, props.onRequestClose);
25 return (
26 <View className="bg-black/70" style={{ paddingTop: insets.top }}>
27 <View className="flex-row items-center gap-2 px-3">
28 <AppText className="flex-1 text-base text-white" numberOfLines={2}>
29 {source.name ?? "Image"}
30 </AppText>
31 <MediaActionsMenu media={mediaActions} inModal />
32 <Pressable
33 accessibilityRole="button"
34 accessibilityLabel="Close image"
35 onPress={props.onRequestClose}
36 className="min-h-11 min-w-11 items-center justify-center"
37 >
38 <SymbolView name="xmark" size={20} tintColor="#ffffff" type="monochrome" />
39 </Pressable>
40 </View>
41 <MediaSourceCaption source={mediaActions.title} />
42 </View>
43 );
45​
46export function MediaImagePreview(props: MediaImagePreviewProps) {
47 const gallery = props.source.gallery ?? [props.source];
48 // Freeze imageIndex: react-native-image-viewing remounts the Modal and replays its fade when it changes.
49 const [initialIndex] = useState(() =>
50 Math.max(
51 0,
52 gallery.findIndex((item) => item.uri === props.source.uri),
53 ),
54 );
55 return (
56 <ImagePreviewContext value={props}>
57 <ImageViewing
58 images={gallery.map((item) => ({ uri: item.uri }))}
59 imageIndex={initialIndex}
60 visible
61 presentationStyle="fullScreen"
62 onRequestClose={props.onRequestClose}
63 swipeToCloseEnabled={false}
64 doubleTapToZoomEnabled
65 HeaderComponent={ImagePreviewHeader}
66 />
67 </ImagePreviewContext>
68 );