1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/core/src/renderer/routes/matching-route.injectable.ts
Sebastian Malton e5c25f94a3 chore: Improve typing of front end routes to fix type errors
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-06-01 09:26:31 -04:00

45 lines
1.2 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import routesInjectable from "./routes.injectable";
import { computed } from "mobx";
import { matchPath } from "react-router";
import currentPathInjectable from "./current-path.injectable";
import type { InferParamFromPath, Route } from "../../common/front-end-routing/front-end-route-injection-token";
export interface MatchedRoute {
route: Route<string>;
pathParameters: InferParamFromPath<string>;
}
const matchingRouteInjectable = getInjectable({
id: "matching-route",
instantiate: (di) => {
const routes = di.inject(routesInjectable);
const currentPath = di.inject(currentPathInjectable);
return computed((): MatchedRoute | undefined => {
for (const route of routes.get()) {
const match = matchPath(currentPath.get(), {
path: route.path,
exact: true,
});
if (match) {
return {
route,
pathParameters: match.params,
};
}
}
return undefined;
});
},
});
export default matchingRouteInjectable;