From 1434c538e43e02c5472ceb0d84318a50834265c0 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 19 Jul 2025 14:59:23 +0000 Subject: [PATCH] Refine card reveal in one-by-one mode Make one card visible at a time in response to user input in one-by-one mode. --- src/components/BinarySearchTrick.tsx | 71 +++++++++++++++++++++------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/src/components/BinarySearchTrick.tsx b/src/components/BinarySearchTrick.tsx index 0745eae..8a33f70 100644 --- a/src/components/BinarySearchTrick.tsx +++ b/src/components/BinarySearchTrick.tsx @@ -5,7 +5,7 @@ import { Checkbox } from '@/components/ui/checkbox'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { Switch } from '@/components/ui/switch'; import { Label } from '@/components/ui/label'; -import { HelpCircle, RefreshCw } from 'lucide-react'; +import { HelpCircle, RefreshCw, ChevronLeft, ChevronRight } from 'lucide-react'; import SocialShare from '@/components/SocialShare'; interface BinarySearchTrickProps { @@ -17,7 +17,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) = const [result, setResult] = useState(null); const [showResult, setShowResult] = useState(false); const [isOneByOne, setIsOneByOne] = useState(false); - const [visibleCards, setVisibleCards] = useState([]); + const [currentCardIndex, setCurrentCardIndex] = useState(0); const [cards, setCards] = useState>([]); // Function to shuffle array @@ -70,23 +70,23 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) = // Initialize cards on component mount useEffect(() => { setCards(generateCards()); - setVisibleCards([16, 8, 4, 2, 1]); // All cards visible by default }, []); // Handle mode toggle const handleModeToggle = (checked: boolean) => { setIsOneByOne(checked); - if (checked) { - // Start one-by-one mode - show first card - setVisibleCards([16]); - setTimeout(() => setVisibleCards(prev => [...prev, 8]), 500); - setTimeout(() => setVisibleCards(prev => [...prev, 4]), 1000); - setTimeout(() => setVisibleCards(prev => [...prev, 2]), 1500); - setTimeout(() => setVisibleCards(prev => [...prev, 1]), 2000); - } else { - // Show all cards immediately - setVisibleCards([16, 8, 4, 2, 1]); - } + setCurrentCardIndex(0); + setSelectedCards([]); + setShowResult(false); + }; + + // Navigation functions for one-by-one mode + const goToPreviousCard = () => { + setCurrentCardIndex(prev => Math.max(0, prev - 1)); + }; + + const goToNextCard = () => { + setCurrentCardIndex(prev => Math.min(cards.length - 1, prev + 1)); }; const handleCardToggle = (cardId: number) => { @@ -108,6 +108,16 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) = setSelectedCards([]); setResult(null); setShowResult(false); + setCurrentCardIndex(0); + setCards(generateCards()); // Regenerate with new randomized order + }; + + // Get cards to display based on mode + const getDisplayCards = () => { + if (isOneByOne) { + return cards.length > 0 ? [cards[currentCardIndex]] : []; + } + return cards; }; return ( @@ -192,10 +202,37 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) = + {/* Navigation controls for one-by-one mode */} + {isOneByOne && cards.length > 0 && ( +
+ + + + Card {currentCardIndex + 1} of {cards.length} + + + +
+ )} +
- {cards - .filter(card => visibleCards.includes(card.id)) - .map((card) => ( + {getDisplayCards().map((card) => (