"use client"; import React, { useEffect, useRef, useState } from "react"; import { Image, ImageViewer, Dialog } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAngleUp, faClose, faSave } from "@fortawesome/free-solid-svg-icons"; const tabItems = [ { key: "commend", title: "推荐" }, { key: "follow", title: "关注" }, ]; export default function Photos({ media }) { const [seeAllPhotos, setSeeAllPhotos] = useState(false); const [currentPhotos, setCurrentPhotos] = useState([]); const [photos, setPhotos] = useState([]); useEffect(() => { if (media) { let imgArr = media.images.map(item=>({type:"img",url:item.urls?.[0]})) let videoArr = media.videos.map(item=>({type:"video",url:item.cover_urls?.[0],mp4:item.urls?.[0]})) let arr=[...imgArr,...videoArr] let newPhotos = [...arr]; setPhotos(arr); if (arr.length > 9) { newPhotos = newPhotos.slice(0, 9); } setCurrentPhotos(newPhotos); } }, [media]); const showPhotos = (photos, index) => { ImageViewer.Multi.show({ images: photos.map((item) => item.url), defaultIndex: index, }); }; const handleShowVideo = (video) => { Dialog.className = "videoMask"; Dialog.show({ title: "", content: (
Dialog.clear()} >
), bodyStyle: { background: "none", maxHeight: "none", height: "100%", }, }); }; const handleSeeAllPhotos = (e) => { e.stopPropagation(); e.preventDefault(); console.log(e); setSeeAllPhotos((old) => { if (old) { const newPhotos = [...photos]; setCurrentPhotos(newPhotos.slice(0, 9)); return false; } else { setCurrentPhotos(photos); return true; } }); }; return ( <>
1?"grid-cols-3 gap-1.5":"grid-cols-1"}`}> {currentPhotos.map((item, index) => { return (
{ if (item.type == "video") { handleShowVideo(item); } else { showPhotos(photos, index); } }} >
} width={currentPhotos.length>1 ? "24vw":"100%"} height={currentPhotos.length>1 ? "24vw":"100%"} className={`rounded max-w-full`} fit="cover" src={item.url} /> {item.type == "video" && (
)} {/* {index==8 && currentPhotos.length>9 && (
+{currentPhotos.length}
)} */} {index == currentPhotos.length - 1 && photos.length > 9 && !seeAllPhotos && (
+{currentPhotos.length}
)}
); })} {seeAllPhotos && (
)} ); }