From 8fcaadd8f30182c61809748a324808add56ec1c0 Mon Sep 17 00:00:00 2001
From: Jonas Dellinger <jonas@dellinger.dev>
Date: Mon, 30 May 2022 20:55:51 +0200
Subject: All props of route, expose routerHook

---
 frontend/src/router-hook.tsx | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

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

diff --git a/frontend/src/router-hook.tsx b/frontend/src/router-hook.tsx
index ef2844b6..83d23bf1 100644
--- a/frontend/src/router-hook.tsx
+++ b/frontend/src/router-hook.tsx
@@ -1,7 +1,13 @@
 import { afterPatch, findModuleChild, unpatch } from 'decky-frontend-lib';
-import { FC, ReactElement, createElement } from 'react';
+import { ReactElement, createElement, memo } from 'react';
+import type { Route } from 'react-router';
 
-import { DeckyRouterState, DeckyRouterStateContextProvider, useDeckyRouterState } from './components/DeckyRouterState';
+import {
+  DeckyRouterState,
+  DeckyRouterStateContextProvider,
+  RouterEntry,
+  useDeckyRouterState,
+} from './components/DeckyRouterState';
 import Logger from './logger';
 
 declare global {
@@ -10,11 +16,6 @@ declare global {
   }
 }
 
-interface RouteProps {
-  path: string;
-  children: ReactElement;
-}
-
 class RouterHook extends Logger {
   private router: any;
   private memoizedRouter: any;
@@ -36,18 +37,22 @@ class RouterHook extends Logger {
       }
     });
 
-    let Route: FC<RouteProps>;
+    let Route: new () => Route;
     const DeckyWrapper = ({ children }: { children: ReactElement }) => {
       const { routes } = useDeckyRouterState();
-      
+
       const routerIndex = children.props.children[0].props.children.length - 1;
       if (
         !children.props.children[0].props.children[routerIndex].length ||
         children.props.children[0].props.children !== routes.size
       ) {
         const newRouterArray: ReactElement[] = [];
-        routes.forEach((Render, path) => {
-          newRouterArray.push(<Route path={path}>{createElement(Render)}</Route>);
+        routes.forEach(({ component, props }, path) => {
+          newRouterArray.push(
+            <Route path={path} {...props}>
+              {createElement(component)}
+            </Route>,
+          );
         });
         children.props.children[0].props.children[routerIndex] = newRouterArray;
       }
@@ -73,7 +78,7 @@ class RouterHook extends Logger {
               );
               return returnVal;
             });
-            this.memoizedRouter = window.SP_REACT.memo(this.router.type);
+            this.memoizedRouter = memo(this.router.type);
             this.memoizedRouter.isDeckyRouter = true;
           }
           ret.props.children.props.children[2].props.children[0].type = this.memoizedRouter;
@@ -83,6 +88,10 @@ class RouterHook extends Logger {
     });
   }
 
+  addRoute(path: string, component: RouterEntry['component'], props: RouterEntry['props'] = {}) {
+    this.routerState.addRoute(path, component, props);
+  }
+
   deinit() {
     unpatch(this.gamepadWrapper, 'render');
     this.router && unpatch(this.router, 'type');
-- 
cgit v1.2.3

