import LightButton from "@/components/ui/buttons/LightButton"; import IconClick from "@/components/ui/buttons/IconClick"; import IMAGES from "@/config/images"; import { Letter, SupportedAlphabets } from "@/lib/interfaces"; import { Dispatch, KeyboardEvent, SetStateAction, useCallback, useEffect, useState, } from "react"; import { useTranslations } from "next-intl"; export default function MemoryCard({ alphabet, setChosenAlphabet, }: { alphabet: Letter[]; setChosenAlphabet: Dispatch>; }) { const t = useTranslations("alphabet"); const [index, setIndex] = useState(() => alphabet.length > 0 ? Math.floor(Math.random() * alphabet.length) : 0); const [more, setMore] = useState(false); const [ipaDisplay, setIPADisplay] = useState(true); const [letterDisplay, setLetterDisplay] = useState(true); const refresh = useCallback(() => { if (alphabet.length > 0) { setIndex(Math.floor(Math.random() * alphabet.length)); } }, [alphabet.length]); useEffect(() => { const handleKeydown = (e: globalThis.KeyboardEvent) => { if (e.key === " ") refresh(); }; document.addEventListener("keydown", handleKeydown); return () => document.removeEventListener("keydown", handleKeydown); }, [refresh]); const letter = alphabet[index] || { letter: "", letter_name_ipa: "", letter_sound_ipa: "" }; return (
) => e.preventDefault()} >
setChosenAlphabet(null)} >
{letterDisplay ? letter.letter : ""} {ipaDisplay ? letter.letter_sound_ipa : ""}
setMore(!more)} > {more ? ( <> { setLetterDisplay(!letterDisplay); }} > {letterDisplay ? t("hideLetter") : t("showLetter")} { setIPADisplay(!ipaDisplay); }} > {ipaDisplay ? t("hideIPA") : t("showIPA")} ) : ( <> )}
); }