Files
kiss-translator/src/views/Selection/TranCont.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-10-24 23:37:53 +08:00
import TextField from "@mui/material/TextField";
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
2023-10-25 11:13:56 +08:00
import Stack from "@mui/material/Stack";
2023-10-24 23:37:53 +08:00
import { useI18n } from "../../hooks/I18n";
import { DEFAULT_API_SETTING } from "../../config";
2023-10-24 23:37:53 +08:00
import { useEffect, useState } from "react";
2024-05-22 23:33:30 +08:00
import { apiTranslate } from "../../apis";
2023-10-26 12:24:24 +08:00
import CopyBtn from "./CopyBtn";
2024-04-16 00:54:37 +08:00
import Typography from "@mui/material/Typography";
2024-04-17 10:03:56 +08:00
import Alert from "@mui/material/Alert";
2025-09-27 21:21:56 +08:00
import { tryDetectLang } from "../../libs/detect";
2023-10-24 23:37:53 +08:00
export default function TranCont({
text,
apiSlug,
2023-10-24 23:37:53 +08:00
fromLang,
toLang,
toLang2 = "en",
2023-10-24 23:37:53 +08:00
transApis,
2024-04-16 00:54:37 +08:00
simpleStyle,
2024-05-22 23:33:30 +08:00
langDetector,
2023-10-24 23:37:53 +08:00
}) {
const i18n = useI18n();
const [trText, setTrText] = useState("");
2024-04-15 18:04:35 +08:00
const [loading, setLoading] = useState(true);
2023-10-24 23:37:53 +08:00
const [error, setError] = useState("");
useEffect(() => {
(async () => {
try {
setLoading(true);
setTrText("");
setError("");
2024-04-15 18:04:35 +08:00
let to = toLang;
if (fromLang === "auto" && toLang !== toLang2 && toLang2 !== "none") {
2025-09-27 21:21:56 +08:00
const detectLang = await tryDetectLang(text, "true", langDetector);
if (detectLang === toLang) {
2024-04-15 18:04:35 +08:00
to = toLang2;
}
}
2023-10-27 13:55:24 +08:00
const apiSetting =
transApis.find((api) => api.apiSlug === apiSlug) ||
DEFAULT_API_SETTING;
2025-09-01 18:56:48 +08:00
const [trText] = await apiTranslate({
2023-10-24 23:37:53 +08:00
text,
apiSlug,
2023-10-24 23:37:53 +08:00
fromLang,
2024-04-15 18:04:35 +08:00
toLang: to,
2023-10-24 23:37:53 +08:00
apiSetting,
});
2025-09-01 18:56:48 +08:00
setTrText(trText);
2023-10-24 23:37:53 +08:00
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
})();
}, [text, apiSlug, fromLang, toLang, toLang2, transApis, langDetector]);
2023-10-24 23:37:53 +08:00
2024-04-16 00:54:37 +08:00
if (simpleStyle) {
return (
2024-04-17 15:35:44 +08:00
<Box className="KT-transbox-target KT-transbox-target_simple">
2024-04-17 10:03:56 +08:00
{error ? (
<Alert severity="error">{error}</Alert>
) : loading ? (
<CircularProgress size={16} />
) : (
<Typography style={{ whiteSpace: "pre-line" }}>{trText}</Typography>
)}
2024-04-16 00:54:37 +08:00
</Box>
);
}
2023-10-24 23:37:53 +08:00
return (
2024-04-17 15:35:44 +08:00
<Box className="KT-transbox-target KT-transbox-target_default">
2024-04-15 18:04:35 +08:00
<TextField
size="small"
label={i18n("translated_text")}
// disabled
fullWidth
multiline
value={trText}
helperText={error}
InputProps={{
startAdornment: loading ? <CircularProgress size={16} /> : null,
endAdornment: (
<Stack
direction="row"
sx={{
position: "absolute",
right: 0,
top: 0,
}}
>
<CopyBtn text={trText} />
</Stack>
),
}}
/>
</Box>
2023-10-24 23:37:53 +08:00
);
}