|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import type { Card, CardStore } from "~/hooks/useCardStore"; |
| 3 | + |
| 4 | +type CardsGridProps = { |
| 5 | + cards: CardStore; |
| 6 | +}; |
| 7 | + |
| 8 | +export default function CardsGrid({ cards }: CardsGridProps) { |
| 9 | + const [activeTab, setActiveTab] = useState<number | undefined>(undefined); |
| 10 | + const cardGroups: { id: number; cards: Card[] }[] = []; |
| 11 | + const groups = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 12 | + |
| 13 | + for (const groupId of groups) { |
| 14 | + const cardsInGroup = Object.values(cards) |
| 15 | + .filter((card) => card.groupId === groupId) |
| 16 | + .sort((a, b) => a.cardId - b.cardId); |
| 17 | + if (cardsInGroup.length < 0) continue; |
| 18 | + cardGroups.push({ |
| 19 | + id: groupId, |
| 20 | + cards: cardsInGroup, |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + setActiveTab(cardGroups[0].id); |
| 26 | + }, []); |
| 27 | + |
| 28 | + function handleTabClick(groupId: number) { |
| 29 | + setActiveTab(groupId); |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="w-full"> |
| 34 | + {cardGroups.map((group) => { |
| 35 | + if (!group.cards) return null; |
| 36 | + return ( |
| 37 | + <button |
| 38 | + type="button" |
| 39 | + key={group.id} |
| 40 | + onClick={() => handleTabClick(group.id)} |
| 41 | + > |
| 42 | + {group.id} |
| 43 | + </button> |
| 44 | + ); |
| 45 | + })} |
| 46 | + |
| 47 | + {/* Tab Content */} |
| 48 | + {cardGroups.map((group) => ( |
| 49 | + <div |
| 50 | + className="flex flex-wrap flex-row justify-start p-2" |
| 51 | + key={group.id} |
| 52 | + style={{ display: activeTab === group.id ? "flex" : "none" }} |
| 53 | + > |
| 54 | + {group.cards.map((card) => { |
| 55 | + let color: string = ""; |
| 56 | + const opacity = Math.floor( |
| 57 | + 100 - (card.forgetProbability / 0.7) * 100, |
| 58 | + ); |
| 59 | + if (card.forgetProbability > 0.7 && card.correctStreak !== null) |
| 60 | + color = |
| 61 | + "border-red-300 dark:border-red-700 rounded-full border-2"; |
| 62 | + if (card.forgetProbability <= 0.7) |
| 63 | + color = `bg-green-300 dark:bg-green-700 rounded-sm opacity-[.${opacity}]`; // not working for some values!! |
| 64 | + return ( |
| 65 | + <div |
| 66 | + key={card.cardId} |
| 67 | + className={`flex-none w-9 h-9 m-[calc((100%-288px)/16)] flex items-center justify-center ${color}`} |
| 68 | + > |
| 69 | + <a href={`/questions/${card.groupId}-${card.cardId}`}> |
| 70 | + {card.cardId} |
| 71 | + </a> |
| 72 | + </div> |
| 73 | + ); |
| 74 | + })} |
| 75 | + </div> |
| 76 | + ))} |
| 77 | + </div> |
| 78 | + ); |
| 79 | +} |
0 commit comments