import { List, Select, Typography } from "antd"; import { useEffect, useState } from "react"; import { TeamOutlined, RobotOutlined } from "@ant-design/icons"; import { get } from "../../request"; import type { IApiResponseDictList, IDictFirstMeaningResponse, IFirstMeaning, } from "../../api/Dict"; const { Text, Link } = Typography; interface IFactorInfo { factors: string; type: string; confidence: number; } interface IOptions { value: string; label: React.ReactNode; } interface IWidget { word?: string; add?: string; split?: string; onSearch?: Function; } const CompoundWidget = ({ word, add, onSearch }: IWidget) => { const [compound, setCompound] = useState([]); const [factors, setFactors] = useState([]); const [meaningData, setMeaningData] = useState(); const [currValue, setCurrValue] = useState(); const onSelectChange = (value?: string) => { console.log("selected", value); setCurrValue(value); if (typeof value === "undefined") { setMeaningData(undefined); } else { const url = `/v2/dict-meaning?lang=zh-Hans&word=` + value.replaceAll("+", "-"); console.info("dict compound url", url); get(url).then((json) => { if (json.ok) { setMeaningData(json.data); } }); } }; useEffect(() => { console.debug("compound changed", add, compound); if (typeof add === "undefined") { setFactors(compound); const value = compound.length > 0 ? compound[0].value : undefined; setCurrValue(value); onSelectChange(value); } else { setFactors([{ value: add, label: add }, ...compound]); setCurrValue(add); onSelectChange(add); } }, [add, compound]); useEffect(() => { setMeaningData([]); setFactors([]); if (typeof word === "undefined") { return; } const url = `/v2/userdict?view=word&word=${word}`; console.info("dict compound url", url); get(url).then((json) => { if (json.ok) { const factors = new Map(); json.data.rows .filter((value) => typeof value.factors === "string") .forEach((value) => { let type = ""; if (value.source?.includes("_USER")) { type = "user"; } if (value.type === ".cp.") { type = "robot"; } if (value.factors) { factors.set(value.factors, { factors: value.factors, type: type, confidence: value.confidence, }); } }); const arrFactors: IFactorInfo[] = []; factors.forEach((value) => { arrFactors.push(value); }); arrFactors.sort((a, b) => b.confidence - a.confidence); setCompound( arrFactors.map((item) => { return { value: item.factors, label: (
{item.factors} {item.type === "user" ? ( ) : item.type === "robot" ? ( ) : ( <> )}
), }; }) ); } }); }, [word]); return (