import { Button, Typography } from "antd"; import { useEffect, useState } from "react"; import { LoadingOutlined } from "@ant-design/icons"; import Marked from "../general/Marked"; import { get } from "../../request"; import type { IAiTranslateResponse } from "../../api/ai"; const { Text } = Typography; interface IAiTranslateWidget { origin?: string; paragraph?: string; autoLoad?: boolean; trigger?: boolean; } const AiTranslate = ({ paragraph, autoLoad = false, trigger = false, }: IAiTranslateWidget) => { const [loading, setLoading] = useState(false); const [translation, setTranslation] = useState(); const [error, setError] = useState(); const url = "/v2/ai-translate"; useEffect(() => { if (typeof paragraph === "undefined") { return; } if (!autoLoad) { return; } //onTranslatePara(); }, [paragraph, autoLoad]); const onTranslatePara = () => { const _url = `${url}/${paragraph}`; console.info("api request", _url); setLoading(true); get(_url) .then((json) => { console.debug("api response", json); if (json.ok) { setTranslation(json.data.choices[0].message.content); } else { setError(json.message); } }) .finally(() => setLoading(false)); }; if (translation) { return ; } else if (loading) { return ; } else if (error) { return (
{error}
); } else if (trigger) { return ( ); } else { return <>; } }; export default AiTranslate;