Fix route unpatching

This commit is contained in:
AAGaming
2022-08-11 20:34:55 -04:00
parent 47e0661773
commit 114c54c9b0

View File

@@ -1,5 +1,5 @@
import { afterPatch, findModuleChild, unpatch } from 'decky-frontend-lib';
import { ReactElement, createElement, memo } from 'react';
import React, { ReactElement, cloneElement, createElement, memo } from 'react';
import type { Route, RouteProps } from 'react-router';
import {
@@ -40,7 +40,7 @@ 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>();
let toReplace = new Map<string, ReactNode>();
const DeckyWrapper = ({ children }: { children: ReactElement }) => {
const { routes, routePatches } = useDeckyRouterState();
@@ -60,19 +60,27 @@ class RouterHook extends Logger {
routeList[routerIndex] = newRouterArray;
}
routeList.forEach((route: Route, index: number) => {
console.log(route);
const replaced = toReplace.get(route?.props?.path as string);
if (replaced) {
routeList[index] = replaced;
routeList[index].props.children = 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),
routeList[index].props.children,
);
routePatches.get(route.props.path as string)?.forEach((patch) => {
routeList[index].props = patch(routeList[index].props);
const oType = routeList[index].props.children.type;
routeList[index].props.children = patch({
...routeList[index].props,
children: {
...cloneElement(routeList[index].props.children),
type: (props) => createElement(oType, props),
},
}).children;
});
}
});