import { Option, Select } from "@mui/joy"; import { CogIcon, DatabaseIcon, KeyIcon, LibraryIcon, LucideIcon, Settings2Icon, UserIcon, UsersIcon } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useLocation } from "react-router-dom"; import MobileHeader from "@/components/MobileHeader"; import MemberSection from "@/components/Settings/MemberSection"; import MemoRelatedSettings from "@/components/Settings/MemoRelatedSettings"; import MyAccountSection from "@/components/Settings/MyAccountSection"; import PreferencesSection from "@/components/Settings/PreferencesSection"; import SSOSection from "@/components/Settings/SSOSection"; import SectionMenuItem from "@/components/Settings/SectionMenuItem"; import StorageSection from "@/components/Settings/StorageSection"; import WorkspaceSection from "@/components/Settings/WorkspaceSection"; import useCurrentUser from "@/hooks/useCurrentUser"; import { useCommonContext } from "@/layouts/CommonContextProvider"; import { useWorkspaceSettingStore } from "@/store/v1"; import { User_Role } from "@/types/proto/api/v1/user_service"; import { WorkspaceSettingKey } from "@/types/proto/store/workspace_setting"; import { useTranslate } from "@/utils/i18n"; type SettingSection = "my-account" | "preference" | "member" | "system" | "memo-related" | "storage" | "sso"; interface State { selectedSection: SettingSection; } const BASIC_SECTIONS: SettingSection[] = ["my-account", "preference"]; const ADMIN_SECTIONS: SettingSection[] = ["member", "system", "memo-related", "storage", "sso"]; const SECTION_ICON_MAP: Record = { "my-account": UserIcon, preference: CogIcon, member: UsersIcon, system: Settings2Icon, "memo-related": LibraryIcon, storage: DatabaseIcon, sso: KeyIcon, }; const Setting = () => { const t = useTranslate(); const location = useLocation(); const commonContext = useCommonContext(); const user = useCurrentUser(); const workspaceSettingStore = useWorkspaceSettingStore(); const [state, setState] = useState({ selectedSection: "my-account", }); const isHost = user.role === User_Role.HOST; const settingsSectionList = useMemo(() => { let settingList = [...BASIC_SECTIONS]; if (isHost) { settingList = settingList.concat(ADMIN_SECTIONS); } return settingList; }, [isHost]); useEffect(() => { let hash = location.hash.slice(1) as SettingSection; // If the hash is not a valid section, redirect to the default section. if (![...BASIC_SECTIONS, ...ADMIN_SECTIONS].includes(hash)) { hash = "my-account"; } setState({ selectedSection: hash, }); }, [location.hash]); useEffect(() => { if (!isHost) { return; } // Initial fetch for workspace settings. (async () => { [WorkspaceSettingKey.MEMO_RELATED, WorkspaceSettingKey.STORAGE].forEach(async (key) => { await workspaceSettingStore.fetchWorkspaceSetting(key); }); })(); }, [isHost]); const handleSectionSelectorItemClick = useCallback((settingSection: SettingSection) => { window.location.hash = settingSection; }, []); return (
{t("common.basic")}
{BASIC_SECTIONS.map((item) => ( handleSectionSelectorItemClick(item)} /> ))}
{isHost ? ( <> {t("common.admin")}
{ADMIN_SECTIONS.map((item) => ( handleSectionSelectorItemClick(item)} /> ))} {t("setting.version")}: v{commonContext.profile.version}
) : null}
{state.selectedSection === "my-account" ? ( ) : state.selectedSection === "preference" ? ( ) : state.selectedSection === "member" ? ( ) : state.selectedSection === "system" ? ( ) : state.selectedSection === "memo-related" ? ( ) : state.selectedSection === "storage" ? ( ) : state.selectedSection === "sso" ? ( ) : null}
); }; export default Setting;