Skip to content

Commit

Permalink
dev(test-studio): add router debugging tool (#5170)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Nov 10, 2023
1 parent 6d5b4e8 commit 94fef50
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
90 changes: 90 additions & 0 deletions dev/test-studio/plugins/router-debug/RouterDebug.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from 'react'
import {Button, Card, Code, Flex, Stack, Text} from '@sanity/ui'
import {IntentLink, RouteScope, StateLink, useRouter, useStateLink} from 'sanity/router'

export function RouterDebug() {
const {navigate} = useRouter()

const link = useStateLink({
state: {
section: 'abc',
_searchParams: [['viewParam', 'from link']],
},
})

return (
<Card sizing="border" padding={5}>
<Flex>
<Stack space={4}>
<StateLink state={{}}>Tool home</StateLink>
<StateLink
state={{
section: 'abc',
_searchParams: [
['someSearchParam', 'yes'],
['emojis', '🤩🤷🎉🫧'],
['🧭', '🔜'],
],
}}
>
Go to section "abc", with search params
</StateLink>

<IntentLink
intent="router-debug-please"
params={{
//@ts-expect-error - todo: we should probably allow arbitrary params
favorite: 'capybara',
}}
>
Resolve intent
</IntentLink>
<Button
onClick={() => {
navigate({
section: 'buttons',
_searchParams: [['from-button', 'true']],
})
}}
mode="ghost"
text="A button navigating w/search param"
/>
<a {...link}>A regular link</a>

<Card shadow={1} padding={3} radius={2}>
<RouteScope scope="some-plugin">
<Stack space={3}>
<Text weight="semibold">A (scoped) plugin</Text>

<StateLink
state={{
pluginParam: 'hello-from-plugin',
_searchParams: [['somePluginParam', 'hi!']],
}}
>
Click to navigate to a plugin param
</StateLink>
<InspectRouterState />
</Stack>
</RouteScope>
</Card>
<Card shadow={1} padding={3} radius={2}>
<InspectRouterState />
</Card>
</Stack>
</Flex>
</Card>
)
}

function InspectRouterState() {
const {state} = useRouter()
return (
<Stack space={3}>
<Text weight="semibold">Decoded router state</Text>
<Code language="json" size={1}>
{JSON.stringify(state, null, 2)}
</Code>
</Stack>
)
}
1 change: 1 addition & 0 deletions dev/test-studio/plugins/router-debug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './plugin'
40 changes: 40 additions & 0 deletions dev/test-studio/plugins/router-debug/plugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {definePlugin} from 'sanity'
import {PinIcon} from '@sanity/icons'
import {route} from 'sanity/router'
import {RouterDebugConfig} from './types'
import {RouterDebug} from './RouterDebug'

/**
* Router playground/debug plugin
*/
export const routerDebugTool = definePlugin<RouterDebugConfig | void>((options) => {
const {name, title, icon} = options || {}

return {
name: 'router-debug',
tools: [
{
name: name || 'router-debug',
title: title || 'Router debug',
icon: icon || PinIcon,
component: RouterDebug,
canHandleIntent: (intent, params) => {
return intent === 'router-debug-please'
},
getIntentState: (intent, params) => {
return {
section: 'from-intent',
_searchParams: [
['intentResolved', 'yes'],
['paramFromIntent', params.favorite],
],
}
},
router: route.create('/', [
route.create('/section/:section'),
route.scope('some-plugin', '/', [route.create('/', route.create('/:pluginParam'))]),
]),
},
],
}
})
7 changes: 7 additions & 0 deletions dev/test-studio/plugins/router-debug/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type {ComponentType} from 'react'

export interface RouterDebugConfig {
name?: string
title?: string
icon?: ComponentType
}
3 changes: 3 additions & 0 deletions dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {copyAction} from './fieldActions/copyAction'
import {assistFieldActionGroup} from './fieldActions/assistFieldActionGroup'
import {customInspector} from './inspectors/custom'
import {pasteAction} from './fieldActions/pasteAction'
import routerDebug from './plugins/router-debug/RouterDebug'
import {routerDebugTool} from './plugins/router-debug'

const sharedSettings = definePlugin({
name: 'sharedSettings',
Expand Down Expand Up @@ -121,6 +123,7 @@ const sharedSettings = definePlugin({
// eslint-disable-next-line camelcase
muxInput({mp4_support: 'standard'}),
presenceTool(),
routerDebugTool(),
tsdoc(),
],
})
Expand Down

2 comments on commit 94fef50

@vercel
Copy link

@vercel vercel bot commented on 94fef50 Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio-git-next.sanity.build
performance-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 94fef50 Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.