summaryrefslogtreecommitdiff
path: root/frontend/src/router-hook.tsx
diff options
context:
space:
mode:
authorAAGaming <aa@mail.catvibers.me>2022-08-02 18:54:55 -0400
committerAAGaming <aa@mail.catvibers.me>2022-08-02 18:54:55 -0400
commitab6ec981604a32611d972ede634abe7ccd19b0d2 (patch)
tree49f04b47f959cf48703a9b36fecc1d183d98cf7a /frontend/src/router-hook.tsx
parentf1e809781ad60cf3f6cddf0a0b94273b61bf1999 (diff)
downloaddecky-loader-ab6ec981604a32611d972ede634abe7ccd19b0d2.tar.gz
decky-loader-ab6ec981604a32611d972ede634abe7ccd19b0d2.zip
API for patching existing routes, lower power usev2.0.4-ab6ec98-pre
Diffstat (limited to 'frontend/src/router-hook.tsx')
-rw-r--r--frontend/src/router-hook.tsx50
1 files changed, 37 insertions, 13 deletions
diff --git a/frontend/src/router-hook.tsx b/frontend/src/router-hook.tsx
index 4e23658e..ca2a7039 100644
--- a/frontend/src/router-hook.tsx
+++ b/frontend/src/router-hook.tsx
@@ -1,10 +1,11 @@
import { afterPatch, findModuleChild, unpatch } from 'decky-frontend-lib';
import { ReactElement, createElement, memo } from 'react';
-import type { Route } from 'react-router';
+import type { Route, RouteProps } from 'react-router';
import {
DeckyRouterState,
DeckyRouterStateContextProvider,
+ RoutePatch,
RouterEntry,
useDeckyRouterState,
} from './components/DeckyRouterState';
@@ -38,19 +39,16 @@ class RouterHook extends Logger {
});
let Route: new () => Route;
+ // Used to store the new replicated routes we create to allow routes to be unpatched.
+ let toReplace = new Map<string, Route>();
const DeckyWrapper = ({ children }: { children: ReactElement }) => {
- const { routes } = useDeckyRouterState();
+ const { routes, routePatches } = useDeckyRouterState();
- let routerIndex = children.props.children[0].props.children.length;
- if (
- !children.props.children[0].props.children[routerIndex - 1]?.length ||
- children.props.children[0].props.children[routerIndex - 1]?.length !== routes.size
- ) {
- if (
- children.props.children[0].props.children[routerIndex - 1]?.length &&
- children.props.children[0].props.children[routerIndex - 1].length !== routes.size
- )
- routerIndex--;
+ const routeList = children.props.children[0].props.children;
+
+ let routerIndex = routeList.length;
+ if (!routeList[routerIndex - 1]?.length || routeList[routerIndex - 1]?.length !== routes.size) {
+ if (routeList[routerIndex - 1]?.length && routeList[routerIndex - 1].length !== routes.size) routerIndex--;
const newRouterArray: ReactElement[] = [];
routes.forEach(({ component, props }, path) => {
newRouterArray.push(
@@ -59,8 +57,26 @@ class RouterHook extends Logger {
</Route>,
);
});
- children.props.children[0].props.children[routerIndex] = newRouterArray;
+ routeList[routerIndex] = newRouterArray;
}
+ routeList.forEach((route: Route, index: number) => {
+ const replaced = toReplace.get(route?.props?.path as string);
+ if (replaced) {
+ routeList[index] = replaced;
+ toReplace.delete(route?.props?.path as string);
+ }
+ if (route?.props?.path && routePatches.has(route.props.path as string)) {
+ toReplace.set(
+ route?.props?.path as string,
+ // @ts-ignore
+ createElement(routeList[index].type, routeList[index].props, routeList[index].props.children),
+ );
+ routePatches.get(route.props.path as string)?.forEach((patch) => {
+ routeList[index].props = patch(routeList[index].props);
+ });
+ }
+ });
+ this.debug('Rerendered routes list');
return children;
};
@@ -97,6 +113,14 @@ class RouterHook extends Logger {
this.routerState.addRoute(path, component, props);
}
+ addPatch(path: string, patch: RoutePatch) {
+ return this.routerState.addPatch(path, patch);
+ }
+
+ removePatch(path: string, patch: RoutePatch) {
+ this.routerState.removePatch(path, patch);
+ }
+
removeRoute(path: string) {
this.routerState.removeRoute(path);
}