Files
kiss-translator/src/views/Options/SyncSetting.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-07-31 15:08:51 +08:00
import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack";
import TextField from "@mui/material/TextField";
import { useI18n } from "../../hooks/I18n";
import { useSync } from "../../hooks/Sync";
import Alert from "@mui/material/Alert";
import Link from "@mui/material/Link";
import { URL_KISS_WORKER } from "../../config";
2023-08-31 00:18:57 +08:00
import { useState } from "react";
2023-08-31 13:38:06 +08:00
import { syncSettingAndRules } from "../../libs/sync";
2023-08-26 14:31:13 +08:00
import Button from "@mui/material/Button";
import { useAlert } from "../../hooks/Alert";
2023-08-26 14:42:50 +08:00
import SyncIcon from "@mui/icons-material/Sync";
import CircularProgress from "@mui/material/CircularProgress";
2023-09-01 10:15:57 +08:00
import { useSetting } from "../../hooks/Setting";
2023-07-31 15:08:51 +08:00
export default function SyncSetting() {
const i18n = useI18n();
2023-08-31 00:18:57 +08:00
const { sync, updateSync } = useSync();
2023-08-26 14:31:13 +08:00
const alert = useAlert();
const [loading, setLoading] = useState(false);
2023-09-01 10:15:57 +08:00
const { reloadSetting } = useSetting();
2023-07-31 15:08:51 +08:00
2023-08-31 00:18:57 +08:00
const handleChange = async (e) => {
e.preventDefault();
const { name, value } = e.target;
await updateSync({
[name]: value,
});
};
2023-08-17 13:27:22 +08:00
2023-08-26 14:31:13 +08:00
const handleSyncTest = async (e) => {
e.preventDefault();
try {
setLoading(true);
2023-08-31 13:38:06 +08:00
await syncSettingAndRules();
2023-09-01 10:15:57 +08:00
await reloadSetting();
2023-09-03 13:11:04 +08:00
alert.success(i18n("sync_success"));
2023-08-26 14:31:13 +08:00
} catch (err) {
console.log("[sync all]", err);
2023-09-03 13:11:04 +08:00
alert.error(i18n("sync_failed"));
2023-08-26 14:31:13 +08:00
} finally {
setLoading(false);
}
};
2023-09-17 21:50:17 +08:00
if (!sync) {
return;
}
2023-09-13 23:24:55 +08:00
const { syncUrl = "", syncKey = "" } = sync;
2023-07-31 15:08:51 +08:00
return (
<Box>
<Stack spacing={3}>
<Alert severity="warning">{i18n("sync_warn")}</Alert>
<TextField
size="small"
label={i18n("data_sync_url")}
2023-08-17 13:27:22 +08:00
name="syncUrl"
2023-08-31 00:18:57 +08:00
value={syncUrl}
2023-08-17 13:27:22 +08:00
onChange={handleChange}
2023-07-31 15:08:51 +08:00
helperText={
2023-09-04 22:29:39 +08:00
<Link href={URL_KISS_WORKER} target="_blank">
{i18n("about_sync_api")}
</Link>
2023-07-31 15:08:51 +08:00
}
/>
<TextField
size="small"
type="password"
label={i18n("data_sync_key")}
2023-08-17 13:27:22 +08:00
name="syncKey"
2023-08-31 00:18:57 +08:00
value={syncKey}
2023-08-17 13:27:22 +08:00
onChange={handleChange}
2023-07-31 15:08:51 +08:00
/>
2023-08-26 14:31:13 +08:00
2023-08-31 00:18:57 +08:00
<Stack
direction="row"
alignItems="center"
spacing={2}
useFlexGap
flexWrap="wrap"
>
2023-08-26 14:31:13 +08:00
<Button
size="small"
variant="contained"
disabled={!syncUrl || !syncKey || loading}
onClick={handleSyncTest}
2023-08-26 14:42:50 +08:00
startIcon={<SyncIcon />}
2023-08-26 14:31:13 +08:00
>
2023-09-08 21:41:32 +08:00
{i18n("sync_now")}
2023-08-26 14:31:13 +08:00
</Button>
2023-08-26 14:42:50 +08:00
{loading && <CircularProgress size={16} />}
2023-08-26 14:31:13 +08:00
</Stack>
2023-07-31 15:08:51 +08:00
</Stack>
</Box>
);
}