-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from handong-app/feat/junglesub/better-image
Feat/junglesub/better image
- Loading branch information
Showing
5 changed files
with
134 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,69 @@ | ||
import "./FeedCardGallery.css"; | ||
import { Gallery, useGallery } from "react-photoswipe-gallery"; | ||
import { getExtensionFromUrl, isImage, isVideo } from "../tools/tools"; | ||
|
||
import "photoswipe/dist/photoswipe.css"; | ||
import FeedCardImageItem from "./FeedCardImageItem"; | ||
|
||
const FeedCardGallery = ({ images = [] }) => { | ||
const filteredImages = images.filter((url) => isImage(url) || isVideo(url)); | ||
if (filteredImages.length === 0) return <></>; | ||
return ( | ||
<div className="FeedCardGallery"> | ||
<div className={`post-images images-${filteredImages.length}`}> | ||
{filteredImages.slice(0, 3).map((url, index) => | ||
isImage(url) ? ( | ||
<img key={index} src={url} alt={`${index + 1}`} loading="lazy" /> | ||
) : isVideo(url) ? ( | ||
<video key={index} controls> | ||
<source src={url} type={`video/${getExtensionFromUrl(url)}`} /> | ||
Your browser does not support the video tag. | ||
</video> | ||
) : ( | ||
<>Unknown Data</> | ||
) | ||
)} | ||
{filteredImages.length >= 4 && ( | ||
<div className="more-images-overlay"> | ||
{filteredImages.length > 5 && <span>+{images.length - 4}</span>} | ||
<img key={1} src={images[3]} alt={`4`} loading="lazy" /> | ||
</div> | ||
)} | ||
</div> | ||
<Gallery withDownloadButton> | ||
<FeedCardGalleryContent | ||
images={images} | ||
filteredImages={filteredImages} | ||
/> | ||
</Gallery> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FeedCardGallery; | ||
|
||
const FeedCardGalleryContent = ({ filteredImages, images }) => { | ||
const { open } = useGallery(); | ||
|
||
return ( | ||
<div className={`post-images images-${filteredImages.length}`}> | ||
{filteredImages.slice(0, 3).map((url, index) => | ||
isImage(url) ? ( | ||
<FeedCardImageItem key={index} url={url} index={index} /> | ||
) : // <img key={index} src={url} alt={`${index + 1}`} loading="lazy" /> | ||
isVideo(url) ? ( | ||
<video key={index} controls> | ||
<source src={url} type={`video/${getExtensionFromUrl(url)}`} /> | ||
Your browser does not support the video tag. | ||
</video> | ||
) : ( | ||
<>Unknown Data</> | ||
) | ||
)} | ||
{filteredImages.length >= 4 && ( | ||
<div className="more-images-overlay"> | ||
{filteredImages.length > 5 && ( | ||
<span | ||
onClick={() => { | ||
open(3); | ||
}} | ||
> | ||
+{images.length - 4} | ||
</span> | ||
)} | ||
<FeedCardImageItem url={images[3]} index={3} /> | ||
{filteredImages.slice(4).map((url, index) => ( | ||
<FeedCardImageItem | ||
key={index + 4} | ||
url={url} | ||
index={index + 4} | ||
hidden={true} | ||
/> | ||
))} | ||
|
||
{/* <img key={1} src={images[3]} alt={`4`} loading="lazy" /> */} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Item } from "react-photoswipe-gallery"; | ||
|
||
function FeedCardImageItem({ url, index, hidden }) { | ||
const [dimensions, setDimensions] = useState({ width: 300, height: 300 }); | ||
|
||
useEffect(() => { | ||
const loadDimensions = async () => { | ||
try { | ||
const { width, height } = await preloadImage(url); | ||
setDimensions({ width, height }); | ||
} catch (error) { | ||
console.error("Failed to load image dimensions", error); | ||
} | ||
}; | ||
|
||
loadDimensions(); | ||
}, [url]); | ||
|
||
return ( | ||
<Item | ||
original={url} | ||
thumbnail={url} | ||
width={dimensions.width} | ||
height={dimensions.height} | ||
> | ||
{({ ref, open }) => ( | ||
<img | ||
ref={ref} | ||
onClick={open} | ||
src={url} | ||
alt={`image-${index}`} | ||
className="thumbnail" | ||
loading="lazy" | ||
style={ | ||
{ display: hidden ? "none" : "block" } | ||
// { display: "block" } | ||
} | ||
/> | ||
)} | ||
</Item> | ||
); | ||
} | ||
|
||
export default FeedCardImageItem; | ||
|
||
const preloadImage = (src) => { | ||
return new Promise((resolve, reject) => { | ||
const img = new Image(); | ||
img.src = src; | ||
img.onload = () => resolve({ width: img.width, height: img.height }); | ||
img.onerror = reject; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters