import React, { useState } from "react"; import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import classnames from "classnames"; import { useUser, userService } from "@/services/user"; import ChangePasswordDialog from "./ChangePasswordDialog"; import ChangeAvatarDialog from "./ChangeAvatarDialog"; import ChangeNicknameDialog from "./ChangeNicknameDialog"; import ConfirmDialog from "../common/dailog/ConfirmDialog"; import Card from "../common/Card"; import "./index.css"; import { convertI18nText, I18nText } from "@/common"; import Spinner from "../common/Spinner"; interface SettingSectionProps { title: I18nText; } const SettingSection: React.FC = ({ title, children }) => { const { t } = useTranslation(); return (

{convertI18nText(title, t)}

{children}
); }; interface ButtonSettingItemProps { title: I18nText; subtext?: I18nText; onClick: () => void; first?: boolean; danger?: boolean; } const ButtonSettingItem: React.FC = ({ title, subtext, onClick, first, danger, }) => { const { t } = useTranslation(); return (
{convertI18nText(title, t)} {subtext && ( {convertI18nText(subtext, t)} )}
); }; interface SelectSettingItemProps { title: I18nText; subtext?: I18nText; options: { value: string; label: I18nText; }[]; value?: string; onSelect: (value: string) => void; first?: boolean; } const SelectSettingsItem: React.FC = ({ title, subtext, options, value, onSelect, first, }) => { const { t } = useTranslation(); return (
{convertI18nText(title, t)}
{convertI18nText(subtext, t)}
{value == null ? ( ) : ( )}
); }; const SettingsPage: React.FC = (_) => { const { i18n } = useTranslation(); const user = useUser(); const navigate = useNavigate(); const [dialog, setDialog] = useState< null | "changepassword" | "changeavatar" | "changenickname" | "logout" >(null); const language = i18n.language.slice(0, 2); return ( <>
{user ? ( setDialog("changeavatar")} first /> setDialog("changenickname")} /> setDialog("changepassword")} danger /> { setDialog("logout"); }} danger /> ) : null} { void i18n.changeLanguage(value); }} first />
setDialog(null)} /> setDialog(null)} open={dialog === "logout"} onConfirm={() => { void userService.logout().then(() => { navigate("/"); }); }} /> setDialog(null)} /> setDialog(null)} /> ); }; export default SettingsPage;