"use client"; import { CardResponse, DeckResponse } from "@/lib/types/cards"; import React, { useCallback, useEffect, useState } from "react"; import { Button } from "../ui/button"; import { ChevronLeftIcon, ChevronRightIcon, RotateCcwIcon } from "lucide-react"; import "./cards.css"; import Flashcard from "./Card"; // --- Main App Component --- function Deck({ data }: { data: DeckResponse }) { const [currentPage, setCurrentPage] = useState(0); const [currentIndex, setCurrentIndex] = useState(0); const [isFlipped, setIsFlipped] = useState(false); const [animationDirection, setAnimationDirection] = useState< "enter-left" | "enter-right" | "exit-left" | "exit-right" | "none" >("none"); const flashcards = data.cards; const [isAnimating, setIsAnimating] = useState(false); const handleFlip = () => { if (isAnimating) return; setIsFlipped(!isFlipped); }; const handleNext = useCallback(() => { if (isAnimating || currentIndex >= flashcards.length - 1) return; setIsAnimating(true); setIsFlipped(false); // Flip back to front before changing card setAnimationDirection("exit-left"); setTimeout(() => { setCurrentIndex((prevIndex) => Math.min(prevIndex + 1, flashcards.length - 1), ); setAnimationDirection("enter-right"); setTimeout(() => { setAnimationDirection("none"); setIsAnimating(false); }, 500); // Duration of enter animation }, 500); // Duration of exit animation }, [currentIndex, flashcards.length, isAnimating]); const handlePrev = useCallback(() => { if (isAnimating || currentIndex <= 0) return; setIsAnimating(true); setIsFlipped(false); // Flip back to front setAnimationDirection("exit-right"); setTimeout(() => { setCurrentIndex((prevIndex) => Math.max(prevIndex - 1, 0)); setAnimationDirection("enter-left"); setTimeout(() => { setAnimationDirection("none"); setIsAnimating(false); }, 500); // Duration of enter animation }, 500); // Duration of exit animation }, [currentIndex, isAnimating]); // Keyboard navigation useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (isAnimating) return; if (event.key === "ArrowRight") { handleNext(); } else if (event.key === "ArrowLeft") { handlePrev(); } else if (event.key === " " || event.key === "Enter") { // Space or Enter to flip event.preventDefault(); // Prevent scrolling if space is pressed handleFlip(); } }; window.addEventListener("keydown", handleKeyDown); return () => { window.removeEventListener("keydown", handleKeyDown); }; }, [handleNext, handlePrev, isAnimating]); if (flashcards.length === 0) { return (

No flashcards available.

); } const currentCard = flashcards[currentIndex]; if (!currentCard) return

wtf

; return (
{/* This div is for positioning the card and managing overflow during animations */}
{" "} {/* Ensure this matches card height */}

Card {currentIndex + 1} of {flashcards.length}

Use Arrow Keys (← →) to navigate, Space/Enter to flip.
); } export default Deck;