From 4e1d0b625210042e24935a24fba4fe836181d05c Mon Sep 17 00:00:00 2001 From: Valerio Ageno Date: Sat, 11 May 2024 16:22:15 +0200 Subject: [PATCH] feat: initialize router on landing --- .../tuono-router/src/components/Matches.tsx | 24 ++-------------- .../src/components/RouterProvider.tsx | 28 +++++++++++++------ .../tuono-router/src/hooks/useRouterStore.tsx | 2 +- packages/tuono-router/src/router.ts | 2 +- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/tuono-router/src/components/Matches.tsx b/packages/tuono-router/src/components/Matches.tsx index 11f2aa36..3002f7c4 100644 --- a/packages/tuono-router/src/components/Matches.tsx +++ b/packages/tuono-router/src/components/Matches.tsx @@ -3,29 +3,11 @@ import { useRouter } from '../hooks/useRouter' import { useRouterStore } from '../hooks/useRouterStore' import NotFound from './NotFound' -export function Matches(): JSX.Element | undefined { - const matchId = useRouterStore.getState().matches[0]?.id - - //if (!matchId) return <> - - return ( - - - - ) -} - -interface MatchProps { - id: string -} - -const Match = React.memo(function ({ id }: MatchProps) { +export const Matches = React.memo(function () { const location = useRouterStore((st) => st.location) const router = useRouter() - console.log(router, location) - - const route = router.routesById[location?.pathname] + const route = router.routesById[location?.pathname || ''] if (!route) { return @@ -35,5 +17,5 @@ const Match = React.memo(function ({ id }: MatchProps) { console.log('Has rust handler') } - return route.component() + return route.options.component() }) diff --git a/packages/tuono-router/src/components/RouterProvider.tsx b/packages/tuono-router/src/components/RouterProvider.tsx index 6cc3d16c..dd030d15 100644 --- a/packages/tuono-router/src/components/RouterProvider.tsx +++ b/packages/tuono-router/src/components/RouterProvider.tsx @@ -1,6 +1,7 @@ -import * as React from 'react' import { getRouterContext } from './RouterContext' import { Matches } from './Matches' +import { useRouterStore } from '../hooks/useRouterStore' +import React, { useLayoutEffect, type ReactNode } from 'react' type Router = any @@ -30,28 +31,37 @@ function RouterContextProvider({ ) : null - const provider = ( + return ( {children} ) - - // NOTE: verify usefulness - if (router.options.Wrap) { - return {provider} - } - - return provider } interface RouterProviderProps { router: Router } +const initRouterStore = (): void => { + const updateLocation = useRouterStore((st) => st.updateLocation) + console.log(window.location) + useLayoutEffect(() => { + const { pathname, hash, href, search } = window.location + updateLocation({ + pathname, + hash, + href, + searchStr: search, + search: new URLSearchParams(search), + }) + }, []) +} + export function RouterProvider({ router, ...rest }: RouterProviderProps): JSX.Element { + initRouterStore() return ( diff --git a/packages/tuono-router/src/hooks/useRouterStore.tsx b/packages/tuono-router/src/hooks/useRouterStore.tsx index 0c80cbda..e7af31a7 100644 --- a/packages/tuono-router/src/hooks/useRouterStore.tsx +++ b/packages/tuono-router/src/hooks/useRouterStore.tsx @@ -4,7 +4,7 @@ import { create } from 'zustand' export interface ParsedLocation { href: string pathname: string - search: Record + search: URLSearchParams searchStr: string hash: string } diff --git a/packages/tuono-router/src/router.ts b/packages/tuono-router/src/router.ts index 8ec2f5f1..2ee09bc1 100644 --- a/packages/tuono-router/src/router.ts +++ b/packages/tuono-router/src/router.ts @@ -11,7 +11,7 @@ interface CreateRouter { } interface RouteOptions { - component?: ReactNode + component?: () => ReactNode hasHandler?: boolean routeTree?: RouteTree }