import { ChangeEvent } from "react"; import toast from "react-hot-toast"; import StyledContainer from "./StyledContainer"; import Toggle from "../../../common/component/styled/Toggle"; import Label from "../../../common/component/styled/Label"; import Input from "../../../common/component/styled/Input"; import SaveTip from "../../../common/component/SaveTip"; import useConfig from "../../../common/hook/useConfig"; import Tooltip from "./Tooltip"; import IssuerList from "./IssuerList"; import useGoogleAuthConfig from "../../../common/hook/useGoogleAuthConfig"; import useGithubAuthConfig from "../../../common/hook/useGithubAuthConfig"; import { LoginConfig } from "../../../types/server"; import { useTranslation } from "react-i18next"; export default function Logins() { const { t } = useTranslation("setting", { keyPrefix: "login" }); const { changed: clientIdChanged, clientId, updateClientId, updateClientIdToServer } = useGoogleAuthConfig(); const { config: githubAuthConfig, changed: githubChanged, updateGithubAuthConfigToServer, updateGithubAuthConfig } = useGithubAuthConfig(); const { values, updateConfig, setValues, reset, changed } = useConfig("login"); const handleUpdate = async () => { const { google } = values as LoginConfig; if (changed) { updateConfig(values); } if (google && clientIdChanged) { // 更新google client id await updateClientIdToServer(); if (!changed) { toast.success("Configuration Updated!"); } } if (github && githubChanged) { // github config await updateGithubAuthConfigToServer(); if (!changed) { toast.success("Configuration Updated!"); } } }; const handleGoogleClientIdChange = (evt: ChangeEvent) => { updateClientId(evt.target.value); }; const handleGithubAuthChange = (evt: ChangeEvent) => { const { key } = evt.target.dataset; if (key) { updateGithubAuthConfig({ [key]: evt.target.value }); } }; const handleToggle = ( val: Partial> ) => { setValues((prev) => { if (!prev) return prev; return { ...prev, ...val }; }); }; if (!values) return null; const { google, magic_link, github, metamask, password, oidc = [] } = values as LoginConfig; const valuesChanged = clientIdChanged || changed || githubChanged; return (
{t("password_desc")}
{t("magic_link_desc")}
{t("google_desc")}
{t("github_desc")}
{t("metamask_desc")}
{t("oidc_desc")}
{ setValues((prev) => { if (!prev) return prev; return { ...prev, oidc: newOidc }; }); }} />
{valuesChanged && }
); }