1- import { createContext , useContext , useState , useMemo } from "react" ;
1+ import { createContext , useContext , useState } from "react" ;
22import { Pressable , View } from "react-native" ;
33import ImageViewing from "react-native-image-viewing" ;
44import { useSafeAreaInsets } from "react-native-safe-area-context" ;
@@ -17,15 +17,16 @@ type MediaImagePreviewProps = {
1717
1818const ImagePreviewContext = createContext < MediaImagePreviewProps | null > ( null ) ;
1919
20- function ImagePreviewHeader ( ) {
20+ function ImagePreviewHeader ( header : { readonly imageIndex : number } ) {
2121 const props = useContext ( ImagePreviewContext ) ! ;
22+ const source = props . source . gallery ?. [ header . imageIndex ] ?? props . source ;
2223 const insets = useSafeAreaInsets ( ) ;
23- const mediaActions = useMediaActions ( props . source . actionsSource , props . onRequestClose ) ;
24+ const mediaActions = useMediaActions ( source . actionsSource , props . onRequestClose ) ;
2425 return (
2526 < View className = "bg-black/70" style = { { paddingTop : insets . top } } >
2627 < View className = "flex-row items-center gap-2 px-3" >
2728 < AppText className = "flex-1 text-base text-white" numberOfLines = { 2 } >
28- { props . source . name ?? "Image" }
29+ { source . name ?? "Image" }
2930 </ AppText >
3031 < MediaActionsMenu media = { mediaActions } inModal />
3132 < Pressable
@@ -44,20 +45,20 @@ function ImagePreviewHeader() {
4445
4546export function MediaImagePreview ( props : MediaImagePreviewProps ) {
4647 const gallery = props . source . gallery ?? [ props . source ] ;
47- const [ index , setIndex ] = useState ( ( ) =>
48+ // react-native-image-viewing keys its whole Modal by imageIndex, so changing it remounts the
49+ // viewer and replays the Android fade over the screen behind. Keep the opening index; the
50+ // header follows paging through the index the library passes it.
51+ const [ initialIndex ] = useState ( ( ) =>
4852 Math . max (
4953 0 ,
5054 gallery . findIndex ( ( item ) => item . uri === props . source . uri ) ,
5155 ) ,
5256 ) ;
53- const selected = gallery [ index ] ?? props . source ;
54- const context = useMemo ( ( ) => ( { ...props , source : selected } ) , [ props , selected ] ) ;
5557 return (
56- < ImagePreviewContext value = { context } >
58+ < ImagePreviewContext value = { props } >
5759 < ImageViewing
5860 images = { gallery . map ( ( item ) => ( { uri : item . uri } ) ) }
59- imageIndex = { index }
60- onImageIndexChange = { setIndex }
61+ imageIndex = { initialIndex }
6162 visible
6263 presentationStyle = "fullScreen"
6364 onRequestClose = { props . onRequestClose }
0 commit comments