"use client" import { useOutsideClick } from "@/hooks/out-side-click" import { cn } from "@/lib/utils" import { IconArrowNarrowLeft, IconArrowNarrowRight } from "@tabler/icons-react" import { motion } from "framer-motion" import Image, { ImageProps } from "next/image" import { createContext, Fragment, JSX, useEffect, useRef, useState, } from "react" interface CarouselProps { items: JSX.Element[] initialScroll?: number cardGap?: number scrollOffset?: number initialActiveIndex?: number } interface Project { id: number title: string description: string image: string link: string } const CarouselContext = createContext<{ onCardClick: (index: number) => void currentIndex: number }>({ onCardClick: () => { }, currentIndex: 0, }) export const Carousel = ({ items, initialScroll = 0, cardGap = 16, scrollOffset: initialScrollOffset, initialActiveIndex = 1, }: CarouselProps) => { const [scrollOffset, setScrollOffset] = useState(950) useEffect(() => { const WidthWindow = window.innerWidth if (WidthWindow < 789) { setScrollOffset(260) } else { setScrollOffset(950) } }, []) const carouselRef = useRef(null) const [canScrollLeft, setCanScrollLeft] = useState(false) const [canScrollRight, setCanScrollRight] = useState(true) const [currentIndex, setCurrentIndex] = useState(initialActiveIndex) const checkScrollAbility = () => { if (carouselRef.current) { const { scrollLeft, scrollWidth, clientWidth } = carouselRef.current setCanScrollLeft(scrollLeft > 0) setCanScrollRight(scrollLeft < scrollWidth - clientWidth) } } useEffect(() => { if (carouselRef.current && initialActiveIndex !== undefined) { const itemWidth = carouselRef.current.offsetWidth - 500 const scrollTo = itemWidth * initialActiveIndex carouselRef.current.scrollTo({ left: scrollTo, behavior: "smooth", }) } }, [initialActiveIndex]) const scroll = (direction: "left" | "right") => { if (carouselRef.current) { carouselRef.current.scrollBy({ left: direction === "left" ? -scrollOffset : scrollOffset, behavior: "smooth", }) } } const handleCarouselClick = (index: number) => { setCurrentIndex(index) } return (
{items.map((item, index) => ( {item} ))}
) } export const Card = ({ project, index, layout = false, }: { project: Project index: number layout?: boolean }) => { const [isOpen, setIsOpen] = useState(false) const containerRef = useRef(null) const cardRef = useRef(null) useOutsideClick(containerRef, () => setIsOpen(false)) const handleCardClick = () => { setIsOpen(true) if (cardRef.current) { cardRef.current.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "center", }) } } return (
{project.title} {project.description}
) } export const BlurImage = ({ height, width, src, className, alt, ...rest }: ImageProps) => { const [isLoading, setLoading] = useState(true) return ( setLoading(false)} src={src} width={width} height={height} loading="lazy" decoding="async" blurDataURL={typeof src === "string" ? src : undefined} alt={alt ? alt : "Background of a beautiful view"} {...rest} /> ) }