summaryrefslogtreecommitdiff
path: root/packages/prosody-ui/src/LangText.tsx
blob: ab9d4f4255c54b6db95a8fa120f1800500c26faf (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { franc } from "franc-all";
import React, { useEffect, useState } from "react";
import ThaiText from "./thai/ThaiText";
import { ColoredText } from "./components/Sentence";
import type { AnalyzeRes, ColorTheme, WordData } from "./logic/types";
import { detectScript, scriptFromLang } from "./logic/utils";
import LatinText from "./latin/LatinText";
import { buildWiktionaryURL, parseWiktionary } from "./logic/wiki";
import FullWord from "./components/word/FullWordData";
import type { AsyncRes, Result } from "@sortug/lib";
import type { FullWordData } from "@sortug/langlib";

export default function LangText({
  text,
  lang,
  theme,
  handleWord,
  handleError,
}: {
  text: string;
  theme?: ColorTheme;
  lang?: string;
  handleWord?: (word: AnalyzeRes) => any;
  handleError?: (error: string) => any;
}) {
  const background: ColorTheme = theme ? theme : "light";
  const [llang, setLang] = useState("");
  const [script, setScript] = useState(scriptFromLang(lang || "", text));
  const [modal, setWordModal] = useState<FullWordData | null>(null);
  useEffect(() => {
    if (!lang) {
      const res = franc(text);
      setLang(res);
    } else setLang(lang);
  }, [text]);
  console.log("langtext", { text, llang, script });

  async function openWord(word: AnalyzeRes) {
    if (handleWord) handleWord(word);
    else {
      const body = JSON.stringify({
        getWordFull: { spelling: word.word, lang: llang },
      });
      const opts = {
        method: "POST",
        body,
        headers: { "Content-type": "application/json" },
      };
      const res = await fetch("/api/db", opts);
      const j = (await res.json()) as Result<FullWordData>;
      console.log({ j });
      if ("error" in j) {
        if (handleError) handleError(j.error);
        else console.error("error opening word", j.error);
      } else {
        setWordModal(j.ok);
      }
    }
    // console.log("looking up", word);
    // const url = buildWiktionaryURL(word);
    // const html = await fetchWiki(url);
    // const parsed = parseWiktionary(html, url);
    // console.log({ parsed });
    // if ("error" in parsed) handleWord(parsed);
    // else {
    //   const wd: WordData = {
    //     spelling: word,
    //     lang: llang,
    //     ipa: parsed.ok.ipa[0],
    //     meanings: parsed.ok.meanings,
    //     references: { url: parsed.ok.url },
    //   };
    //   handleWord({ ok: wd });
    // }
    // // const d = data[s];
    // // setModal(d);
    // // setModal(<WordModal data={d} lang={lang} />);
  }

  return (
    <div className="lang-text-container">
      {script === "Thai" ? (
        <ThaiText text={text} theme={background} openWord={openWord} />
      ) : script === "Latin" ? (
        <LatinText text={text} lang={llang} openWord={openWord} />
      ) : (
        <Generic text={text} lang={llang} />
      )}
      {modal && (
        <WordModal
          word={modal}
          lang={llang}
          theme={background}
          onClose={() => setWordModal(null)}
        />
      )}
    </div>
  );
}
function Generic({ text, lang }: { text: string; lang: string }) {
  const [data, setData] = useState<AnalyzeRes>();
  useEffect(() => {
    // segmentate(text, lang)
  }, [text, lang]);
  console.log({ lang }, "generic");
  // <p className="lang-lang">{lang}</p>
  // <p className="lang-text">{text}</p>
  // {data && <ColoredText frags={Object.keys(data)} />}
  return <div className="lang-text-div"></div>;
}

function WordModal({
  word,
  lang,
  theme,
  onClose,
}: {
  word: FullWordData;
  lang: string;
  theme: ColorTheme;
  onClose: () => void;
}) {
  return (
    <div
      id="modal-bg"
      role="dialog"
      aria-modal="true"
      onMouseDown={(event) => {
        if (event.target === event.currentTarget) {
          onClose();
        }
      }}
    >
      <div
        id="modal-fg"
        style={{ backgroundColor: "white", border: "5px solid black" }}
      >
        <FullWord data={word} lang={lang} theme={"light"} />
      </div>
    </div>
  );
}