diff --git a/frontend/lecture/package.json b/frontend/lecture/package.json index 4f31e4f..49e5bba 100644 --- a/frontend/lecture/package.json +++ b/frontend/lecture/package.json @@ -9,8 +9,7 @@ "check-types": "tsc", "lint": "eslint .", "format": "prettier . --check", - "preview": "vite preview", - "check-all": "yarn check-types && yarn lint && yarn format" + "check-all": "yarn check-types && yarn lint && yarn format && yarn knip" }, "dependencies": { "@radix-ui/react-icons": "1.3.0", @@ -19,7 +18,6 @@ "@radix-ui/react-tabs": "1.1.0", "class-variance-authority": "0.7.0", "clsx": "2.1.1", - "embla-carousel-react": "8.2.0", "react": "18.3.1", "react-dom": "18.3.1", "react-markdown": "9.0.1", diff --git a/frontend/lecture/src/components/Slides/index.tsx b/frontend/lecture/src/components/Slides/index.tsx index 507ced2..2f14a0f 100644 --- a/frontend/lecture/src/components/Slides/index.tsx +++ b/frontend/lecture/src/components/Slides/index.tsx @@ -1,19 +1,18 @@ -import { ReactNode } from 'react'; +import { ReactNode, useCallback, useEffect } from 'react'; +import { useSearchParams } from 'react-router-dom'; import { Card, CardContent, - CardFooter, CardHeader, CardTitle, } from '@/designsystem/ui/card'; import { - Carousel, - CarouselContent, - CarouselItem, - CarouselNext, - CarouselPrevious, -} from '@/designsystem/ui/carousel'; + Pagination, + PaginationContent, + PaginationItem, + PaginationLink, +} from '@/designsystem/ui/pagination'; import { Separator } from '@/designsystem/ui/separator'; export const Slides = ({ @@ -21,33 +20,98 @@ export const Slides = ({ }: { slides: { title: string; content: ReactNode }[]; }) => { + const { page: page, onChangePage: onChangePage } = usePage({ + minPage: 1, + maxPage: slides.length, + }); + + const slide = slides[page - 1]; + + useEffect(() => { + const keydownHandler = (event: KeyboardEvent) => { + if (event.key === 'ArrowRight') onChangePage(page + 1); + else if (event.key === 'ArrowLeft') onChangePage(page - 1); + }; + + window.addEventListener('keydown', keydownHandler); + return () => { + window.removeEventListener('keydown', keydownHandler); + }; + }, [page, onChangePage]); + + if (slide === undefined) return null; + return ( - - - {slides.map((slide, index) => ( - - - - {slide.title} - - - - - - {slide.content} - - - - - - {index + 1}/{slides.length} - - - - ))} - - - - +
+ + + {slide.title} + + + + + + {slide.content} + + + + + {Array.from({ length: slides.length }, (_, i) => { + const itemPage = i + 1; + return ( + { + onChangePage(itemPage); + }} + > + + {itemPage} + + + ); + })} + + +
); }; + +const usePage = ({ + maxPage: maxPage, + minPage: minPage, +}: { + maxPage: number; + minPage: number; +}) => { + const key = 'page'; + const [searchParams, setSearchParams] = useSearchParams(); + + const pageParams = searchParams.get(key); + + const page = (() => { + const calculatedPage = + pageParams !== null && `${parseInt(pageParams)}` === pageParams + ? parseInt(pageParams) + : 1; + + if (calculatedPage < minPage) return minPage; + if (calculatedPage > maxPage) return maxPage; + return calculatedPage; + })(); + + const onChangePage = useCallback( + (newPage: number) => { + if (newPage < 1 || newPage > maxPage) return; + const newSearchParams = new URLSearchParams(searchParams); + newSearchParams.set(key, newPage.toString()); + setSearchParams(newSearchParams, { replace: true }); + }, + [searchParams, setSearchParams, maxPage], + ); + + return { page, onChangePage }; +}; diff --git a/frontend/lecture/src/designsystem/ui/carousel.tsx b/frontend/lecture/src/designsystem/ui/carousel.tsx deleted file mode 100644 index fb83063..0000000 --- a/frontend/lecture/src/designsystem/ui/carousel.tsx +++ /dev/null @@ -1,259 +0,0 @@ -import { ArrowLeftIcon, ArrowRightIcon } from '@radix-ui/react-icons'; -import useEmblaCarousel, { - type UseEmblaCarouselType, -} from 'embla-carousel-react'; -import * as React from 'react'; - -import { Button } from '@/designsystem/ui/button'; -import { cn } from '@/utils/designsystem'; - -type CarouselApi = UseEmblaCarouselType[1]; -type UseCarouselParameters = Parameters; -type CarouselOptions = UseCarouselParameters[0]; -type CarouselPlugin = UseCarouselParameters[1]; - -type CarouselProps = { - opts?: CarouselOptions; - plugins?: CarouselPlugin; - orientation?: 'horizontal' | 'vertical'; - setApi?: (api: CarouselApi) => void; -}; - -type CarouselContextProps = { - carouselRef: ReturnType[0]; - api: ReturnType[1]; - scrollPrev: () => void; - scrollNext: () => void; - canScrollPrev: boolean; - canScrollNext: boolean; -} & CarouselProps; - -const CarouselContext = React.createContext(null); - -function useCarousel() { - const context = React.useContext(CarouselContext); - - if (context === null) { - throw new Error('useCarousel must be used within a '); - } - - return context; -} - -const Carousel = React.forwardRef< - HTMLDivElement, - React.HTMLAttributes & CarouselProps ->( - ( - { - orientation = 'horizontal', - opts, - setApi, - plugins, - className, - children, - ...props - }, - ref, - ) => { - const [carouselRef, api] = useEmblaCarousel( - { - ...opts, - axis: orientation === 'horizontal' ? 'x' : 'y', - }, - plugins, - ); - const [canScrollPrev, setCanScrollPrev] = React.useState(false); - const [canScrollNext, setCanScrollNext] = React.useState(false); - - const onSelect = React.useCallback((carouselApi: CarouselApi) => { - if (carouselApi === undefined) { - return; - } - - setCanScrollPrev(carouselApi.canScrollPrev()); - setCanScrollNext(carouselApi.canScrollNext()); - }, []); - - const scrollPrev = React.useCallback(() => { - api?.scrollPrev(); - }, [api]); - - const scrollNext = React.useCallback(() => { - api?.scrollNext(); - }, [api]); - - const handleKeyDown = React.useCallback( - (event: React.KeyboardEvent) => { - if (event.key === 'ArrowLeft') { - event.preventDefault(); - scrollPrev(); - } else if (event.key === 'ArrowRight') { - event.preventDefault(); - scrollNext(); - } - }, - [scrollPrev, scrollNext], - ); - - React.useEffect(() => { - if (api === undefined || setApi === undefined) { - return; - } - - setApi(api); - }, [api, setApi]); - - React.useEffect(() => { - if (api === undefined) { - return; - } - - onSelect(api); - api.on('reInit', onSelect); - api.on('select', onSelect); - - return () => { - api.off('select', onSelect); - }; - }, [api, onSelect]); - - return ( - -
- {children} -
-
- ); - }, -); -Carousel.displayName = 'Carousel'; - -const CarouselContent = React.forwardRef< - HTMLDivElement, - React.HTMLAttributes ->(({ className, ...props }, ref) => { - const { carouselRef, orientation } = useCarousel(); - - return ( -
-
-
- ); -}); -CarouselContent.displayName = 'CarouselContent'; - -const CarouselItem = React.forwardRef< - HTMLDivElement, - React.HTMLAttributes ->(({ className, ...props }, ref) => { - const { orientation } = useCarousel(); - - return ( -
- ); -}); -CarouselItem.displayName = 'CarouselItem'; - -const CarouselPrevious = React.forwardRef< - HTMLButtonElement, - React.ComponentProps ->(({ className, variant = 'outline', size = 'icon', ...props }, ref) => { - const { orientation, scrollPrev, canScrollPrev } = useCarousel(); - - return ( - - ); -}); -CarouselPrevious.displayName = 'CarouselPrevious'; - -const CarouselNext = React.forwardRef< - HTMLButtonElement, - React.ComponentProps ->(({ className, variant = 'outline', size = 'icon', ...props }, ref) => { - const { orientation, scrollNext, canScrollNext } = useCarousel(); - - return ( - - ); -}); -CarouselNext.displayName = 'CarouselNext'; - -export { - type CarouselApi, - Carousel, - CarouselContent, - CarouselItem, - CarouselPrevious, - CarouselNext, -}; diff --git a/frontend/lecture/src/designsystem/ui/pagination.tsx b/frontend/lecture/src/designsystem/ui/pagination.tsx new file mode 100644 index 0000000..99a6703 --- /dev/null +++ b/frontend/lecture/src/designsystem/ui/pagination.tsx @@ -0,0 +1,121 @@ +import { + ChevronLeftIcon, + ChevronRightIcon, + DotsHorizontalIcon, +} from '@radix-ui/react-icons'; +import * as React from 'react'; + +import { ButtonProps, buttonVariants } from '@/designsystem/ui/button'; +import { cn } from '@/utils/designsystem'; + +const Pagination = ({ className, ...props }: React.ComponentProps<'nav'>) => ( +