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

93 lines
2.3 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-17 13:27:22 +08:00
import { debounce } from "../../libs/utils";
2023-08-26 14:31:13 +08:00
import { useMemo, useState } from "react";
2023-08-20 23:30:08 +08:00
import { syncAll } from "../../libs/sync";
2023-08-26 14:31:13 +08:00
import Button from "@mui/material/Button";
import { useAlert } from "../../hooks/Alert";
2023-07-31 15:08:51 +08:00
export default function SyncSetting() {
const i18n = useI18n();
const sync = useSync();
2023-08-26 14:31:13 +08:00
const alert = useAlert();
const [loading, setLoading] = useState(false);
2023-07-31 15:08:51 +08:00
2023-08-17 13:27:22 +08:00
const handleChange = useMemo(
() =>
2023-08-20 23:30:08 +08:00
debounce(async (e) => {
2023-08-17 13:27:22 +08:00
e.preventDefault();
const { name, value } = e.target;
2023-08-20 23:30:08 +08:00
await sync.update({
2023-08-17 13:27:22 +08:00
[name]: value,
});
2023-08-26 14:31:13 +08:00
// trySyncAll();
}, 500),
2023-08-17 13:27:22 +08:00
[sync]
);
2023-08-26 14:31:13 +08:00
const handleSyncTest = async (e) => {
e.preventDefault();
try {
setLoading(true);
await syncAll();
alert.success(i18n("data_sync_success"));
} catch (err) {
console.log("[sync all]", err);
alert.error(i18n("data_sync_error"));
} finally {
setLoading(false);
}
};
2023-07-31 15:08:51 +08:00
if (!sync.opt) {
return;
}
const { syncUrl, syncKey } = sync.opt;
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-07-31 15:08:51 +08:00
defaultValue={syncUrl}
2023-08-17 13:27:22 +08:00
onChange={handleChange}
2023-07-31 15:08:51 +08:00
helperText={
<Link href={URL_KISS_WORKER}>{i18n("about_sync_api")}</Link>
}
/>
<TextField
size="small"
type="password"
label={i18n("data_sync_key")}
2023-08-17 13:27:22 +08:00
name="syncKey"
2023-07-31 15:08:51 +08:00
defaultValue={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
<Stack direction="row" spacing={2} useFlexGap flexWrap="wrap">
<Button
size="small"
variant="contained"
disabled={!syncUrl || !syncKey || loading}
onClick={handleSyncTest}
>
{i18n("data_sync_test")}
</Button>
</Stack>
2023-07-31 15:08:51 +08:00
</Stack>
</Box>
);
}