import { PageHeader } from '@ant-design/pro-layout'; import { css } from '@emotion/css'; import { Layout, Menu, Result } from 'antd'; import _, { get } from 'lodash'; import React, { createContext, useCallback, useMemo } from 'react'; import { Navigate, Outlet, useLocation, useNavigate } from 'react-router-dom'; import { useStyles } from './style'; import { ADMIN_SETTINGS_PATH, PluginSettingsPageType, useApp } from '../application'; import { useCompile } from '../schema-component'; export const SettingsCenterContext = createContext({}); function getMenuItems(list: PluginSettingsPageType[]) { return list.map((item) => { return { key: item.name, label: item.label, title: item.title, icon: item.icon, children: item.children?.length ? getMenuItems(item.children) : undefined, }; }); } export const SettingsCenterComponent = () => { const { styles, theme } = useStyles(); const app = useApp(); const navigate = useNavigate(); const location = useLocation(); const compile = useCompile(); const settings = useMemo(() => { const list = app.pluginSettingsManager.getList(); // compile title function traverse(settings: PluginSettingsPageType[]) { settings.forEach((item) => { item.title = compile(item.title); item.label = compile(item.title); if (item.children?.length) { traverse(item.children); } }); } traverse(list); return list; }, [app.pluginSettingsManager, compile]); const getFirstDeepChildPath = useCallback((settings: PluginSettingsPageType[]) => { if (!settings || !settings.length) { return '/admin'; } const first = settings[0]; if (first.children?.length) { return getFirstDeepChildPath(first.children); } return first.path; }, []); const settingsMapByPath = useMemo>(() => { const map = {}; const traverse = (settings: PluginSettingsPageType[]) => { settings.forEach((item) => { map[item.path] = item; if (item.children?.length) { traverse(item.children); } }); }; traverse(settings); return map; }, [settings]); const currentSetting = useMemo(() => settingsMapByPath[location.pathname], [location.pathname, settingsMapByPath]); const currentPlugin = useMemo(() => { if (!currentSetting) { return null; } return settings.find((item) => item.name === currentSetting.pluginName); }, [currentSetting, settings]); const sidebarMenus = useMemo(() => { return getMenuItems(settings.map((item) => ({ ...item, children: null }))); }, [settings]); if (!currentSetting || location.pathname === ADMIN_SETTINGS_PATH || location.pathname === ADMIN_SETTINGS_PATH + '/') { return ; } if (location.pathname === currentPlugin.path && currentPlugin.children?.length > 0) { return ; } return (
{ const plugin = settings.find((item) => item.name === key); if (plugin.children?.length) { return navigate(getFirstDeepChildPath(plugin.children)); } else { return navigate(plugin.path); } }} items={sidebarMenus} /> {currentSetting && ( 0 ? 0 : theme.paddingSM, }} ghost={false} title={currentPlugin.title} footer={ currentPlugin.children?.length > 0 && ( { navigate(app.pluginSettingsManager.getRoutePath(key)); }} selectedKeys={[currentSetting?.name]} mode="horizontal" items={getMenuItems(currentPlugin.children)} > ) } /> )}
{currentSetting ? ( ) : ( )}
); };