added carousel image on post on mainFeed

This commit is contained in:
SlimusMinus
2026-10-09 01:21:14 +03:00
parent 55c71e664d
commit b924483231
12 changed files with 29 additions and 205 deletions

View File

@@ -1,142 +0,0 @@
import { useState, useCallback, useEffect, useRef, useMemo } from "react";
import { useTranslation } from 'react-i18next';
import CloseIcon from "./icons/CloseIcon";
import CheckIcon from "./icons/CheckIcon";
import Toast from "./Toast";
import { getImageUrl } from "../config/authConstants";
import { CATEGORY_PET } from "../config/feedConstants";
import { getFoundAt, formatFoundDate } from "../utils/foundAt";
import { useContactSeller } from "../hooks/useContactSeller";
import { useAuthContext } from "../context/AuthContext";
export default function ImageViewer({ images, initialIndex = 0, onClose, postData }) {
const { t, i18n } = useTranslation();
const [index, setIndex] = useState(initialIndex);
const touchStart = useRef(null);
const swiped = useRef(false);
const { isAuthenticated } = useAuthContext();
const { contactSeller, error: contactError, clearError } = useContactSeller(postData);
const urls = useMemo(() => images.map(u => getImageUrl(u)), [images]);
const next = useCallback(() => {
setIndex(i => (i + 1) % urls.length);
}, [urls.length]);
const prev = useCallback(() => {
setIndex(i => (i - 1 + urls.length) % urls.length);
}, [urls.length]);
const handleImgClick = useCallback(() => {
next();
}, [next]);
useEffect(() => {
const handleKey = (e) => {
if (e.key === "Escape") onClose();
if (e.key === "ArrowRight") next();
if (e.key === "ArrowLeft") prev();
};
window.addEventListener("keydown", handleKey);
return () => window.removeEventListener("keydown", handleKey);
}, [onClose, next, prev]);
useEffect(() => {
setIndex(initialIndex);
}, [initialIndex]);
const handleTouchStart = useCallback((e) => {
touchStart.current = { x: e.touches[0].clientX, y: e.touches[0].clientY };
}, []);
const handleTouchEnd = useCallback((e) => {
if (!touchStart.current) return;
const dx = e.changedTouches[0].clientX - touchStart.current.x;
const dy = e.changedTouches[0].clientY - touchStart.current.y;
const isHorizontal = Math.abs(dx) > Math.abs(dy);
if (isHorizontal && Math.abs(dx) > 50) {
swiped.current = true;
if (dx > 0) prev();
else next();
setTimeout(() => { swiped.current = false; }, 400);
}
touchStart.current = null;
}, [prev, next]);
const handleContact = useCallback(() => {
// Закрываем вьюер перед переходом в чат
contactSeller(onClose);
}, [contactSeller, onClose]);
const isClosed = postData?.status === 'CLOSED';
const showContact = isAuthenticated && postData && !postData.isOwner && !isClosed;
// Статус «найдено» держим на каждом слайде: перелистывая карусель, человек
// не должен терять из виду, что вещь/питомец уже вернулся к владельцу.
const foundText = postData?.category === CATEGORY_PET
? t('postCard.petFoundStatus')
: t('postCard.foundStatus');
const foundDate = isClosed ? formatFoundDate(getFoundAt(postData), i18n.language) : null;
const foundTitle = foundDate
? t('postCard.foundStatusWithDate', { status: foundText, date: foundDate })
: foundText;
if (!urls.length) return null;
return (
<div
className="f-viewer-bg"
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
onClick={e => e.target === e.currentTarget && !swiped.current && onClose()}
>
<button type="button" className="f-viewer-close" onClick={onClose} aria-label={t('common.close')}>
<CloseIcon />
</button>
<div className="f-viewer">
{isClosed && (
<span className="f-viewer-found-badge" title={String(foundTitle)}>
<CheckIcon size={14}/>
<span>{foundText}</span>
</span>
)}
<div className="f-viewer-img-wrap" onClick={handleImgClick}>
<img
src={urls[index]}
alt=""
className={`f-viewer-img${isClosed ? ' f-viewer-img-closed' : ''}`}
/>
</div>
{showContact && (
<button type="button" className="f-viewer-contact" onClick={handleContact}>
{t('postCard.write')}
</button>
)}
{urls.length > 1 && (
<div className="f-viewer-dots">
{urls.map((_, i) => (
<button
type="button"
key={i}
className={`f-viewer-dot${i === index ? " active" : ""}`}
onClick={() => setIndex(i)}
aria-label={t('viewer.goToImage', { number: i + 1 })}
/>
))}
</div>
)}
</div>
{contactError && (
<Toast
message={contactError}
type="error"
onDismiss={clearError}
/>
)}
</div>
);
}

