import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import CloseIcon from "../icons/CloseIcon"; /** * Полноэкранный просмотр фото сообщения: стрелки/точки на десктопе, свайп на * телефоне, Esc и клик по фону — закрыть. */ export default function ChatPhotoViewer({ photos, startIndex = 0, onClose }) { const { t } = useTranslation(); const [index, setIndex] = useState(startIndex); const touchStartRef = useRef(null); const count = photos.length; const go = (step) => setIndex((i) => (i + step + count) % count); useEffect(() => { const onKey = (e) => { if (e.key === "Escape") onClose(); else if (e.key === "ArrowRight" && count > 1) setIndex((i) => (i + 1) % count); else if (e.key === "ArrowLeft" && count > 1) setIndex((i) => (i - 1 + count) % count); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [count, onClose]); if (!count) return null; const photo = photos[Math.min(index, count - 1)]; return (
e.target === e.currentTarget && onClose()} onTouchStart={(e) => { touchStartRef.current = e.touches[0].clientX; }} onTouchEnd={(e) => { const start = touchStartRef.current; touchStartRef.current = null; if (start == null || count < 2) return; const dx = e.changedTouches[0].clientX - start; if (Math.abs(dx) > 40) go(dx < 0 ? 1 : -1); }} > {count > 1 && ( <>
{index + 1} / {count}
)}
); }