import { MenuDivider, MenuItem } from '@szhsin/react-menu'; import { getBlurHashAverageColor } from 'fast-blurhash'; import { useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'preact/hooks'; import { oklab2rgb, rgb2oklab } from '../utils/color-utils'; import showToast from '../utils/show-toast'; import states from '../utils/states'; import useHotkeys from '../utils/useHotkeys'; import Icon from './icon'; import Link from './link'; import Media from './media'; import Menu2 from './menu2'; import MenuLink from './menu-link'; const { PHANPY_IMG_ALT_API_URL: IMG_ALT_API_URL } = import.meta.env; function MediaModal({ mediaAttachments, statusID, instance, lang, index = 0, onClose = () => {}, }) { const [uiState, setUIState] = useState('default'); const carouselRef = useRef(null); const [currentIndex, setCurrentIndex] = useState(index); const carouselFocusItem = useRef(null); useLayoutEffect(() => { carouselFocusItem.current?.scrollIntoView(); // history.pushState({ mediaModal: true }, ''); // const handlePopState = (e) => { // if (e.state?.mediaModal) { // onClose(); // } // }; // window.addEventListener('popstate', handlePopState); // return () => { // window.removeEventListener('popstate', handlePopState); // }; }, []); const prevStatusID = useRef(statusID); useEffect(() => { const scrollLeft = index * carouselRef.current.clientWidth; const differentStatusID = prevStatusID.current !== statusID; if (differentStatusID) prevStatusID.current = statusID; carouselRef.current.scrollTo({ left: scrollLeft, behavior: differentStatusID ? 'auto' : 'smooth', }); carouselRef.current.focus(); }, [index, statusID]); const [showControls, setShowControls] = useState(true); useEffect(() => { let handleSwipe = () => { onClose(); }; if (carouselRef.current) { carouselRef.current.addEventListener('swiped-down', handleSwipe); } return () => { if (carouselRef.current) { carouselRef.current.removeEventListener('swiped-down', handleSwipe); } }; }, []); useHotkeys( 'esc', onClose, { ignoreEventWhen: (e) => { const hasModal = !!document.querySelector('#modal-container > *'); return hasModal; }, }, [onClose], ); useEffect(() => { let handleScroll = () => { const { clientWidth, scrollLeft } = carouselRef.current; const index = Math.round(scrollLeft / clientWidth); setCurrentIndex(index); }; if (carouselRef.current) { carouselRef.current.addEventListener('scroll', handleScroll, { passive: true, }); } return () => { if (carouselRef.current) { carouselRef.current.removeEventListener('scroll', handleScroll); } }; }, []); useEffect(() => { let timer = setTimeout(() => { carouselRef.current?.focus?.(); }, 100); return () => clearTimeout(timer); }, []); const mediaAccentColors = useMemo(() => { return mediaAttachments?.map((media) => { const { blurhash } = media; if (blurhash) { const averageColor = getBlurHashAverageColor(blurhash); const labAverageColor = rgb2oklab(averageColor); return oklab2rgb([0.6, labAverageColor[1], labAverageColor[2]]); } return null; }); }, [mediaAttachments]); const mediaAccentGradient = useMemo(() => { const gap = 5; const range = 100 / mediaAccentColors.length; return ( mediaAccentColors ?.map((color, i) => { const start = i * range + gap; const end = (i + 1) * range - gap; if (color) { return ` rgba(${color?.join(',')}, 0.4) ${start}%, rgba(${color?.join(',')}, 0.4) ${end}% `; } return ` transparent ${start}%, transparent ${end}% `; }) ?.join(', ') || 'transparent' ); }, [mediaAccentColors]); let toastRef = useRef(null); useEffect(() => { return () => { toastRef.current?.hideToast?.(); }; }, []); return (
{mediaAttachments?.length > 1 && ( )}
); } export default MediaModal;