BasicLayout.tsx 3.25 KB
Newer Older
zombieJ's avatar
zombieJ committed
1 2 3 4 5 6
/**
 * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
 * You can view component api by:
 * https://github.com/ant-design/ant-design-pro-layout
 */

7
import { ConnectProps, ConnectState } from '@/models/connect';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8
import {
9
  MenuDataItem,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10 11
  BasicLayout as ProLayoutComponents,
  BasicLayoutProps as ProLayoutComponentsProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12 13
  Settings,
} from '@ant-design/pro-layout';
14 15 16
import React, { useState } from 'react';

import Authorized from '@/utils/Authorized';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17
import Link from 'umi/link';
18 19 20 21 22 23
import RightContent from '@/components/GlobalHeader/RightContent';
import { connect } from 'dva';
import { formatMessage } from 'umi-plugin-react/locale';
import { isAntDesignPro } from '@/utils/utils';
import logo from '../assets/logo.svg';

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
24
export interface BasicLayoutProps extends ProLayoutComponentsProps, ConnectProps {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25 26 27
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
28
  settings: Settings;
29
}
30
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31 32 33
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
34
};
35

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36
/**
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
 * use Authorized check all menu item
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38
 */
39 40 41 42 43 44
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  menuList.map(item => {
    const localItem = {
      ...item,
      children: item.children ? menuDataRender(item.children) : [],
    };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
45 46 47
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48
const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
49
  if (!isAntDesignPro()) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51 52 53 54 55 56
    return defaultDom;
  }
  return (
    <>
      {defaultDom}
      <div
        style={{
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
57
          padding: '0px 24px 24px',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
58
          textAlign: 'center',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59 60
        }}
      >
61
        <a href="https://www.netlify.com" target="_blank" rel="noopener noreferrer">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
62
          <img
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63
            src="https://www.netlify.com/img/global/badges/netlify-color-bg.svg"
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64
            width="82px"
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
65 66 67 68 69 70 71 72
            alt="netlify logo"
          />
        </a>
      </div>
    </>
  );
};

何乐's avatar
何乐 committed
73
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
74
  const { dispatch, children, settings } = props;
何乐's avatar
何乐 committed
75 76 77
  /**
   * constructor
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78

79
  useState(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80 81 82 83 84 85 86 87
    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
      dispatch({
        type: 'settings/getSetting',
      });
    }
88
  });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
89

何乐's avatar
何乐 committed
90 91 92
  /**
   * init variables
   */
93
  const handleMenuCollapse = (payload: boolean): void =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
94 95
    dispatch &&
    dispatch({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
96 97 98 99
      type: 'global/changeLayoutCollapsed',
      payload,
    });

100
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
101
    <ProLayoutComponents
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
102 103
      logo={logo}
      onCollapse={handleMenuCollapse}
104 105 106 107 108 109 110 111 112 113 114 115 116
      menuItemRender={(menuItemProps, defaultDom) => (
        <Link to={menuItemProps.path}>{defaultDom}</Link>
      )}
      breadcrumbRender={(routers = []) => [
        {
          path: '/',
          breadcrumbName: formatMessage({
            id: 'menu.home',
            defaultMessage: 'Home',
          }),
        },
        ...routers,
      ]}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117
      footerRender={footerRender}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
118 119 120 121 122 123 124
      menuDataRender={menuDataRender}
      formatMessage={formatMessage}
      rightContentRender={rightProps => <RightContent {...rightProps} />}
      {...props}
      {...settings}
    >
      {children}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
125
    </ProLayoutComponents>
126 127 128
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
129
export default connect(({ global, settings }: ConnectState) => ({
130
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
131
  settings,
132
}))(BasicLayout);