summaryrefslogtreecommitdiff
path: root/src/components/Flashcard/SyllableSpan.tsx
blob: 445895e9348fa3791de58b1237ba21944e5d89d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use client";

import { syllableAction, thaiAnalysis } from "@/actions/lang";
import { CardResponse } from "@/lib/types/cards";
import { ReactNode, useState, useTransition } from "react";
import { Spinner } from "../ui/spinner";
import Modal from "@/components/Modal";
import { getRandomHexColor } from "@/lib/utils";

const SyllableSpan: React.FC<{ spelling: string; lang: string }> = ({
  spelling,
  lang,
}) => {
  const [modalContent, setModalContent] = useState<ReactNode | null>(null);

  const closeModal = () => setModalContent(null);

  const [isPending, startTransition] = useTransition();
  const handleClick = (e: React.MouseEvent) => {
    e.stopPropagation();
    startTransition(async () => {
      const modal = await syllableAction(spelling, lang);
      setModalContent(modal);
    });
  };

  return (
    <>
      <span
        onClick={handleClick}
        className="m-1 hover:text-6xl"
        style={{ color: getRandomHexColor() }}
      >
        {spelling}
      </span>
      {modalContent && (
        <Modal onClose={closeModal} isOpen={!!modalContent}>
          {modalContent}
        </Modal>
      )}
    </>
  );
};

export default SyllableSpan;