/* eslint-disable no-constant-condition */ import '@babel/polyfill'; import React, { useState, useEffect, useLayoutEffect } from 'react'; import { BrowserRouter, Switch, Route, useLocation, useHistory } from 'react-router-dom'; import { get as lodashGet } from 'lodash'; import { DProLayout, AppContainer, IconFont, Menu, Utils, Page403, Page404, Page500, Modal } from 'knowdesign'; import dantdZhCN from 'knowdesign/es/locale/zh_CN'; import dantdEnUS from 'knowdesign/es/locale/en_US'; import { DotChartOutlined } from '@ant-design/icons'; import { licenseEventBus } from './constants/axiosConfig'; import intlZhCN from './locales/zh'; import intlEnUS from './locales/en'; import registerApps from '../config/registerApps'; import feSystemsConfig from '../config/systemsConfig'; import './index.less'; import { Login } from './pages/Login'; import { getLicenseInfo } from './constants/common'; import api from './api'; import ClusterContainer from './pages/index'; import NoLicense from './pages/NoLicense'; import ksLogo from './assets/ks-logo.png'; interface ILocaleMap { [index: string]: any; } const localeMap: ILocaleMap = { 'zh-CN': { dantd: dantdZhCN, intl: 'zh-CN', intlMessages: intlZhCN, }, en: { dantd: dantdEnUS, intl: 'en', intlMessages: intlEnUS, }, }; const primaryFeConf = feSystemsConfig.feConfig; const systemsConfig = feSystemsConfig.systemsConfig as any; const defaultLanguage = 'zh-CN'; export const { Provider, Consumer } = React.createContext('zh'); const judgePage404 = () => { const { pathname } = window.location; const paths = pathname.split('/'); const exceptionLocationPaths = ['/404', '/500', '/403']; const row = systemsConfig.filter((item: any) => item.ident === paths?.[1]); if (exceptionLocationPaths.indexOf(pathname) < -1 && paths?.[1] && !row.length) { window.location.href = '/404'; } }; const logout = () => { Utils.request(api.logout, { method: 'POST', }).then((res) => { window.location.href = '/login'; }); localStorage.removeItem('userInfo'); }; const LicenseLimitModal = () => { const [visible, setVisible] = useState(false); const [msg, setMsg] = useState(''); useLayoutEffect(() => { licenseEventBus.on('licenseError', (desc: string) => { !visible && setVisible(true); setMsg(desc); }); return () => { licenseEventBus.removeAll('licenseError'); }; }, []); return ( 许可证限制 } footer={null} onCancel={() => setVisible(false)} >
); }; const AppContent = (props: { setlanguage: (language: string) => void }) => { const { pathname } = useLocation(); const history = useHistory(); const userInfo = localStorage.getItem('userInfo'); const [curActiveAppName, setCurActiveAppName] = useState(''); useEffect(() => { if (pathname.startsWith('/config')) { setCurActiveAppName('config'); } else { setCurActiveAppName('cluster'); } }, [pathname]); return ( ), username: userInfo ? JSON.parse(userInfo)?.userName : '', icon: , quickEntries: [ { icon: , txt: '多集群管理', ident: '', active: curActiveAppName === 'cluster', }, { icon: , txt: '系统管理', ident: 'config', active: curActiveAppName === 'config', }, ], isFixed: false, userDropMenuItems: [ 登出 , ], onChangeLanguage: props.setlanguage, onClickQuickEntry: (qe) => { history.push({ pathname: '/' + (qe.ident || ''), }); }, onClickMain: () => { history.push('/'); }, }} onMount={(customProps: any) => { judgePage404(); registerApps(systemsConfig, { ...customProps, getLicenseInfo, licenseEventBus }, () => { // postMessage(); }); }} > <> { return ( <> {curActiveAppName === 'cluster' && }
); }} /> ); }; export default function App(): JSX.Element { const [language, setlanguage] = useState(navigator.language.substr(0, 2)); const intlMessages = lodashGet(localeMap[language], 'intlMessages', intlZhCN); const [feConf] = useState(primaryFeConf || {}); const locale = lodashGet(localeMap[defaultLanguage], 'intl', 'zh-CN'); const antdLocale = lodashGet(localeMap[defaultLanguage], 'dantd', dantdZhCN); const pageTitle = lodashGet(feConf, 'title'); if (pageTitle) { document.title = pageTitle; } useEffect(() => { window.postMessage( { type: 'language', value: language, }, window.location.origin ); }, [language]); return ( <> } /> ); }