fixed image on post on mainFeed

This commit is contained in:
SlimusMinus
2026-10-09 01:00:21 +03:00
parent 17795e71ad
commit 326d00d005
4 changed files with 174 additions and 8 deletions

View File

@@ -20,9 +20,12 @@ export default function PostOnFeed({ ad, onEdit, onDelete, onMarkFound, onReopen
const [showReward, setShowReward] = useState(false);
const [descExpanded, setDescExpanded] = useState(false);
const [descTruncated, setDescTruncated] = useState(false);
const [imgIndex, setImgIndex] = useState(0);
const dateAnchorRef = useRef(null);
const rewardAnchorRef = useRef(null);
const descRef = useRef(null);
const imgTouchStartRef = useRef(null);
const imgSwipedRef = useRef(false);
const { isAuthenticated } = useAuthContext();
const { contactSeller, error: contactError, clearError } = useContactSeller(ad);
@@ -46,7 +49,45 @@ export default function PostOnFeed({ ad, onEdit, onDelete, onMarkFound, onReopen
contactSeller();
};
const imageUrl = getImageUrl(ad.images?.[0]);
const images = Array.isArray(ad.images) ? ad.images : [];
const hasMultipleImages = images.length > 1;
// Карусель только для карточек ленты (не для компактного вида в детальной
// модалке, где фото показывается целиком и overflow не скрыт).
const carousel = hasMultipleImages && !compact;
// Листание фото прямо в карточке: на телефоне — свайпом, на десктопе —
// стрелками и точками. Полноэкранный просмотр по-прежнему открывается по клику.
const goToImage = (target) => {
setImgIndex(Math.max(0, Math.min(images.length - 1, target)));
};
const handleImgTouchStart = (e) => {
if (!carousel) return;
imgTouchStartRef.current = { x: e.touches[0].clientX, y: e.touches[0].clientY };
};
const handleImgTouchEnd = (e) => {
const start = imgTouchStartRef.current;
imgTouchStartRef.current = null;
if (!start || !carousel) return;
const dx = e.changedTouches[0].clientX - start.x;
const dy = e.changedTouches[0].clientY - start.y;
// Реагируем только на явно горизонтальный жест — вертикальный скролл ленты
// не должен переключать фото.
if (Math.abs(dx) > 40 && Math.abs(dx) > Math.abs(dy)) {
imgSwipedRef.current = true;
setImgIndex(i => Math.max(0, Math.min(images.length - 1, dx < 0 ? i + 1 : i - 1)));
window.setTimeout(() => { imgSwipedRef.current = false; }, 350);
}
};
const handleImgAreaClick = (e) => {
e.stopPropagation();
// После свайпа браузер может дослать click — не открываем просмотр.
if (imgSwipedRef.current) return;
if (onImageClick) onImageClick(images, imgIndex, ad);
};
const address = ad.address || ad.city || ad.district || ad.location;
const createdAt = ad.createdAt
? new Date(ad.createdAt).toLocaleDateString("ru-RU")
@@ -92,11 +133,34 @@ export default function PostOnFeed({ ad, onEdit, onDelete, onMarkFound, onReopen
</div>
</div>
<div className="f-card-img" onClick={e => { e.stopPropagation(); if (onImageClick) onImageClick(ad.images, 0, ad); }}>
{imageUrl
? <img src={imageUrl} alt={ad.title} />
: <span className="f-card-emoji">{ad.emoji || "📋"}</span>
}
<div
className={`f-card-img${carousel ? " f-card-img-multi" : ""}`}
onClick={handleImgAreaClick}
onTouchStart={handleImgTouchStart}
onTouchEnd={handleImgTouchEnd}
>
{images.length > 0 ? (
carousel ? (
<div
className="f-card-img-track"
style={{ transform: `translateX(-${imgIndex * 100}%)` }}
>
{images.map((raw, i) => (
<img
key={i}
src={getImageUrl(raw)}
alt={ad.title}
className="f-card-img-slide"
draggable={false}
/>
))}
</div>
) : (
<img src={getImageUrl(images[0])} alt={ad.title} />
)
) : (
<span className="f-card-emoji">{ad.emoji || "📋"}</span>
)}
{/* Плашка статуса поверх фото: при быстром скролле шапка карточки
проезжает мимо, а фото с плашкой — нет. */}
{isClosed && (
@@ -105,6 +169,37 @@ export default function PostOnFeed({ ad, onEdit, onDelete, onMarkFound, onReopen
<span>{foundText}</span>
</span>
)}
{carousel && (
<>
<button
type="button"
className="f-img-nav f-img-nav-prev"
aria-label={t('viewer.prevImage')}
onClick={e => { e.stopPropagation(); goToImage(imgIndex - 1); }}
>
‹
</button>
<button
type="button"
className="f-img-nav f-img-nav-next"
aria-label={t('viewer.nextImage')}
onClick={e => { e.stopPropagation(); goToImage(imgIndex + 1); }}
>
›
</button>
<div className="f-img-dots">
{images.map((_, i) => (
<button
type="button"
key={i}
className={`f-img-dot${i === imgIndex ? " active" : ""}`}
aria-label={t('viewer.goToImage', { number: i + 1 })}
onClick={e => { e.stopPropagation(); goToImage(i); }}
/>
))}
</div>
</>
)}
</div>
{!compact && (