1+ import type { MenuItem } from '../../config/menu' ;
2+ import type { PREV_ROUTE_KEY } from '../../config/other' ;
13import type { NotificationItem , UserState } from '../../config/state' ;
4+ import type { DMenuItem } from '@react-devui/ui/components/menu' ;
25
3- import { isNull } from 'lodash' ;
6+ import { isNull , isObject , isUndefined } from 'lodash' ;
7+ import React from 'react' ;
8+ import { useTranslation } from 'react-i18next' ;
9+ import { Link , useLocation , useNavigate } from 'react-router-dom' ;
410
11+ import { MENU } from '../../config/menu' ;
12+ import { useMenuState } from '../../config/state' ;
513import { useNotificationState } from '../../config/state' ;
614import { useUserState } from '../../config/state' ;
715import { TOKEN , TOKEN_REFRESH , TOKEN_REFRESH_OFFSET } from '../../config/token' ;
@@ -11,10 +19,16 @@ import { useHttp } from './useHttp';
1119let CLEAR_TOKEN_REFRESH : ( ( ) => void ) | undefined ;
1220
1321export function useInit ( ) {
22+ const { t } = useTranslation ( ) ;
23+ const navigate = useNavigate ( ) ;
24+ const location = useLocation ( ) ;
25+ const from = ( location . state as null | { [ PREV_ROUTE_KEY ] ?: Location } ) ?. from ?. pathname ;
1426 const createHttp = useHttp ( ) ;
27+ const acl = useACL ( ) ;
28+
1529 const [ , setUser ] = useUserState ( ) ;
30+ const [ , setMenu ] = useMenuState ( ) ;
1631 const [ , setNotification ] = useNotificationState ( ) ;
17- const acl = useACL ( ) ;
1832
1933 const refreshToken = ( ) => {
2034 CLEAR_TOKEN_REFRESH ?.( ) ;
@@ -49,6 +63,65 @@ export function useInit() {
4963 acl . setFull ( user . role === 'admin' ) ;
5064 acl . set ( [ ] ) ;
5165 //#endregion
66+
67+ //#region Menu
68+ const allItem : { id : string ; parentSub : string [ ] } [ ] = [ ] ;
69+ const reduceMenu = ( arr : MenuItem [ ] , parentSub : string [ ] = [ ] ) : DMenuItem < string > [ ] => {
70+ const newArr : DMenuItem < string > [ ] = [ ] ;
71+ for ( const item of arr ) {
72+ if ( item . acl ) {
73+ const params =
74+ isObject ( item . acl ) && 'control' in item . acl
75+ ? item . acl
76+ : {
77+ control : item . acl ,
78+ } ;
79+ if ( ! acl . can ( params . control , params . mode ) ) {
80+ continue ;
81+ }
82+ }
83+
84+ const title = item . title ?? t ( item . titleI18n ! ) ;
85+ const obj : DMenuItem < string > = {
86+ id : item . path ,
87+ title :
88+ item . type === 'item'
89+ ? React . createElement ( Link , { className : 'app-layout-sidebar__link' , tabIndex : - 1 , to : item . path } , title )
90+ : title ,
91+ icon : item . icon ? React . createElement ( item . icon ) : undefined ,
92+ type : item . type ,
93+ } ;
94+
95+ if ( item . children ) {
96+ obj . children = reduceMenu ( item . children , parentSub . concat ( item . type === 'sub' ? [ item . path ] : [ ] ) ) ;
97+ if ( obj . children . length > 0 ) {
98+ newArr . push ( obj ) ;
99+ }
100+ } else {
101+ newArr . push ( obj ) ;
102+ allItem . push ( { id : item . path , parentSub } ) ;
103+ }
104+ }
105+ return newArr ;
106+ } ;
107+
108+ const menu = reduceMenu ( MENU ) ;
109+ const toFirstItem = ( ) => {
110+ setMenu ( { menu, expands : allItem [ 0 ] . parentSub } ) ;
111+ navigate ( allItem [ 0 ] . id , { replace : true } ) ;
112+ } ;
113+ if ( isUndefined ( from ) ) {
114+ toFirstItem ( ) ;
115+ } else {
116+ const findIndex = allItem . findIndex ( ( item ) => item . id === from ) ;
117+ if ( findIndex === - 1 ) {
118+ toFirstItem ( ) ;
119+ } else {
120+ setMenu ( { menu, expands : allItem [ findIndex ] . parentSub } ) ;
121+ navigate ( from , { replace : true } ) ;
122+ }
123+ }
124+ //#endregion
52125 } ;
53126
54127 const getNotification = ( ) => {
0 commit comments