-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DataRouteMatch, RouteObject, UIMatch } from "react-router-dom"; | ||
Check failure on line 1 in src/examples/basic/utils.ts GitHub Actions / Build
Check failure on line 1 in src/examples/basic/utils.ts GitHub Actions / Build
Check failure on line 1 in src/examples/basic/utils.ts GitHub Actions / Build
Check failure on line 1 in src/examples/basic/utils.ts GitHub Actions / Build
|
||
import { Handle, TabHandle } from "./types"; | ||
import { flattenRoutes } from "src/lib/tabs"; | ||
import { InsertMethod, TabConfig } from "src/lib/tabs/useRouterTabs.tsx"; | ||
|
||
export const convertRouteTreeToConfig = (tree: RouteObject[], key: string) => { | ||
const flatRoutes = flattenRoutes(tree); | ||
|
||
const matchedRoutes = flatRoutes.filter((route) => { | ||
return (route.handle as Handle)?.tabs.find((tab) => tab.key === key); | ||
}); | ||
|
||
const config: TabConfig<any>[] = matchedRoutes.map((route) => { | ||
const handle = route.handle as Handle; | ||
const tabMeta = handle.tabs.find((tab) => (tab.key = key)); | ||
|
||
return { | ||
title: tabMeta!.title, | ||
routeId: route.id!, | ||
insertMethod: InsertMethod.Prepend, | ||
}; | ||
}); | ||
return config; | ||
}; | ||
|
||
export const getTabHandle = | ||
(key: string) => | ||
(match: DataRouteMatch): TabHandle | undefined => { | ||
return (match.route?.handle as Handle | undefined)?.tabs.find( | ||
(tabHandle: TabHandle) => tabHandle.key === key, | ||
); | ||
}; | ||
|
||
export const getTabHandleUI = | ||
(key: string) => | ||
(match: UIMatch<any, Handle>): TabHandle | undefined => { | ||
return match?.handle?.tabs.find( | ||
(tabHandle: TabHandle) => tabHandle.key === key, | ||
); | ||
}; |