-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev(test-studio): add router debugging tool (#5170)
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './plugin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))]), | ||
]), | ||
}, | ||
], | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94fef50
There was a problem hiding this comment.
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
94fef50
There was a problem hiding this comment.
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