import GridContent from '@/components/PageHeaderWrapper/GridContent'; import { Menu } from 'antd'; import { MenuMode } from 'antd/es/menu'; import { connect } from 'dva'; import * as H from 'history'; import React, { Component } from 'react'; import { match } from 'react-router'; import { FormattedMessage } from 'umi-plugin-locale'; import router from 'umi/router'; import styles from './Info.less'; const { Item } = Menu; interface InfoProps { currentUser: { avatar?: string; name?: string; title?: string; group?: string; signature?: string; geographic?: any; tags?: any[]; userid?: string; }; match: match; location: H.Location; } interface InfoState { mode: MenuMode; menuMap: object; selectKey: string; } @connect(({ user }) => ({ currentUser: user.currentUser, })) class Info extends Component { static getDerivedStateFromProps(props, state) { const { match, location } = props; let selectKey = location.pathname.replace(`${match.path}/`, ''); selectKey = state.menuMap[selectKey] ? selectKey : 'base'; if (selectKey !== state.selectKey) { return { selectKey }; } return null; } main: HTMLDivElement; constructor(props) { super(props); const { match, location } = props; const menuMap = { base: , security: ( ), binding: ( ), notification: ( ), }; const key = location.pathname.replace(`${match.path}/`, ''); this.state = { mode: 'inline', menuMap, selectKey: menuMap[key] ? key : 'base', }; } componentDidMount() { window.addEventListener('resize', this.resize); this.resize(); } componentWillUnmount() { window.removeEventListener('resize', this.resize); } getmenu = () => { const { menuMap } = this.state; return Object.keys(menuMap).map(item => {menuMap[item]}); }; getRightTitle = () => { const { selectKey, menuMap } = this.state; return menuMap[selectKey]; }; selectKey = ({ key }) => { router.push(`/account/settings/${key}`); this.setState({ selectKey: key, }); }; resize = () => { if (!this.main) { return; } requestAnimationFrame(() => { let mode: MenuMode = 'inline'; const { offsetWidth } = this.main; if (this.main.offsetWidth < 641 && offsetWidth > 400) { mode = 'horizontal'; } if (window.innerWidth < 768 && offsetWidth > 400) { mode = 'horizontal'; } this.setState({ mode, }); }); }; render() { const { children, currentUser } = this.props; if (!currentUser.userid) { return ''; } const { mode, selectKey } = this.state; return (
{ this.main = ref; }} >
{this.getmenu()}
{this.getRightTitle()}
{children}
); } } export default Info;