From 114c54c9b05b5a7885c1939fb63aa006f879ba53 Mon Sep 17 00:00:00 2001
From: AAGaming <aa@mail.catvibers.me>
Date: Thu, 11 Aug 2022 20:34:55 -0400
Subject: Fix route unpatching

---
 frontend/src/router-hook.tsx | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

(limited to 'frontend/src/router-hook.tsx')

diff --git a/frontend/src/router-hook.tsx b/frontend/src/router-hook.tsx
index ca2a7039..e42307ee 100644
--- a/frontend/src/router-hook.tsx
+++ b/frontend/src/router-hook.tsx
@@ -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;
           });
         }
       });
-- 
cgit v1.2.3

