Files
nahodka-front/src/components/chat/ChatPhotoViewer.jsx
SlimusMinus b7b30575a2 Send and view photos in chats
- paperclip button and clipboard paste in the chat input, previews with
  remove, up to 10 photos per message (sent via REST multipart)
- photos in message bubbles with reserved size, fullscreen viewer
  (arrows, swipe, Esc), fresh link when a presigned URL expires
- chat list preview shows "Photo" for photo-only messages
- keep the feed pinned to the bottom when the preview strip appears

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 22:26:44 +03:00

60 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 (
<div
className="chat-viewer"
role="dialog"
aria-modal="true"
onClick={(e) => 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);
}}
>
<button type="button" className="chat-viewer-close" onClick={onClose} aria-label={t("common.close")}>
<CloseIcon />
</button>
<img className="chat-viewer-img" src={photo.url} alt="" />
{count > 1 && (
<>
<button type="button" className="chat-viewer-nav chat-viewer-prev" onClick={() => go(-1)}
aria-label={t("viewer.prevImage")}>‹</button>
<button type="button" className="chat-viewer-nav chat-viewer-next" onClick={() => go(1)}
aria-label={t("viewer.nextImage")}>›</button>
<div className="chat-viewer-counter">{index + 1} / {count}</div>
</>
)}
</div>
);
}