BasicLayout.tsx 2.9 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
/**
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
28
 * use Authorized check all menu item
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
29
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31 32 33
  return menuList.map(item => {
    const localItem = {
      ...item,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34
      children: item.children ? menuDataRender(item.children) : [],
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35
    };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37 38 39 40
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

何乐's avatar
何乐 committed
41
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
42
  const { dispatch, children, settings, location } = props;
何乐's avatar
何乐 committed
43 44 45
  /**
   * constructor
   */
46
  useState(() => {
何乐's avatar
何乐 committed
47
    dispatch!({ type: 'user/fetchCurrent' });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48
    dispatch!({ type: 'settings/getSetting' });
49
  });
何乐's avatar
何乐 committed
50 51 52
  /**
   * init variables
   */
53
  const handleMenuCollapse = (payload: boolean) =>
何乐's avatar
何乐 committed
54
    dispatch!({ type: 'global/changeLayoutCollapsed', payload });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
55

56
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
57 58 59 60
    <>
      <BasicLayoutComponents
        logo={logo}
        onCollapse={handleMenuCollapse}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
61 62 63
        menuItemRender={(menuItemProps, defaultDom) => {
          return <Link to={menuItemProps.path}>{defaultDom}</Link>;
        }}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
        breadcrumbRender={(routers = []) => {
          return [
            {
              path: '/',
              breadcrumbName: formatMessage({
                id: 'menu.home',
                defaultMessage: 'Home',
              }),
            },
            ...routers,
          ];
        }}
        menuDataRender={menuDataRender}
        formatMessage={formatMessage}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78
        rightContentRender={rightProps => <RightContent {...rightProps} />}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
79
        {...props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80
        {...settings}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
81 82 83
      >
        {children}
      </BasicLayoutComponents>
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
84 85 86 87 88 89 90 91 92 93 94
      {ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' && (
        <SettingDrawer
          settings={settings}
          onSettingChange={config =>
            dispatch!({
              type: 'settings/changeSetting',
              payload: config,
            })
          }
        />
      )}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
95
      <CopyBlock url={location!.pathname} />
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
96
    </>
97 98 99
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
100
export default connect(({ global, settings }: ConnectState) => ({
101
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
102
  settings,
103
}))(BasicLayout);