View File

@@ -6,7 +6,7 @@ import { useAuthContext } from "../context/AuthContext";
import PostOnFeed from "../pages/PostOnFeed";
import CloseIcon from "./icons/CloseIcon";
export default function PostDetailModal({ postId, onClose, onImageClick, onLocationClick, onAuthRequired }) {
export default function PostDetailModal({ postId, onClose, onLocationClick, onAuthRequired }) {
const { t } = useTranslation();
const { user } = useAuthContext();
const [post, setPost] = useState(null);
@@ -62,7 +62,6 @@ export default function PostDetailModal({ postId, onClose, onImageClick, onLocat
<PostOnFeed
ad={post}
isOwner={isOwner}
onImageClick={onImageClick}
onLocationClick={onLocationClick}
onAuthRequired={onAuthRequired}
compact

View File

@@ -3,7 +3,6 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import PostOnFeed from "../../pages/PostOnFeed";
import CreateUpdatePost from "../../pages/CreateUpdatePost";
import ImageViewer from "../ImageViewer";
import Toast from "../Toast";
import {
getAdminPosts,
@@ -66,7 +65,6 @@ export default function PostsTab({ isAdmin, mode = "all" }) {
const [statusFilter, setStatusFilter] = useState("");
const [editing, setEditing] = useState(null);
const [viewer, setViewer] = useState(null);
const [busyId, setBusyId] = useState(null);
const [toast, setToast] = useState(null);
@@ -148,10 +146,6 @@ export default function PostsTab({ isAdmin, mode = "all" }) {
showToast(t("adminPanel.postsUpdated"));
}, [load, page, showToast, t]);
const handleImageClick = useCallback((images, index, ad) => {
if (images?.length) setViewer({ images, index, ad });
}, []);
const contentStyle = useMemo(() => ({ opacity: busyId === null ? 1 : 0.75 }), [busyId]);
if (!isAdmin) return null;
@@ -234,7 +228,6 @@ export default function PostsTab({ isAdmin, mode = "all" }) {
onDelete={handleDelete}
onMarkFound={(id) => setStatus(id, "CLOSED")}
onReopen={(id) => setStatus(id, "ACTIVE")}
onImageClick={handleImageClick}
/>
))}
</div>
@@ -261,15 +254,6 @@ export default function PostsTab({ isAdmin, mode = "all" }) {
/>
)}
{viewer && (
<ImageViewer
images={viewer.images}
initialIndex={viewer.index}
postData={viewer.ad}
onClose={() => setViewer(null)}
/>
)}
{toast && <Toast message={toast.message} type={toast.type} onDismiss={() => setToast(null)} />}
</>
);

View File

@@ -17,7 +17,6 @@ export default function FeedContent({
onDelete,
onMarkFound,
onReopen,
onImageClick,
onLocationClick,
onAuthRequired,
showAds = false,
@@ -91,7 +90,6 @@ export default function FeedContent({
onMarkFound={activeNav === "myads" ? onMarkFound : undefined}
onReopen={activeNav === "myads" ? onReopen : undefined}
isOwner={activeNav === "myads"}
onImageClick={onImageClick}
onLocationClick={onLocationClick}
onAuthRequired={onAuthRequired}
/>