import { getRouteSegments, hasKeys, hasMissingPathParams, interpolatePath, rootRouteId, trimPath, } from '@tanstack/router-core' import { For, Match, Show, Switch, createEffect, createMemo, createSignal, onCleanup, untrack, } from 'solid-js' import { useDevtoolsOnClose } from './context' import { attachStyles } from './styles' import useLocalStorage from './useLocalStorage' import { Explorer } from './Explorer' import { getRouteStatusColor, getStatusColor, multiSortBy } from './utils' import { AgeTicker } from './AgeTicker' // import type { DevtoolsPanelOptions } from './TanStackRouterDevtoolsPanel' import { NavigateButton } from './NavigateButton' import type { AnyContext, AnyRoute, AnyRouteMatch, AnyRouter, FileRouteTypes, MakeRouteMatchUnion, Route, RouterState, } from '@tanstack/router-core' import type { Accessor } from 'solid-js' import type { JSX } from '@solidjs/web' export interface BaseDevtoolsPanelOptions { /** * The standard React style object used to style a component with inline styles */ style?: Accessor /** * The standard React class property used to style a component with classes */ className?: Accessor /** * A boolean variable indicating whether the panel is open or closed */ isOpen?: boolean /** * A function that toggles the open and close state of the panel */ setIsOpen?: (isOpen: boolean) => void /** * Handles the opening and closing the devtools panel */ handleDragStart?: (e: any) => void /** * A boolean variable indicating if the "lite" version of the library is being used */ router: Accessor routerState: Accessor /** * The element the devtools are mounted in. Its document or shadow root gets * the devtools styles. */ mountElement?: Element /** * Adds the devtools styles to this shadow root instead. */ shadowDOMTarget?: ShadowRoot } const HISTORY_LIMIT = 15 function Logo(props: any) { return ( ) } function NavigateLink(props: { class?: string left?: JSX.Element children?: JSX.Element right?: JSX.Element }) { return (
{props.left}
{props.children}
{props.right}
) } function RouteComp({ routerState, pendingMatches, router, route, isRoot, activeId, setActiveId, }: { routerState: Accessor< RouterState< Route< any, any, any, '/', '/', string, '__root__', undefined, {}, {}, AnyContext, AnyContext, {}, undefined, any, FileRouteTypes, unknown, undefined >, MakeRouteMatchUnion > > pendingMatches: Accessor> router: Accessor route: AnyRoute isRoot?: boolean activeId: Accessor setActiveId: (id: string) => void }) { const matches = createMemo(() => pendingMatches().length ? pendingMatches() : routerState().matches, ) const match = createMemo(() => routerState().matches.find((d) => d.routeId === route.id), ) const param = createMemo(() => { try { if (match()?.params) { const p = match()?.params const r: string = route.path || trimPath(route.id) if (r.startsWith('$')) { const trimmed = r.slice(1) if (p[trimmed]) { return `(${p[trimmed]})` } } } return '' } catch (error) { return '' } }) const navigationTarget = createMemo(() => { if (isRoot) return undefined // rootRouteId has no path if (!route.path) return undefined // no path to navigate to // flatten all params in the router state, into a single object const allParams = Object.assign({}, ...matches().map((m) => m.params)) const segments = getRouteSegments(route) const pathname = segments ? interpolatePath( route.fullPath, segments, allParams, router().pathParamsDecoder, ) : route.fullPath return segments && hasMissingPathParams(segments, allParams) ? undefined : pathname }) return (
{ if (match()) { setActiveId(activeId() === route.id ? '' : route.id) } }} class={`tsrd-routes-row-container${match() ? ' tsrd-routes-row-container-match' : ''}${route.id === activeId() ? ' tsrd-routes-row-container-active' : ''}`} >
{(navigate) => } } right={} > {isRoot ? rootRouteId : route.path || trimPath(route.id)}{' '} {param()}
{route.children?.length ? (
{[...(route.children as Array)] .sort((a, b) => { return a.rank - b.rank }) .map((r) => ( ))}
) : null}
) } export const BaseTanStackRouterDevtoolsPanel = function BaseTanStackRouterDevtoolsPanel({ ...props }: BaseDevtoolsPanelOptions): JSX.Element { const { isOpen = true, setIsOpen, handleDragStart, router, routerState, mountElement, shadowDOMTarget, ...panelProps } = props if (mountElement) { attachStyles(mountElement, shadowDOMTarget) } const { onCloseClick } = useDevtoolsOnClose() const { className, style, ...otherPanelProps } = panelProps // useStore(router.stores.__store) const [currentTab, setCurrentTab] = useLocalStorage< 'routes' | 'matches' | 'history' >('tanstackRouterDevtoolsActiveTab', 'routes') const [activeId, setActiveId] = useLocalStorage( 'tanstackRouterDevtoolsActiveRouteId', '', ) const [history, setHistory] = createSignal>([]) const [hasHistoryOverflowed, setHasHistoryOverflowed] = createSignal(false) const pendingMatches = () => { const matches = routerState().matches return matches.some((match: AnyRouteMatch) => match.status === 'pending') ? matches : [] } let cache = untrack(router)._cache let cacheMatches = [...cache.values()] const [cachedMatches, setCachedMatches] = createSignal(cacheMatches) createEffect(router, (currentRouter) => { const refreshCache = () => { const next = currentRouter._cache let valuesChanged = next.size !== cacheMatches.length if (!valuesChanged) { let index = 0 for (const match of next.values()) { if (match !== cacheMatches[index++]) { valuesChanged = true break } } } if (next !== cache || valuesChanged) { cache = next cacheMatches = [...next.values()] setCachedMatches(cacheMatches) } } refreshCache() const interval = setInterval(refreshCache, 500) return () => clearInterval(interval) }) createEffect( () => routerState().matches, (matches) => { const currentMatch = matches[matches.length - 1] if (!currentMatch) { return } const historyUntracked = untrack(history) const lastMatch = historyUntracked[0] const sameLocation = lastMatch && lastMatch.pathname === currentMatch.pathname && JSON.stringify(lastMatch.search ?? {}) === JSON.stringify(currentMatch.search ?? {}) if (!lastMatch || !sameLocation) { if (historyUntracked.length >= HISTORY_LIMIT) { setHasHistoryOverflowed(true) } setHistory((prev) => { const newHistory = [currentMatch, ...prev] // truncate to ensure we don't overflow too much the ui newHistory.splice(HISTORY_LIMIT) return newHistory }) } }, ) const activeMatch = createMemo(() => { const matches = [ ...pendingMatches(), ...routerState().matches, ...cachedMatches(), ] return matches.find( (d) => d.routeId === activeId() || d.id === activeId(), ) }) const hasSearch = createMemo(() => hasKeys(routerState().location.search)) const explorerState = createMemo(() => { return { ...router(), state: routerState(), } }) const routerExplorerValue = createMemo(() => Object.fromEntries( multiSortBy( Object.keys(explorerState()), ( [ 'state', 'routesById', 'routesByPath', 'options', 'manifest', ] as const ).map((d) => (dd) => dd !== d), ) .map((key) => [key, (explorerState() as any)[key]]) .filter( (d) => typeof d[1] !== 'function' && ![ 'stores', 'basepath', 'subscribers', '_scroll', 'tempLocationKey', 'latestLocation', 'routeTree', 'history', ].includes(d[0]), ), ), ) const activeMatchLoaderData = createMemo(() => activeMatch()?.loaderData) const activeMatchValue = createMemo(() => activeMatch()) const locationSearchValue = createMemo(() => routerState().location.search) return (
{handleDragStart ? (
) : null}
{ if (setIsOpen) { setIsOpen(false) } onCloseClick(e) }} />
{ return subEntries.filter( (d: any) => typeof d.value() !== 'function', ) }} />
Pathname {routerState().location.maskedLocation ? (
masked
) : null}
{routerState().location.pathname} {routerState().location.maskedLocation ? ( {routerState().location.maskedLocation?.pathname} ) : null}
age / staleTime / gcTime
{(routeTree) => ( )}
{(pendingMatches().length ? pendingMatches() : routerState().matches ).map((match: any, _i: any) => { return (
setActiveId(activeId() === match.id ? '' : match.id) } class={ match === activeMatch() ? 'tsrd-match-row tsrd-match-row-active' : 'tsrd-match-row' } >
} right={} > {`${match.routeId === rootRouteId ? rootRouteId : match.pathname}`}
) })}
    {(match, index) => (
  • } right={ } > {`${match.routeId === rootRouteId ? rootRouteId : match.pathname}`}
  • )}
    {hasHistoryOverflowed() ? (
  • This panel displays the most recent {HISTORY_LIMIT}{' '} navigations.
  • ) : null}
{cachedMatches().length ? (
Cached Matches
age / staleTime / gcTime
{cachedMatches().map((match: any) => { return (
setActiveId(activeId() === match.id ? '' : match.id) } class={ match === activeMatch() ? 'tsrd-match-row tsrd-match-row-active' : 'tsrd-match-row' } >
} right={} > {`${match.id}`}
) })}
) : null}
{activeMatch() && activeMatch()?.status ? (
Match Details
{activeMatch()?.status === 'success' && activeMatch()?.isFetching ? 'fetching' : activeMatch()?.status}
ID:
{activeMatch()?.id}
State:
{pendingMatches().find( (d: AnyRouteMatch) => d.id === activeMatch()?.id, ) ? 'Pending' : routerState().matches.find( (d: any) => d.id === activeMatch()?.id, ) ? 'Active' : 'Cached'}
Last Updated:
{activeMatch()?.updatedAt ? new Date(activeMatch()?.updatedAt).toLocaleTimeString() : 'N/A'}
{activeMatchLoaderData() ? ( <>
Loader Data
) : null}
Explorer
) : null} {hasSearch() ? (
Search Params {typeof navigator !== 'undefined' ? ( { const search = routerState().location.search return JSON.stringify(search) }} /> ) : null}
Object.keys(routerState().location.search).reduce( (obj: any, next) => { obj[next] = {} return obj }, {}, ), )} />
) : null}
) } function CopyButton({ getValue }: { getValue: () => string }) { const [copied, setCopied] = createSignal(false) let timeoutId: ReturnType | null = null const handleCopy = async () => { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (typeof navigator === 'undefined' || !navigator.clipboard?.writeText) { console.warn('TanStack Router Devtools: Clipboard API unavailable') return } try { const value = getValue() await navigator.clipboard.writeText(value) setCopied(true) if (timeoutId) clearTimeout(timeoutId) timeoutId = setTimeout(() => setCopied(false), 2500) } catch (e) { console.error('TanStack Router Devtools: Failed to copy', e) } } onCleanup(() => { if (timeoutId) clearTimeout(timeoutId) }) return ( ) } export default BaseTanStackRouterDevtoolsPanel