-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ show image alongside yt embed. +media Carousel
- Loading branch information
1 parent
60e1bc6
commit baf4f9c
Showing
2 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
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,58 @@ | ||
import { ChevronLeftIcon, ChevronRightIcon } from "@chakra-ui/icons"; | ||
import { GetUserLaptimesResponse } from "../../Types"; | ||
import * as React from "react"; | ||
import miscFunctions from "../../misc/miscFunctions"; | ||
import { Box, Button } from "@chakra-ui/react"; | ||
|
||
|
||
type MediaItem = { | ||
type: 'youtube' | 'image'; | ||
content: string; | ||
}; | ||
|
||
type MediaCarouselProps = { | ||
youtubeLink?: string; | ||
image?: string; | ||
}; | ||
|
||
const MediaCarousel: React.FC<MediaCarouselProps> = ({ youtubeLink, image }) => { | ||
|
||
const { LazyLoadYoutubeEmbed } = miscFunctions(); | ||
const [currentIndex, setCurrentIndex] = React.useState(0); | ||
const mediaItems: MediaItem[] = [ | ||
youtubeLink && { type: 'youtube', content: youtubeLink }, | ||
image && { type: 'image', content: image } | ||
].filter((item): item is MediaItem => Boolean(item)); | ||
|
||
const nextSlide = () => { | ||
setCurrentIndex((prevIndex) => (prevIndex + 1) % mediaItems.length); | ||
}; | ||
|
||
const prevSlide = () => { | ||
setCurrentIndex((prevIndex) => (prevIndex - 1 + mediaItems.length) % mediaItems.length); | ||
}; | ||
|
||
if (mediaItems.length === 0) return null; | ||
|
||
return ( | ||
<Box position="relative"> | ||
{mediaItems[currentIndex].type === 'youtube' ? ( | ||
<LazyLoadYoutubeEmbed youtubeLink={mediaItems[currentIndex].content} /> | ||
) : ( | ||
<Box as="img" src={mediaItems[currentIndex].content} alt="User uploaded image" width="100%" height="auto" objectFit="cover" /> | ||
)} | ||
{mediaItems.length > 1 && ( | ||
<> | ||
<Button position="absolute" left="0" top="50%" transform="translateY(-50%)" onClick={prevSlide}> | ||
<ChevronLeftIcon /> | ||
</Button> | ||
<Button position="absolute" right="0" top="50%" transform="translateY(-50%)" onClick={nextSlide}> | ||
<ChevronRightIcon /> | ||
</Button> | ||
</> | ||
)} | ||
</Box> | ||
); | ||
}; | ||
|
||
export default MediaCarousel; |
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