File tree 2 files changed +43
-5
lines changed
2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change 1
- import { DataRouteMatch } from "react-router-dom" ;
2
- import { RouterTabModel } from "src/lib/tabs/useRouterTabs.tsx" ;
1
+ import { Params } from "react-router-dom" ;
3
2
4
3
export type TabHandle = {
5
- key : any ;
6
- title : ( match : DataRouteMatch ) => string ;
7
- insertAt ?: ( models : RouterTabModel [ ] ) => number ;
4
+ key : string ;
5
+ title : ( props : { params : Params } ) => string ;
8
6
} ;
9
7
10
8
export type Handle = {
Original file line number Diff line number Diff line change
1
+ import { DataRouteMatch , RouteObject , UIMatch } from "react-router-dom" ;
2
+ import { Handle , TabHandle } from "./types" ;
3
+ import { flattenRoutes } from "src/lib/tabs" ;
4
+ import { InsertMethod , TabConfig } from "src/lib/tabs/useRouterTabs.tsx" ;
5
+
6
+ export const convertRouteTreeToConfig = ( tree : RouteObject [ ] , key : string ) => {
7
+ const flatRoutes = flattenRoutes ( tree ) ;
8
+
9
+ const matchedRoutes = flatRoutes . filter ( ( route ) => {
10
+ return ( route . handle as Handle ) ?. tabs . find ( ( tab ) => tab . key === key ) ;
11
+ } ) ;
12
+
13
+ const config : TabConfig < any > [ ] = matchedRoutes . map ( ( route ) => {
14
+ const handle = route . handle as Handle ;
15
+ const tabMeta = handle . tabs . find ( ( tab ) => ( tab . key = key ) ) ;
16
+
17
+ return {
18
+ title : tabMeta ! . title ,
19
+ routeId : route . id ! ,
20
+ insertMethod : InsertMethod . Prepend ,
21
+ } ;
22
+ } ) ;
23
+ return config ;
24
+ } ;
25
+
26
+ export const getTabHandle =
27
+ ( key : string ) =>
28
+ ( match : DataRouteMatch ) : TabHandle | undefined => {
29
+ return ( match . route ?. handle as Handle | undefined ) ?. tabs . find (
30
+ ( tabHandle : TabHandle ) => tabHandle . key === key ,
31
+ ) ;
32
+ } ;
33
+
34
+ export const getTabHandleUI =
35
+ ( key : string ) =>
36
+ ( match : UIMatch < any , Handle > ) : TabHandle | undefined => {
37
+ return match ?. handle ?. tabs . find (
38
+ ( tabHandle : TabHandle ) => tabHandle . key === key ,
39
+ ) ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments