BasicLayout.tsx 2.79 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
import { ConnectState, ConnectProps } from '@/models/connect';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2
import RightContent from '@/components/GlobalHeader/RightContent';
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
3
import CopyBlock from '@/components/CopyBlock';
4
import { connect } from 'dva';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
5
import React, { useState } from 'react';
6
import logo from '../assets/logo.svg';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7 8
import Authorized from '@/utils/Authorized';
import { formatMessage } from 'umi-plugin-react/locale';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
9 10 11 12 13
import {
  BasicLayout as BasicLayoutComponents,
  BasicLayoutProps as BasicLayoutComponentsProps,
  MenuDataItem,
  Settings,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
14
  SettingDrawer,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
15
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
16
import Link from 'umi/link';
17

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
18
export interface BasicLayoutProps extends BasicLayoutComponentsProps, ConnectProps {
何乐's avatar
何乐 committed
19
  breadcrumbNameMap: { [path: string]: MenuDataItem };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
  settings: Settings;
21 22
}

23
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
何乐's avatar
何乐 committed
24
  breadcrumbNameMap: { [path: string]: MenuDataItem };
25
};
26

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
27 28 29
/**
 * default menuLocal
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
const filterMenuData = (menuList: MenuDataItem[], locale: boolean): MenuDataItem[] => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31 32 33
  return menuList.map(item => {
    const localItem = {
      ...item,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34 35 36 37
      name:
        item.locale && locale
          ? formatMessage({ id: item.locale, defaultMessage: item.name })
          : item.name,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38
      children: item.children ? filterMenuData(item.children, locale) : [],
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39
    };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
41 42 43 44
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

何乐's avatar
何乐 committed
45
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
46
  const { dispatch, children, settings, location } = props;
何乐's avatar
何乐 committed
47 48 49
  /**
   * constructor
   */
50
  useState(() => {
何乐's avatar
何乐 committed
51
    dispatch!({ type: 'user/fetchCurrent' });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52
    dispatch!({ type: 'settings/getSetting' });
53
  });
何乐's avatar
何乐 committed
54 55 56
  /**
   * init variables
   */
57
  const handleMenuCollapse = (payload: boolean) =>
何乐's avatar
何乐 committed
58
    dispatch!({ type: 'global/changeLayoutCollapsed', payload });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59 60 61
  const {
    menu: { locale },
  } = settings;
62
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63 64 65 66
    <>
      <BasicLayoutComponents
        logo={logo}
        onCollapse={handleMenuCollapse}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67 68 69
        menuItemRender={(menuItemProps, defaultDom) => {
          return <Link to={menuItemProps.path}>{defaultDom}</Link>;
        }}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
70
        filterMenuData={menuList => filterMenuData(menuList, locale)}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
71
        rightContentRender={rightProps => <RightContent {...rightProps} />}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72
        {...props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73
        {...settings}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
74 75 76
      >
        {children}
      </BasicLayoutComponents>
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
77 78 79 80 81 82 83 84 85 86 87 88
      {ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' && (
        <SettingDrawer
          settings={settings}
          onSettingChange={config =>
            dispatch!({
              type: 'settings/changeSetting',
              payload: config,
            })
          }
        />
      )}
      <CopyBlock url={location.pathname} />
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
89
    </>
90 91 92
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
93
export default connect(({ global, settings }: ConnectState) => ({
94
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
95
  settings,
96
}))(BasicLayout);