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.
This commit is contained in:
parent
325b0dd139
commit
1434c538e4
1 changed files with 54 additions and 17 deletions
|
|
@ -5,7 +5,7 @@ import { Checkbox } from '@/components/ui/checkbox';
|
||||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
||||||
import { Switch } from '@/components/ui/switch';
|
import { Switch } from '@/components/ui/switch';
|
||||||
import { Label } from '@/components/ui/label';
|
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';
|
import SocialShare from '@/components/SocialShare';
|
||||||
|
|
||||||
interface BinarySearchTrickProps {
|
interface BinarySearchTrickProps {
|
||||||
|
|
@ -17,7 +17,7 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
const [result, setResult] = useState<number | null>(null);
|
const [result, setResult] = useState<number | null>(null);
|
||||||
const [showResult, setShowResult] = useState(false);
|
const [showResult, setShowResult] = useState(false);
|
||||||
const [isOneByOne, setIsOneByOne] = useState(false);
|
const [isOneByOne, setIsOneByOne] = useState(false);
|
||||||
const [visibleCards, setVisibleCards] = useState<number[]>([]);
|
const [currentCardIndex, setCurrentCardIndex] = useState(0);
|
||||||
const [cards, setCards] = useState<Array<{id: number; title: string; numbers: number[]}>>([]);
|
const [cards, setCards] = useState<Array<{id: number; title: string; numbers: number[]}>>([]);
|
||||||
|
|
||||||
// Function to shuffle array
|
// Function to shuffle array
|
||||||
|
|
@ -70,23 +70,23 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
// Initialize cards on component mount
|
// Initialize cards on component mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCards(generateCards());
|
setCards(generateCards());
|
||||||
setVisibleCards([16, 8, 4, 2, 1]); // All cards visible by default
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Handle mode toggle
|
// Handle mode toggle
|
||||||
const handleModeToggle = (checked: boolean) => {
|
const handleModeToggle = (checked: boolean) => {
|
||||||
setIsOneByOne(checked);
|
setIsOneByOne(checked);
|
||||||
if (checked) {
|
setCurrentCardIndex(0);
|
||||||
// Start one-by-one mode - show first card
|
setSelectedCards([]);
|
||||||
setVisibleCards([16]);
|
setShowResult(false);
|
||||||
setTimeout(() => setVisibleCards(prev => [...prev, 8]), 500);
|
};
|
||||||
setTimeout(() => setVisibleCards(prev => [...prev, 4]), 1000);
|
|
||||||
setTimeout(() => setVisibleCards(prev => [...prev, 2]), 1500);
|
// Navigation functions for one-by-one mode
|
||||||
setTimeout(() => setVisibleCards(prev => [...prev, 1]), 2000);
|
const goToPreviousCard = () => {
|
||||||
} else {
|
setCurrentCardIndex(prev => Math.max(0, prev - 1));
|
||||||
// Show all cards immediately
|
};
|
||||||
setVisibleCards([16, 8, 4, 2, 1]);
|
|
||||||
}
|
const goToNextCard = () => {
|
||||||
|
setCurrentCardIndex(prev => Math.min(cards.length - 1, prev + 1));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCardToggle = (cardId: number) => {
|
const handleCardToggle = (cardId: number) => {
|
||||||
|
|
@ -108,6 +108,16 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
setSelectedCards([]);
|
setSelectedCards([]);
|
||||||
setResult(null);
|
setResult(null);
|
||||||
setShowResult(false);
|
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 (
|
return (
|
||||||
|
|
@ -192,10 +202,37 @@ const BinarySearchTrick = ({ showSocialShare = true }: BinarySearchTrickProps) =
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
{/* Navigation controls for one-by-one mode */}
|
||||||
|
{isOneByOne && cards.length > 0 && (
|
||||||
|
<div className="flex items-center justify-center gap-4 py-4">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={goToPreviousCard}
|
||||||
|
disabled={currentCardIndex === 0}
|
||||||
|
>
|
||||||
|
<ChevronLeft className="w-4 h-4 mr-1" />
|
||||||
|
Previous
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<span className="text-sm text-muted-foreground px-4">
|
||||||
|
Card {currentCardIndex + 1} of {cards.length}
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={goToNextCard}
|
||||||
|
disabled={currentCardIndex === cards.length - 1}
|
||||||
|
>
|
||||||
|
Next
|
||||||
|
<ChevronRight className="w-4 h-4 ml-1" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-5 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-5 gap-4">
|
||||||
{cards
|
{getDisplayCards().map((card) => (
|
||||||
.filter(card => visibleCards.includes(card.id))
|
|
||||||
.map((card) => (
|
|
||||||
<Card
|
<Card
|
||||||
key={card.id}
|
key={card.id}
|
||||||
className={`cursor-pointer transition-all duration-500 hover:shadow-lg animate-fade-in ${
|
className={`cursor-pointer transition-all duration-500 hover:shadow-lg animate-fade-in ${
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue