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(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; 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(); } return (
{script === "Thai" ? ( ) : script === "Latin" ? ( ) : ( )} {modal && ( setWordModal(null)} /> )}
); } function Generic({ text, lang }: { text: string; lang: string }) { const [data, setData] = useState(); useEffect(() => { // segmentate(text, lang) }, [text, lang]); console.log({ lang }, "generic"); //

{lang}

//

{text}

// {data && } return
; } function WordModal({ word, lang, theme, onClose, }: { word: FullWordData; lang: string; theme: ColorTheme; onClose: () => void; }) { return ( ); }