BasicLayout.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
  3. *
  4. * @see You can view component api by: https://github.com/ant-design/ant-design-pro-layout
  5. */
  6. import type {
  7. MenuDataItem,
  8. BasicLayoutProps as ProLayoutProps,
  9. Settings,
  10. } from '@ant-design/pro-layout';
  11. import ProLayout, { DefaultFooter } from '@ant-design/pro-layout';
  12. import React, { useEffect, useMemo, useRef } from 'react';
  13. import type { Dispatch } from 'umi';
  14. import { Link, useIntl, connect, history } from 'umi';
  15. import { GithubOutlined } from '@ant-design/icons';
  16. import { Result, Button } from 'antd';
  17. import Authorized from '@/utils/Authorized';
  18. import RightContent from '@/components/GlobalHeader/RightContent';
  19. import type { ConnectState } from '@/models/connect';
  20. import { getMatchMenu } from '@umijs/route-utils';
  21. import logo from '../assets/logo.svg';
  22. const noMatch = (
  23. <Result
  24. status={403}
  25. title="403"
  26. subTitle="Sorry, you are not authorized to access this page."
  27. extra={
  28. <Button type="primary">
  29. <Link to="/user/login">Go Login</Link>
  30. </Button>
  31. }
  32. />
  33. );
  34. export type BasicLayoutProps = {
  35. breadcrumbNameMap: Record<string, MenuDataItem>;
  36. route: ProLayoutProps['route'] & {
  37. authority: string[];
  38. };
  39. settings: Settings;
  40. dispatch: Dispatch;
  41. } & ProLayoutProps;
  42. export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
  43. breadcrumbNameMap: Record<string, MenuDataItem>;
  44. };
  45. /** Use Authorized check all menu item */
  46. const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  47. menuList.map((item) => {
  48. const localItem = {
  49. ...item,
  50. children: item.children ? menuDataRender(item.children) : undefined,
  51. };
  52. return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  53. });
  54. const defaultFooterDom = (
  55. <DefaultFooter
  56. copyright={`${new Date().getFullYear()} Produced by Ant Group Experience Technology Department`}
  57. links={[
  58. {
  59. key: 'Ant Design Pro',
  60. title: 'Ant Design Pro',
  61. href: 'https://pro.ant.design',
  62. blankTarget: true,
  63. },
  64. {
  65. key: 'github',
  66. title: <GithubOutlined />,
  67. href: 'https://github.com/ant-design/ant-design-pro',
  68. blankTarget: true,
  69. },
  70. {
  71. key: 'Ant Design',
  72. title: 'Ant Design',
  73. href: 'https://ant.design',
  74. blankTarget: true,
  75. },
  76. ]}
  77. />
  78. );
  79. const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
  80. const {
  81. dispatch,
  82. children,
  83. settings,
  84. location = {
  85. pathname: '/',
  86. },
  87. } = props;
  88. const menuDataRef = useRef<MenuDataItem[]>([]);
  89. useEffect(() => {
  90. if (dispatch) {
  91. dispatch({
  92. type: 'user/fetchCurrent',
  93. });
  94. }
  95. }, []);
  96. /** Init variables */
  97. const handleMenuCollapse = (payload: boolean): void => {
  98. if (dispatch) {
  99. dispatch({
  100. type: 'global/changeLayoutCollapsed',
  101. payload,
  102. });
  103. }
  104. };
  105. // get children authority
  106. const authorized = useMemo(
  107. () =>
  108. getMatchMenu(location.pathname || '/', menuDataRef.current).pop() || {
  109. authority: undefined,
  110. },
  111. [location.pathname],
  112. );
  113. const { formatMessage } = useIntl();
  114. return (
  115. <ProLayout
  116. logo={logo}
  117. formatMessage={formatMessage}
  118. {...props}
  119. {...settings}
  120. onCollapse={handleMenuCollapse}
  121. onMenuHeaderClick={() => history.push('/')}
  122. menuItemRender={(menuItemProps, defaultDom) => {
  123. if (
  124. menuItemProps.isUrl ||
  125. !menuItemProps.path ||
  126. location.pathname === menuItemProps.path
  127. ) {
  128. return defaultDom;
  129. }
  130. return <Link to={menuItemProps.path}>{defaultDom}</Link>;
  131. }}
  132. breadcrumbRender={(routers = []) => [
  133. {
  134. path: '/',
  135. breadcrumbName: formatMessage({ id: 'menu.home' }),
  136. },
  137. ...routers,
  138. ]}
  139. itemRender={(route, params, routes, paths) => {
  140. const first = routes.indexOf(route) === 0;
  141. return first ? (
  142. <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
  143. ) : (
  144. <span>{route.breadcrumbName}</span>
  145. );
  146. }}
  147. footerRender={() => {
  148. if (settings.footerRender || settings.footerRender === undefined) {
  149. return defaultFooterDom;
  150. }
  151. return null;
  152. }}
  153. menuDataRender={menuDataRender}
  154. rightContentRender={() => <RightContent />}
  155. postMenuData={(menuData) => {
  156. menuDataRef.current = menuData || [];
  157. return menuData || [];
  158. }}
  159. waterMarkProps={{
  160. content: 'Ant Design Pro',
  161. fontColor: 'rgba(24,144,255,0.15)',
  162. }}
  163. >
  164. <Authorized authority={authorized!.authority} noMatch={noMatch}>
  165. {children}
  166. </Authorized>
  167. </ProLayout>
  168. );
  169. };
  170. export default connect(({ global, settings }: ConnectState) => ({
  171. collapsed: global.collapsed,
  172. settings,
  173. }))(BasicLayout);