From 92e2933ff1d038a9728ccd3804ffe393b8f90d4c Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 8 Nov 2024 19:59:05 +0200 Subject: [PATCH] resolve conflicts --- src/examples/basic/types.ts | 8 +++----- src/examples/basic/utils.ts | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/examples/basic/utils.ts diff --git a/src/examples/basic/types.ts b/src/examples/basic/types.ts index c38faf5..f3fad33 100644 --- a/src/examples/basic/types.ts +++ b/src/examples/basic/types.ts @@ -1,10 +1,8 @@ -import { DataRouteMatch } from "react-router-dom"; -import { RouterTabModel } from "src/lib/tabs/useRouterTabs.tsx"; +import { Params } from "react-router-dom"; export type TabHandle = { - key: any; - title: (match: DataRouteMatch) => string; - insertAt?: (models: RouterTabModel[]) => number; + key: string; + title: (props: { params: Params }) => string; }; export type Handle = { diff --git a/src/examples/basic/utils.ts b/src/examples/basic/utils.ts new file mode 100644 index 0000000..531a4b5 --- /dev/null +++ b/src/examples/basic/utils.ts @@ -0,0 +1,40 @@ +import { DataRouteMatch, RouteObject, UIMatch } from "react-router-dom"; +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[] = 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): TabHandle | undefined => { + return match?.handle?.tabs.find( + (tabHandle: TabHandle) => tabHandle.key === key, + ); + };