import TextField from "@mui/material/TextField";
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
import Stack from "@mui/material/Stack";
import { useI18n } from "../../hooks/I18n";
import { DEFAULT_API_SETTING } from "../../config";
import { useEffect, useState } from "react";
import { apiTranslate } from "../../apis";
import CopyBtn from "./CopyBtn";
import Typography from "@mui/material/Typography";
import Alert from "@mui/material/Alert";
import { tryDetectLang } from "../../libs";
export default function TranCont({
text,
apiSlug,
fromLang,
toLang,
toLang2 = "en",
transApis,
simpleStyle,
langDetector,
}) {
const i18n = useI18n();
const [trText, setTrText] = useState("");
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
useEffect(() => {
(async () => {
try {
setLoading(true);
setTrText("");
setError("");
let to = toLang;
if (fromLang === "auto" && toLang !== toLang2 && toLang2 !== "none") {
const detectLang = await tryDetectLang(text, true, langDetector);
if (detectLang === toLang) {
to = toLang2;
}
}
const apiSetting =
transApis.find((api) => api.apiSlug === apiSlug) ||
DEFAULT_API_SETTING;
const [trText] = await apiTranslate({
text,
apiSlug,
fromLang,
toLang: to,
apiSetting,
});
setTrText(trText);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
})();
}, [text, apiSlug, fromLang, toLang, toLang2, transApis, langDetector]);
if (simpleStyle) {
return (
{error ? (
{error}
) : loading ? (
) : (
{trText}
)}
);
}
return (
: null,
endAdornment: (
),
}}
/>
);
}