BasicLayout.tsx 2.94 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';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17
import { isAntDesignProOrDev } from '@/utils/utils';
18

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

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

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

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

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

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

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