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

116 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-04-15 18:04:35 +08:00
import { useState, useEffect } from "react";
2023-10-26 23:55:05 +08:00
import Stack from "@mui/material/Stack";
import FavBtn from "./FavBtn";
2023-11-22 10:23:14 +08:00
import Typography from "@mui/material/Typography";
2024-03-25 18:14:12 +08:00
import AudioBtn from "./AudioBtn";
2024-04-15 18:04:35 +08:00
import CircularProgress from "@mui/material/CircularProgress";
import Alert from "@mui/material/Alert";
2024-04-16 16:29:59 +08:00
import { OPT_TRANS_BAIDU, PHONIC_MAP } from "../../config";
2024-04-15 18:04:35 +08:00
import { apiTranslate } from "../../apis";
import { isValidWord } from "../../libs/utils";
2024-04-16 11:25:04 +08:00
import CopyBtn from "./CopyBtn";
2023-10-26 23:55:05 +08:00
2024-04-15 18:04:35 +08:00
export default function DictCont({ text }) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
const [dictResult, setDictResult] = useState(null);
useEffect(() => {
(async () => {
try {
setLoading(true);
setError("");
setDictResult(null);
if (!isValidWord(text)) {
return;
}
const dictRes = await apiTranslate({
text,
translator: OPT_TRANS_BAIDU,
fromLang: "en",
toLang: "zh-CN",
});
if (dictRes[2]?.type === 1) {
setDictResult(JSON.parse(dictRes[2].result));
}
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
})();
}, [text]);
if (error) {
return <Alert severity="error">{error}</Alert>;
}
if (loading) {
return <CircularProgress size={16} />;
}
if (!text || !dictResult) {
2023-10-26 23:55:05 +08:00
return;
}
2024-04-16 11:25:04 +08:00
const copyText = [
dictResult.src,
dictResult.voice
?.map(Object.entries)
.map((item) => item[0])
2024-04-16 16:29:59 +08:00
.map(([key, val]) => `${PHONIC_MAP[key]?.[0] || key} ${val}`)
2024-04-16 11:25:04 +08:00
.join(" "),
dictResult.content[0].mean
.map(({ pre, cont }) => {
return `${pre ? `[${pre}] ` : ""}${Object.keys(cont).join("; ")}`;
})
.join("\n"),
].join("\n");
2023-10-26 23:55:05 +08:00
return (
2024-04-16 11:25:04 +08:00
<Stack spacing={1}>
<Stack direction="row" justifyContent="space-between">
2023-11-22 10:23:14 +08:00
<Typography variant="subtitle1" style={{ fontWeight: "bold" }}>
2024-01-18 15:26:37 +08:00
{dictResult.src}
2023-11-22 10:23:14 +08:00
</Typography>
2024-04-16 11:25:04 +08:00
<Stack direction="row" justifyContent="space-between">
<CopyBtn text={copyText} />
<FavBtn word={dictResult.src} />
</Stack>
2023-10-26 23:55:05 +08:00
</Stack>
2024-01-18 15:26:37 +08:00
<Typography component="div">
2024-04-16 11:25:04 +08:00
<Typography component="div">
2024-01-18 15:26:37 +08:00
{dictResult.voice
2024-02-21 15:41:27 +08:00
?.map(Object.entries)
2024-01-18 15:26:37 +08:00
.map((item) => item[0])
2024-03-25 18:14:12 +08:00
.map(([key, val]) => (
2024-04-16 11:25:04 +08:00
<Typography
component="div"
key={key}
style={{ display: "inline-block" }}
>
<Typography component="span">{`${
2024-04-16 16:29:59 +08:00
PHONIC_MAP[key]?.[0] || key
2024-04-16 11:25:04 +08:00
} ${val}`}</Typography>
2024-04-16 16:29:59 +08:00
<AudioBtn text={dictResult.src} lan={PHONIC_MAP[key]?.[1]} />
2024-04-16 11:25:04 +08:00
</Typography>
2024-03-25 18:14:12 +08:00
))}
2024-04-16 11:25:04 +08:00
</Typography>
<Typography component="ul">
2024-01-18 15:26:37 +08:00
{dictResult.content[0].mean.map(({ pre, cont }, idx) => (
2024-04-16 11:25:04 +08:00
<Typography component="li" key={idx}>
2024-01-18 15:26:37 +08:00
{pre && `[${pre}] `}
{Object.keys(cont).join("; ")}
2024-04-16 11:25:04 +08:00
</Typography>
2023-10-26 23:55:05 +08:00
))}
2024-04-16 11:25:04 +08:00
</Typography>
2024-01-18 15:26:37 +08:00
</Typography>
2024-04-16 11:25:04 +08:00
</Stack>
2023-10-26 23:55:05 +08:00
);
}