Skip to content

Commit

Permalink
fix: devtools
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Oct 21, 2022
1 parent 98bc989 commit 9e8f005
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 56 deletions.
27 changes: 13 additions & 14 deletions packages/react-router-devtools/src/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Entry = {
}

type RendererProps = {
HandleEntry: HandleEntryComponent
handleEntry: HandleEntryFn
label?: React.ReactNode
value: unknown
subEntries: Entry[]
Expand Down Expand Up @@ -102,7 +102,7 @@ export function chunkArray<T>(array: T[], size: number): T[][] {
type Renderer = (props: RendererProps) => JSX.Element

export const DefaultRenderer: Renderer = ({
HandleEntry,
handleEntry,
label,
value,
subEntries = [],
Expand Down Expand Up @@ -134,9 +134,7 @@ export const DefaultRenderer: Renderer = ({
{expanded ? (
subEntryPages.length === 1 ? (
<SubEntries>
{subEntries.map((entry) => (
<HandleEntry key={entry.label} entry={entry} />
))}
{subEntries.map((entry, index) => handleEntry(entry))}
</SubEntries>
) : (
<SubEntries>
Expand All @@ -157,9 +155,7 @@ export const DefaultRenderer: Renderer = ({
</LabelButton>
{expandedPages.includes(index) ? (
<SubEntries>
{entries.map((entry) => (
<HandleEntry key={entry.label} entry={entry} />
))}
{entries.map((entry) => handleEntry(entry))}
</SubEntries>
) : null}
</Entry>
Expand Down Expand Up @@ -198,7 +194,7 @@ export const DefaultRenderer: Renderer = ({
)
}

type HandleEntryComponent = (props: { entry: Entry }) => JSX.Element
type HandleEntryFn = (entry: Entry) => JSX.Element

type ExplorerProps = Partial<RendererProps> & {
renderer?: Renderer
Expand Down Expand Up @@ -273,11 +269,14 @@ export default function Explorer({
const subEntryPages = chunkArray(subEntries, pageSize)

return renderer({
HandleEntry: React.useCallback(
({ entry }) => (
<Explorer value={value} renderer={renderer} {...rest} {...entry} />
),
[value, renderer],
handleEntry: (entry) => (
<Explorer
key={entry.label}
value={value}
renderer={renderer}
{...rest}
{...entry}
/>
),
type,
subEntries,
Expand Down
95 changes: 53 additions & 42 deletions packages/react-router-devtools/src/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,40 @@ export const TanStackRouterDevtoolsPanel = React.forwardRef<
} = props

const router = useRouter() as Router
const routerExplorerValue = React.useMemo(() => {
const {
listeners,
buildLocation,
mount,
update,
buildNext,
navigate,
cancelMatches,
loadLocation,
cleanPreloadCache,
loadRoute,
matchRoutes,
loadMatches,
invalidateRoute,
resolvePath,
matchRoute,
buildLink,
__experimental__createSnapshot,
destroy,
...rest
} = router

return rest
}, [router.state])

const rerender = React.useReducer(() => ({}), {})[1]

React.useEffect(() => {
let interval = setInterval(() => {
router.cleanPreloadCache()
router.notify()
}, 1000)
// router.notify()
rerender()
}, 250)

return () => {
clearInterval(interval)
Expand All @@ -386,6 +414,27 @@ export const TanStackRouterDevtoolsPanel = React.forwardRef<
(d) => d.routeId === activeRouteId,
)

const activeMatchExplorerValue = React.useMemo(() => {
if (!activeMatch) {
return {}
}

const {
cancel,
load,
router,
Link,
MatchRoute,
buildLink,
linkProps,
matchRoute,
navigate,
...rest
} = activeMatch

return rest
}, [activeMatch])

return (
<ThemeProvider theme={theme}>
<Panel ref={ref} className="TanStackRouterDevtoolsPanel" {...panelProps}>
Expand Down Expand Up @@ -505,30 +554,7 @@ export const TanStackRouterDevtoolsPanel = React.forwardRef<
>
<Explorer
label="Router"
value={(() => {
const {
listeners,
buildLocation,
mount,
update,
buildNext,
navigate,
cancelMatches,
loadLocation,
cleanPreloadCache,
loadRoute,
matchRoutes,
loadMatches,
invalidateRoute,
resolvePath,
matchRoute,
buildLink,
__experimental__createSnapshot,
destroy,
...rest
} = router
return rest
})()}
value={routerExplorerValue}
defaultExpanded={{}}
/>
</div>
Expand Down Expand Up @@ -860,22 +886,7 @@ export const TanStackRouterDevtoolsPanel = React.forwardRef<
>
<Explorer
label="Match"
value={(() => {
const {
cancel,
load,
router,
Link,
MatchRoute,
buildLink,
linkProps,
matchRoute,
navigate,
...rest
} = activeMatch

return rest
})()}
value={activeMatchExplorerValue}
defaultExpanded={{}}
/>
</div>
Expand Down

0 comments on commit 9e8f005

Please sign in to comment.