Unverified Commit 356cdf23 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[devtool] display segment explorer as tree view (#80261)

parent b8b85c67
...@@ -62,6 +62,7 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) { ...@@ -62,6 +62,7 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) {
tree={tree} tree={tree}
node={tree.getRoot()} node={tree.getRoot()}
level={0} level={0}
segment=""
/> />
</div> </div>
) )
...@@ -69,10 +70,12 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) { ...@@ -69,10 +70,12 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) {
function PageSegmentTreeLayerPresentation({ function PageSegmentTreeLayerPresentation({
tree, tree,
segment,
node, node,
level, level,
}: { }: {
tree: Trie<SegmentNode> tree: Trie<SegmentNode>
segment: string
node: TrieNode<SegmentNode> node: TrieNode<SegmentNode>
level: number level: number
}) { }) {
...@@ -81,11 +84,9 @@ function PageSegmentTreeLayerPresentation({ ...@@ -81,11 +84,9 @@ function PageSegmentTreeLayerPresentation({
const segments = pagePath.split('/') || [] const segments = pagePath.split('/') || []
const fileName = segments.pop() || '' const fileName = segments.pop() || ''
const segmentPath = segments.join('/') const childrenKeys = Object.keys(node.children)
let pagePathPrefix = segmentPath const sortedChildrenKeys = childrenKeys.sort((a, b) => {
const childrenKeys = Object.keys(node.children).sort((a, b) => {
// Prioritize if it's a file convention like layout or page, // Prioritize if it's a file convention like layout or page,
// then the rest parallel routes. // then the rest parallel routes.
const aHasExt = a.includes('.') const aHasExt = a.includes('.')
...@@ -98,13 +99,11 @@ function PageSegmentTreeLayerPresentation({ ...@@ -98,13 +99,11 @@ function PageSegmentTreeLayerPresentation({
return ( return (
<div <div
className={cx( className="segment-explorer-item"
'segment-explorer-item', data-nextjs-devtool-segment-explorer-segment={segment}
level > 1 && 'segment-explorer-item--nested'
)}
> >
{!fileName || level === 0 ? null : ( {fileName ? (
<div className="segment-explorer-item-row"> <div className={cx('segment-explorer-item-row')}>
<div className="segment-explorer-line"> <div className="segment-explorer-line">
<div className={`segment-explorer-line-text-${nodeName}`}> <div className={`segment-explorer-line-text-${nodeName}`}>
<span <span
...@@ -115,20 +114,39 @@ function PageSegmentTreeLayerPresentation({ ...@@ -115,20 +114,39 @@ function PageSegmentTreeLayerPresentation({
> >
{nodeName === 'layout' ? ICONS.layout : ICONS.page} {nodeName === 'layout' ? ICONS.layout : ICONS.page}
</span> </span>
{pagePathPrefix === '' ? '' : `${pagePathPrefix}/`}
<span className="segment-explorer-filename-path">{fileName}</span> <span className="segment-explorer-filename-path">{fileName}</span>
</div> </div>
</div> </div>
</div> </div>
)} ) : segment ? (
<div className={'segment-explorer-item-row'}>
<div className="tree-node-expanded-rendered-children"> <div className="segment-explorer-line">
{childrenKeys.map((segment) => { <div className={`segment-explorer-line-text-${nodeName}`}>
const child = node.children[segment] <span
className={cx(
'segment-explorer-line-icon',
`segment-explorer-line-icon-${nodeName}`
)}
></span>
<span className="segment-explorer-filename-path">
{`${segment}/`}
</span>
</div>
</div>
</div>
) : null}
<div
className="segment-explorer-segment-children"
data-nextjs-devtool-segment-explorer-level={level}
>
{sortedChildrenKeys.map((childSegment) => {
const child = node.children[childSegment]
return ( return (
child && ( child && (
<PageSegmentTreeLayerPresentation <PageSegmentTreeLayerPresentation
key={segment} key={childSegment}
segment={childSegment}
tree={tree} tree={tree}
node={child} node={child}
level={level + 1} level={level + 1}
...@@ -163,10 +181,6 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css` ...@@ -163,10 +181,6 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css`
font-size: var(--size-14); font-size: var(--size-14);
} }
.segment-explorer-item--nested {
padding-left: 20px;
}
.segment-explorer-item-row { .segment-explorer-item-row {
display: flex; display: flex;
align-items: center; align-items: center;
...@@ -174,6 +188,13 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css` ...@@ -174,6 +188,13 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css`
padding: 2px 0; padding: 2px 0;
} }
[data-nextjs-devtool-segment-explorer-level].segment-explorer-segment-children {
padding-left: 20px;
}
[data-nextjs-devtool-segment-explorer-level='0'].segment-explorer-segment-children {
padding-left: 0px;
}
.segment-explorer-filename-path { .segment-explorer-filename-path {
display: inline-block; display: inline-block;
} }
......
export default function Layout({ children }) {
return (
<div>
<h3>Nested Layout</h3>
<div className="nested-children">{children}</div>
</div>
)
}
export default function Page() {
return (
<div>
Page <br />
</div>
)
}
export default function Layout({ children }) {
return (
<div>
<h3>Nested Layout</h3>
<div className="nested-children">{children}</div>
</div>
)
}
export default function Layout({ children }) {
return (
<div>
<h3>Nested Layout</h3>
<div className="nested-children">{children}</div>
</div>
)
}
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { openDevToolsIndicatorPopover } from 'next-test-utils' import { openDevToolsIndicatorPopover } from 'next-test-utils'
import { Playwright } from 'next-webdriver'
async function getSegmentExplorerContent(browser: Playwright) {
// open the devtool button
await openDevToolsIndicatorPopover(browser)
// open the segment explorer
await browser.elementByCss('[data-segment-explorer]').click()
// wait for the segment explorer to be visible
await browser.waitForElementByCss('[data-nextjs-devtool-segment-explorer]')
const content = await browser.elementByCss(
'[data-nextjs-devtool-segment-explorer]'
)
return content.text()
}
describe('segment-explorer', () => { describe('segment-explorer', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
it('should render the segment explorer', async () => { it('should render the segment explorer for parallel routes', async () => {
const browser = await next.browser('/parallel-routes') const browser = await next.browser('/parallel-routes')
expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(`
"app/
layout.tsx
parallel-routes/
layout.tsx
page.tsx
@bar/
layout.tsx
page.tsx
@foo/
layout.tsx
page.tsx"
`)
})
// open the devtool button it('should render the segment explorer for nested routes', async () => {
await openDevToolsIndicatorPopover(browser) const browser = await next.browser('/blog/~/grid')
expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(`
// open the segment explorer "app/
await browser.elementByCss('[data-segment-explorer]').click() layout.tsx
(v2)/
// wait for the segment explorer to be visible layout.tsx
await browser.waitForElementByCss('[data-nextjs-devtool-segment-explorer]') blog/
(team)/
const content = await browser.elementByCss( layout.tsx
'[data-nextjs-devtool-segment-explorer]' ~/
) (overview)/
expect(await content.text()).toMatchInlineSnapshot(` layout.tsx
"app/layout.tsx grid/
app/parallel-routes/layout.tsx page.tsx"
app/parallel-routes/page.tsx
app/parallel-routes/@bar/layout.tsx
app/parallel-routes/@bar/page.tsx
app/parallel-routes/@foo/layout.tsx
app/parallel-routes/@foo/page.tsx"
`) `)
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment