import { Ionicons } from "@expo/vector-icons"; import React, { useEffect, useRef, useState } from "react"; import { FlatList, Modal, Pressable, StyleSheet, Text, useWindowDimensions, View, ViewToken, } from "react-native"; import { GestureHandlerRootView } from "react-native-gesture-handler"; import { SafeAreaView } from "react-native-safe-area-context"; import { useAuthStore } from "@/store/auth.store"; import { Colors } from "@/theme/colors"; import type { ContentMediaItem } from "../types/media.types"; import { mediaLabel } from "./media-source"; import { ZoomableImage, type ZoomableImageHandle } from "./ZoomableImage"; interface FullScreenMediaViewerProps { visible: boolean; images: ContentMediaItem[]; initialIndex: number; onClose: () => void; } export function FullScreenMediaViewer({ visible, images, initialIndex, onClose, }: FullScreenMediaViewerProps) { const listRef = useRef>(null); const imageRefs = useRef>(new Map()); const { width, height } = useWindowDimensions(); const accessToken = useAuthStore((state) => state.accessToken); const [activeIndex, setActiveIndex] = useState(initialIndex); const [zoomed, setZoomed] = useState(false); useEffect(() => { if (!visible) return; const nextIndex = Math.min(Math.max(initialIndex, 0), Math.max(images.length - 1, 0)); setActiveIndex(nextIndex); setZoomed(false); requestAnimationFrame(() => { listRef.current?.scrollToIndex({ index: nextIndex, animated: false }); }); }, [images.length, initialIndex, visible]); const onViewableItemsChanged = useRef( ({ viewableItems }: { viewableItems: ViewToken[] }) => { const index = viewableItems.find((item) => item.isViewable)?.index; if (typeof index === "number") { setActiveIndex(index); setZoomed(false); } }, ).current; if (!images.length) return null; const active = images[activeIndex]; const handleZoomIn = () => { if (active) { imageRefs.current.get(active.id)?.zoomIn(); } }; const handleZoomOut = () => { if (active) { imageRefs.current.get(active.id)?.zoomOut(); } }; const handleResetZoom = () => { if (active) { imageRefs.current.get(active.id)?.resetZoom(); } }; return ( {activeIndex + 1} / {images.length} [styles.controlButton, pressed && styles.pressed]} > [styles.controlButton, pressed && styles.pressed]} > [styles.controlButton, pressed && styles.pressed]} > [styles.closeButton, pressed && styles.pressed]} > item.id} initialScrollIndex={Math.min(initialIndex, Math.max(images.length - 1, 0))} getItemLayout={(_data, index) => ({ length: width, offset: width * index, index })} onScrollToIndexFailed={(info) => { requestAnimationFrame(() => { listRef.current?.scrollToOffset({ offset: info.index * width, animated: false }); }); }} viewabilityConfig={{ itemVisiblePercentThreshold: 60 }} onViewableItemsChanged={onViewableItemsChanged} renderItem={({ item, index }) => ( { if (handle) { imageRefs.current.set(item.id, handle); } else { imageRefs.current.delete(item.id); } }} item={item} accessToken={accessToken} width={width} height={Math.max(height - 150, 320)} active={index === activeIndex} onZoomStateChange={index === activeIndex ? setZoomed : undefined} /> )} /> {mediaLabel(active, activeIndex)} Pinch, double-tap, or use +/- to zoom ยท Drag to pan ); } const styles = StyleSheet.create({ modalRoot: { flex: 1, backgroundColor: Colors.black }, root: { flex: 1, backgroundColor: Colors.black }, toolbar: { height: 56, marginTop: 16, paddingHorizontal: 16, flexDirection: "row", alignItems: "center", justifyContent: "space-between", }, counter: { color: Colors.white, fontSize: 15, fontWeight: "700" }, controlsGroup: { flexDirection: "row", alignItems: "center", backgroundColor: "rgba(255,255,255,0.12)", borderRadius: 20, paddingHorizontal: 4, gap: 4, }, controlButton: { width: 36, height: 36, borderRadius: 18, alignItems: "center", justifyContent: "center", }, closeButton: { width: 44, height: 44, borderRadius: 22, alignItems: "center", justifyContent: "center", backgroundColor: "rgba(255,255,255,0.12)", }, pressed: { opacity: 0.65 }, footer: { minHeight: 64, paddingHorizontal: 20, paddingTop: 6, paddingBottom: 8, }, caption: { color: Colors.white, fontSize: 15, lineHeight: 20, textAlign: "center" }, hint: { marginTop: 4, color: "#AFAFAF", fontSize: 12, textAlign: "center" }, });