BasicLayout.tsx 3.56 KB
Newer Older
zombieJ's avatar
zombieJ committed
1 2 3 4 5
/**
 * 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
 */
6
import { ConnectProps, ConnectState } from '@/models/connect';
7
import ProLayout, {
8
  MenuDataItem,
9
  BasicLayoutProps as ProLayoutProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10
  Settings,
duanledexianxianxian's avatar
duanledexianxianxian committed
11
  SettingDrawer,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12
} from '@ant-design/pro-layout';
13 14
import React, { useState } from 'react';
import Authorized from '@/utils/Authorized';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
15
import Link from 'umi/link';
16 17 18 19 20 21
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';

22
export interface BasicLayoutProps extends ProLayoutProps, ConnectProps {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
23 24 25
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
26
  settings: Settings;
27
}
28
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
29 30 31
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
32
};
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33
/**
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34
 * use Authorized check all menu item
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
36

37 38
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  menuList.map(item => {
duanledexianxianxian's avatar
duanledexianxianxian committed
39
    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40 41 42
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });

duanledexianxianxian's avatar
duanledexianxianxian committed
43
// footerζΈ²ζŸ“
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44
const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
45
  if (!isAntDesignPro()) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46 47
    return defaultDom;
  }
duanledexianxianxian's avatar
duanledexianxianxian committed
48

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

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

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

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

97
  return (
duanledexianxianxian's avatar
duanledexianxianxian committed
98
    <div className="kim-layout">
duanledexianxianxian's avatar
duanledexianxianxian committed
99
      <ProLayout
duanledexianxianxian's avatar
duanledexianxianxian committed
100 101 102
        siderWidth={200}
        logo={false}
        navTheme="dark"
duanledexianxianxian's avatar
duanledexianxianxian committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116
        onCollapse={handleMenuCollapse}
        menuItemRender={(menuItemProps, defaultDom) => (
          <Link to={menuItemProps.path}>{defaultDom}</Link>
        )}
        breadcrumbRender={(routers = []) => [
          {
            path: '/',
            breadcrumbName: formatMessage({
              id: 'menu.home',
              defaultMessage: 'Home',
            }),
          },
          ...routers,
        ]}
duanledexianxianxian's avatar
duanledexianxianxian committed
117
        footerRender={false} // 不显瀺footer
duanledexianxianxian's avatar
duanledexianxianxian committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        menuDataRender={menuDataRender}
        formatMessage={formatMessage}
        rightContentRender={rightProps => <RightContent {...rightProps} />}
        {...props}
        {...settings}
      >
        {children}
      </ProLayout>
      <SettingDrawer
        settings={settings}
        onSettingChange={config =>
          dispatch({
            type: 'settings/changeSetting',
            payload: config,
          })
        }
      />
duanledexianxianxian's avatar
duanledexianxianxian committed
135
    </div>
136 137 138
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
139
export default connect(({ global, settings }: ConnectState) => ({
140
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
141
  settings,
142
}))(BasicLayout);