mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-07-11 17:02:00 +00:00
All props of route, expose routerHook
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user