media page active image slide to replace url, and set download and share links

This commit is contained in:
BFlat committed 2024-03-05 22:19:31 +07:00
1 parent f914765dd6
commit 35bc9b930d
2 files changed
+83 -29

No files matched your search

@@ -64,23 +64,27 @@ export default function MediaPage() {
},
});
const batchToken = mediaFile?.maybe_batch_token;
const [images, setImages] = useState<string[]>([]);
const [images, setImages] = useState<{ url: string; token: string }[]>([]);
const bucketConfig = new BucketConfig();
const timeCreated = moment(mediaFile?.created_at || "").fromNow();
const dateCreated = moment(mediaFile?.created_at || "").format("LLL");
const [buttonLabel, setButtonLabel] = useState("Copy");
const [copyPositiveButtonText, setCopyPositiveButtonText] = useState("Copy");
const [copyNegativeButtonText, setCopyNegativeButtonText] = useState("Copy");
const [activeSlide, setActiveSlide] = useState({ url: "", token: "" });
// Inside MediaPage.tsx
useEffect(() => {
if (batchToken) {
GetMediaBatchImages(batchToken, {}, {})
.then(response => {
if (response.success) {
const imageUrls = response.results.map(result =>
bucketConfig.getGcsUrl(result.public_bucket_path)
);
setImages(imageUrls);
const mediaItems = response.results.map(result => ({
url: bucketConfig.getGcsUrl(result.public_bucket_path),
token: result.token,
}));
setImages(mediaItems);
} else {
console.error("Failed to fetch batch images");
}
@@ -111,6 +115,10 @@ export default function MediaPage() {
}
};
const handleActiveSlideChange = (image: any) => {
setActiveSlide(image);
};
const promptSection = (
<>
{prompt?.maybe_positive_prompt && (
@@ -198,9 +206,16 @@ export default function MediaPage() {
);
case MediaFileType.Image:
let sdMediaImage = ["/images/avatars/default-pfp.png"];
let sdMediaImage = [
{ url: "/images/avatars/default-pfp.png", token: "default" },
];
if (mediaFile.public_bucket_path) {
sdMediaImage = [bucketConfig.getGcsUrl(mediaFile.public_bucket_path)];
sdMediaImage = [
{
url: bucketConfig.getGcsUrl(mediaFile.public_bucket_path),
token: mediaFile?.token,
},
];
}
return (
@@ -208,6 +223,7 @@ export default function MediaPage() {
<SdBatchMediaPanel
key={images.length}
images={mediaFile.maybe_batch_token ? images : sdMediaImage}
onActiveSlideChange={handleActiveSlideChange}
/>
{promptSection}
</>
@@ -285,9 +301,12 @@ export default function MediaPage() {
? mediaTypeLabels[mediaFile?.media_type]
: "";
let audioLink = new BucketConfig().getGcsUrl(mediaFile?.public_bucket_path);
let downloadLink =
activeSlide.url || bucketConfig.getGcsUrl(mediaFile?.public_bucket_path);
const shareUrl = `https://fakeyou.com/media/${mediaFile?.token || ""}`;
const shareUrl = `https://fakeyou.com/media/${
activeSlide.token || mediaFile?.token || ""
}`;
const shareText = "Check out this media on FakeYou.com!";
if (status < 3)
@@ -592,8 +611,8 @@ export default function MediaPage() {
icon={faArrowDownToLine}
label="Download"
className="flex-grow-1"
href={audioLink}
download={audioLink}
href={downloadLink}
download={downloadLink}
variant="secondary"
target="_blank"
/>
@@ -604,8 +623,8 @@ export default function MediaPage() {
icon={faArrowDownToLine}
square={true}
variant="secondary"
href={audioLink}
download={audioLink}
href={downloadLink}
download={downloadLink}
tooltip="Download"
target="_blank"
/>
@@ -9,32 +9,52 @@ import { FreeMode, Navigation, Thumbs } from "swiper/modules";
import { Swiper as SwiperType } from "swiper/types";
import "./SdBatchMediaPanel.scss";
interface SdCoverImagePanelProps {
images: string[];
interface ImageWithToken {
url: string;
token: string;
}
export default function SdBatchMediaPanel({ images }: SdCoverImagePanelProps) {
interface SdBatchMediaPanelProps {
images: ImageWithToken[];
onActiveSlideChange: (image: ImageWithToken) => void;
}
export default function SdBatchMediaPanel({
images,
onActiveSlideChange,
}: SdBatchMediaPanelProps) {
const [thumbsSwiper, setThumbsSwiper] = useState<SwiperType | null>(null);
const [forceUpdateKey, setForceUpdateKey] = useState(0);
const [isPortrait, setIsPortrait] = useState(false);
const [initialSlide, setInitialSlide] = useState(0);
const [isFirstLoad, setIsFirstLoad] = useState(true);
useEffect(() => {
images.forEach(imgSrc => {
const currentUrl = new URL(window.location.href);
const currentToken = currentUrl.pathname.split("/media/")[1].split("?")[0];
const initialSlideIndex = images.findIndex(
image => image.token === currentToken
);
if (initialSlideIndex !== -1) {
setInitialSlide(initialSlideIndex);
}
setIsFirstLoad(false);
}, [images]);
useEffect(() => {
images.forEach(image => {
const img = new Image();
img.onload = () => {
// If any image is portrait, update isPortrait to true
if (img.height > img.width && !isPortrait) {
setIsPortrait(true);
}
};
img.src = imgSrc;
img.src = image.url;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [images]);
}, [images, isPortrait]);
useEffect(() => {
if (images.length > 0) {
// Force re-render of the Swiper component by updating a key
setForceUpdateKey(prevKey => prevKey + 1);
}
}, [images]);
@@ -43,6 +63,20 @@ export default function SdBatchMediaPanel({ images }: SdCoverImagePanelProps) {
setThumbsSwiper(swiper);
};
const handleSlideChange = (swiper: SwiperType) => {
if (!isFirstLoad) {
// Only update URL if it's not the first load
const currentSlideIndex = swiper.realIndex;
const currentImage = images[currentSlideIndex];
const currentImageToken = images[currentSlideIndex].token;
const baseUrl = window.location.href.split("/media/")[0]; // Get the base URL before the token
const queryParams = window.location.search; // Get query parameters if any
const newUrl = `${baseUrl}/media/${currentImageToken}${queryParams}`; // Construct the new URL
window.history.replaceState(null, "", newUrl); // Replace the current history state
onActiveSlideChange(currentImage);
}
};
const secondSwiperClass = `secondSwiper ${
isPortrait ? "portrait" : "landscape"
}`;
@@ -58,29 +92,30 @@ export default function SdBatchMediaPanel({ images }: SdCoverImagePanelProps) {
modules={[FreeMode, Navigation, Thumbs]}
className={secondSwiperClass}
slidesPerView={1}
initialSlide={0}
initialSlide={initialSlide}
onSlideChange={handleSlideChange}
>
{images.map((imgSrc, index) => (
{images.map((image, index) => (
<SwiperSlide key={index}>
<img src={imgSrc} alt={`Slide ${index + 1}`} />
<img src={image.url} alt={`Slide ${index + 1}`} />
</SwiperSlide>
))}
</Swiper>
{images.length > 1 && (
<Swiper
onSwiper={handleSwiper}
loop={true}
loop={false}
spaceBetween={10}
slidesPerView={5}
freeMode={true}
watchSlidesProgress={true}
modules={[FreeMode, Navigation, Thumbs]}
className="firstSwiper"
initialSlide={0}
initialSlide={initialSlide}
>
{images.map((imgSrc, index) => (
{images.map((image, index) => (
<SwiperSlide key={index}>
<img src={imgSrc} alt={`Thumbnail ${index + 1}`} />
<img src={image.url} alt={`Thumbnail ${index + 1}`} />
</SwiperSlide>
))}
</Swiper>