import LightButton from "@/components/buttons/LightButton";
import { WordData } from "@/interfaces";
import { Dispatch, SetStateAction, useState } from "react";
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
import { getTTSAudioUrl } from "@/utils";
import { VOICES } from "@/config/locales";
import { useTranslations } from "next-intl";
interface WordBoardProps {
children: React.ReactNode;
}
function WordBoard({ children }: WordBoardProps) {
return (
{children}
);
}
interface Props {
wordData: WordData;
setPage: Dispatch>;
}
export default function Start({ wordData, setPage }: Props) {
const t = useTranslations("memorize.start");
const [display, setDisplay] = useState<"ask" | "show">("ask");
const [wordPair, setWordPair] = useState(
wordData.wordPairs[Math.floor(Math.random() * wordData.wordPairs.length)],
);
const [reverse, setReverse] = useState(false);
const [dictation, setDictation] = useState(false);
const { load, play } = useAudioPlayer();
const show = () => {
setDisplay("show");
};
const next = async () => {
setDisplay("ask");
const newWordPair =
wordData.wordPairs[Math.floor(Math.random() * wordData.wordPairs.length)];
setWordPair(newWordPair);
if (dictation)
await load(
await getTTSAudioUrl(
newWordPair[reverse ? 1 : 0],
VOICES.find((v) => v.locale === wordData.locales[reverse ? 1 : 0])!
.short_name,
),
).then(play);
};
return (
{dictation ? (
<>
{display === "show" && (
<>
{wordPair[reverse ? 1 : 0]}
{wordPair[reverse ? 0 : 1]}
>
)}
>
) : (
<>
{wordPair[reverse ? 1 : 0]}
{display === "show" && (
{wordPair[reverse ? 0 : 1]}
)}
>
)}
{display === "ask" ? (
{t("show")}
) : (
{t("next")}
)}
setReverse(!reverse)}
selected={reverse}
>
{t("reverse")}
setDictation(!dictation)}
selected={dictation}
>
{t("dictation")}
setPage("main")}>
{t("back")}
);
}