tiefen_space_h5/components/Photos/index.js

167 lines
5.2 KiB
JavaScript
Raw Normal View History

2024-06-25 20:18:37 +08:00
"use client";
2024-06-29 12:07:37 +08:00
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";
2024-07-06 11:05:19 +08:00
2024-06-25 20:18:37 +08:00
const tabItems = [
{ key: "commend", title: "推荐" },
{ key: "follow", title: "关注" },
];
2024-07-03 19:59:39 +08:00
export default function Photos({ media }) {
2024-06-29 12:07:37 +08:00
const [seeAllPhotos, setSeeAllPhotos] = useState(false);
2024-07-03 19:59:39 +08:00
const [currentPhotos, setCurrentPhotos] = useState([]);
const [photos, setPhotos] = useState([]);
2024-06-29 12:07:37 +08:00
useEffect(() => {
2024-07-03 19:59:39 +08:00
if (media) {
2024-07-06 11:05:19 +08:00
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]}))
2024-07-03 19:59:39 +08:00
let arr=[...imgArr,...videoArr]
let newPhotos = [...arr];
setPhotos(arr);
if (arr.length > 9) {
newPhotos = newPhotos.slice(0, 9);
}
setCurrentPhotos(newPhotos);
2024-06-29 12:07:37 +08:00
}
2024-07-03 19:59:39 +08:00
}, [media]);
2024-06-25 20:18:37 +08:00
const showPhotos = (photos, index) => {
ImageViewer.Multi.show({
images: photos.map((item) => item.url),
2024-06-29 12:07:37 +08:00
defaultIndex: index,
});
};
const handleShowVideo = (video) => {
Dialog.className = "videoMask";
Dialog.show({
title: "",
content: (
<div className="flex flex-col gap-2">
<div className="flex w-full justify-end">
<div
className="flex w-12 h-12 justify-center items-center bg-[#33333348] rounded-full"
key="closeBtn"
2024-07-03 19:59:39 +08:00
onClick={() => Dialog.clear()}
2024-06-29 12:07:37 +08:00
>
2024-07-06 11:05:19 +08:00
<FontAwesomeIcon icon={faClose} size="2xl" />
2024-06-29 12:07:37 +08:00
</div>
</div>
<div className="my-4">
<video
width="100%"
height="100%"
controls
2024-07-06 11:05:19 +08:00
className="w-screen h-[70vh] rounded-lg object-contain"
2024-07-03 19:59:39 +08:00
poster={video.url}
2024-06-29 12:07:37 +08:00
>
<source
2024-07-03 19:59:39 +08:00
src={video.mp4}
2024-06-29 12:07:37 +08:00
type="video/mp4"
/>
您的浏览器不支持 Video 标签
</video>
</div>
<div
className="flex w-12 h-12 justify-center items-center bg-[#33333348] rounded-full"
key="closeBtn"
onClick={handleSeeAllPhotos}
>
2024-07-06 11:05:19 +08:00
<FontAwesomeIcon icon={faSave} size="2xl"/>
2024-06-29 12:07:37 +08:00
</div>
</div>
),
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;
}
});
2024-06-25 20:18:37 +08:00
};
return (
<>
2024-07-08 20:07:36 +08:00
<div className={`grid ${currentPhotos.length > 1?"grid-cols-3 gap-1.5":"grid-cols-1"}`}>
2024-06-29 12:07:37 +08:00
{currentPhotos.map((item, index) => {
2024-06-25 20:18:37 +08:00
return (
<div
2024-07-08 20:07:36 +08:00
className="relative w-mfull"
2024-06-25 20:18:37 +08:00
key={index}
onClick={() => {
2024-06-29 12:07:37 +08:00
if (item.type == "video") {
2024-07-03 19:59:39 +08:00
handleShowVideo(item);
2024-06-29 12:07:37 +08:00
} else {
showPhotos(photos, index);
}
2024-06-25 20:18:37 +08:00
}}
>
<Image
2024-07-08 20:07:36 +08:00
// lazy={true}
2024-06-29 12:07:37 +08:00
placeholder={
<div className="w-full h-full bg-[#1d1d1d] rounded"></div>
}
2024-07-08 20:07:36 +08:00
width={"100%"}
height={"100%"}
className={`rounded max-w-full`}
2024-06-25 20:18:37 +08:00
fit="cover"
src={item.url}
2024-06-25 20:18:37 +08:00
/>
2024-06-29 12:07:37 +08:00
{item.type == "video" && (
2024-07-06 11:05:19 +08:00
<div className="absolute top-0 w-full h-full rounded flex justify-center items-center bg-[#33333348]">
2024-06-29 12:07:37 +08:00
<Image
className=""
2024-07-06 11:05:19 +08:00
width={98}
height={98}
2024-06-29 12:07:37 +08:00
src="/icons/play.png"
placeholder=""
/>
</div>
)}
{/* {index==8 && currentPhotos.length>9 && (
<div className="absolute top-0 w-full h-full flex justify-center items-center bg-[#33333348]">
<span className="text-2xl">+{currentPhotos.length}</span>
</div>
)} */}
{index == currentPhotos.length - 1 &&
photos.length > 9 &&
!seeAllPhotos && (
<div
className="absolute top-0 w-full h-full flex justify-center items-center bg-[#33333348]"
onClick={handleSeeAllPhotos}
>
<span className="text-2xl">+{currentPhotos.length}</span>
</div>
)}
2024-06-25 20:18:37 +08:00
</div>
);
})}
2024-06-29 12:07:37 +08:00
{seeAllPhotos && (
<div
className="flex justify-center items-center bg-[#33333348] rounded"
key="closeBtn"
onClick={handleSeeAllPhotos}
>
<FontAwesomeIcon icon={faAngleUp} size="2xl" className="h-14" />
</div>
)}
2024-06-25 20:18:37 +08:00
</div>
</>
);
}