Skip to content

Commit

Permalink
Restore params and remove redundant code (I think)
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed Feb 28, 2023
1 parent ec28d6c commit e086fbd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 71 deletions.
33 changes: 5 additions & 28 deletions packages/router/src/active-route-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React, { Suspense, useEffect, useRef, useState } from 'react'
import React, { Suspense, useEffect, useRef } from 'react'

import { getAnnouncement, getFocus, resetFocus } from './a11yUtils'
import {
ActivePageContextProvider,
LoadingStateRecord,
} from './ActivePageContext'
import { useLocation } from './location'
import { inIframe, Spec } from './util'

interface Props {
Expand All @@ -18,17 +13,12 @@ interface Props {
}

export const ActiveRouteLoader = ({
path,
spec,
params,
whileLoadingPage,
}: Props) => {
const location = useLocation()
const announcementRef = useRef<HTMLDivElement>(null)
const LazyRouteComponent = spec.LazyComponent
const [loadingState, setLoadingState] = useState<LoadingStateRecord>({
[path]: { specName: '', state: 'PRE_SHOW', location },
})

useEffect(() => {
// Make this hook a no-op if we're rendering in an iframe.
Expand All @@ -50,25 +40,13 @@ export const ActiveRouteLoader = ({
}
}, [spec, params])

// @MARK keep this useffect to see if we can get the params to work
useEffect(() => {
setLoadingState((loadingState: LoadingStateRecord) => ({
...loadingState,
[path]: {
specName: spec.name,
state: 'DONE',
location,
},
}))
}, [spec, path, location])

// @TODO whileLoadingPage is undefined, why?
return (
<Suspense fallback={whileLoadingPage?.()}>
<ActivePageContextProvider value={{ loadingState }}>
<LazyRouteComponent {...params} />
{/* @TODO adding announcer causes hydration warnings */}
{/* <div
<LazyRouteComponent {...params} />
{/* @TODO why do we need activePageContext in InternalRoute??? */}
{/* @TODO adding announcer causes hydration warnings */}
{/* <div
id="redwood-announcer"
style={{
position: 'absolute',
Expand All @@ -86,7 +64,6 @@ export const ActiveRouteLoader = ({
aria-atomic="true"
ref={announcementRef}
></div> */}
</ActivePageContextProvider>
</Suspense>
)
}
33 changes: 4 additions & 29 deletions packages/router/src/params.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useContext } from 'react'

import { LocationContextType, useLocation } from './location'
import { useRouterState } from './router-context'
import { createNamedContext, matchPath, parseSearch } from './util'
import { createNamedContext } from './util'

export interface ParamsContextProps {
params: Record<string, string>
Expand All @@ -11,39 +9,16 @@ export interface ParamsContextProps {
export const ParamsContext = createNamedContext<ParamsContextProps>('Params')

interface Props {
path?: string
location?: LocationContextType
allParams?: Record<any, any>
children?: React.ReactNode
}

export const ParamsProvider: React.FC<Props> = ({
path,
location,
children,
}) => {
const { paramTypes } = useRouterState()
const contextLocation = useLocation()
const internalLocation = location || contextLocation

let pathParams = {}
const searchParams = parseSearch(internalLocation.search)

if (path) {
const { match, params } = matchPath(path, internalLocation.pathname, {
paramTypes,
})

if (match && typeof params !== 'undefined') {
pathParams = params
}
}

export const ParamsProvider: React.FC<Props> = ({ allParams, children }) => {
return (
<ParamsContext.Provider
value={{
params: {
...searchParams,
...pathParams,
...allParams,
},
}}
>
Expand Down
33 changes: 19 additions & 14 deletions packages/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function Route(props: RouteProps | RedirectRouteProps | NotFoundRouteProps) {
return <InternalRoute {...props} />
}

// @MARK Why do we need this???
// The actual route gets loaded by active-route-loader, and not render by this component
// is this component only used to validate?
// The matchPath, parseSearch, validatePath functions are used already in the Router, why do
// repeat them here? (see analyzeRouterPath and Router component)
const InternalRoute = ({
path,
page,
Expand Down Expand Up @@ -256,18 +261,20 @@ const LocationAwareRouter: React.FC<RouterProps> = ({
// Level 2/3 (LocationAwareRouter)
return (
<RouterContextProvider useAuth={useAuth} paramTypes={paramTypes}>
{redirect && <Redirect to={replaceParams(redirect, allParams)} />}
{!redirect && page && (
<ActiveRouteLoader
path={path}
spec={normalizePage(page)}
delay={pageLoadingDelay}
params={allParams}
whileLoadingPage={whileLoadingPage}
>
{root}
</ActiveRouteLoader>
)}
<ParamsProvider allParams={allParams}>
{redirect && <Redirect to={replaceParams(redirect, allParams)} />}
{!redirect && page && (
<ActiveRouteLoader
path={path}
spec={normalizePage(page)}
delay={pageLoadingDelay}
params={allParams}
whileLoadingPage={whileLoadingPage}
>
{root}
</ActiveRouteLoader>
)}
</ParamsProvider>
</RouterContextProvider>
)
}
Expand Down Expand Up @@ -317,8 +324,6 @@ function analyzeRouterTree(
return previousValue
}

console.log(`👉 \n ~ file: router.tsx:358 ~ child:`, child)

if (isRoute(child)) {
if (child.props.notfound && child.props.page) {
NotFoundPage = child.props.page
Expand Down

0 comments on commit e086fbd

Please sign in to comment.