Unverified Commit 737d3199 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[segment-explorer] optimize tree view (#80392)

parent cce749aa
...@@ -2,53 +2,12 @@ import type { HTMLProps } from 'react' ...@@ -2,53 +2,12 @@ import type { HTMLProps } from 'react'
import { css } from '../../utils/css' import { css } from '../../utils/css'
import type { DevToolsInfoPropsCore } from '../errors/dev-tools-indicator/dev-tools-info/dev-tools-info' import type { DevToolsInfoPropsCore } from '../errors/dev-tools-indicator/dev-tools-info/dev-tools-info'
import { DevToolsInfo } from '../errors/dev-tools-indicator/dev-tools-info/dev-tools-info' import { DevToolsInfo } from '../errors/dev-tools-indicator/dev-tools-info/dev-tools-info'
import { cx } from '../../utils/cx'
import { import {
type SegmentNode, type SegmentNode,
useSegmentTreeClientState, useSegmentTreeClientState,
} from '../../../../shared/lib/devtool/app-segment-tree' } from '../../../../shared/lib/devtool/app-segment-tree'
import type { Trie, TrieNode } from '../../../../shared/lib/devtool/trie' import type { Trie, TrieNode } from '../../../../shared/lib/devtool/trie'
const IconLayout = (props: React.SVGProps<SVGSVGElement>) => {
return (
<svg
{...props}
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M16 12.5L15.9873 12.7559C15.8677 13.9323 14.9323 14.8677 13.7559 14.9873L13.5 15H2.5L2.24414 14.9873C1.06772 14.8677 0.132274 13.9323 0.0126953 12.7559L0 12.5V1H16V12.5ZM1.5 6.25488V12.5C1.5 13.0523 1.94772 13.5 2.5 13.5H4.99512V6.25488H1.5ZM6.24512 6.25488V13.5H13.5C14.0523 13.5 14.5 13.0523 14.5 12.5V6.25488H6.24512ZM1.5 5.00488H14.5V2.5H1.5V5.00488Z"
fill="currentColor"
/>
</svg>
)
}
const IconPage = (props: React.SVGProps<SVGSVGElement>) => {
return (
<svg
{...props}
viewBox="0 0 16 16"
fill="none"
strokeLinejoin="round"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M14.5 6.5V13.5C14.5 14.8807 13.3807 16 12 16H4C2.61929 16 1.5 14.8807 1.5 13.5V1.5V0H3H8H9.08579C9.351 0 9.60536 0.105357 9.79289 0.292893L14.2071 4.70711C14.3946 4.89464 14.5 5.149 14.5 5.41421V6.5ZM13 6.5V13.5C13 14.0523 12.5523 14.5 12 14.5H4C3.44772 14.5 3 14.0523 3 13.5V1.5H8V5V6.5H9.5H13ZM9.5 2.12132V5H12.3787L9.5 2.12132Z"
fill="currentColor"
/>
</svg>
)
}
const ICONS = {
layout: <IconLayout width={16} />,
page: <IconPage width={16} />,
}
function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) { function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) {
if (!tree) { if (!tree) {
return null return null
...@@ -63,6 +22,7 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) { ...@@ -63,6 +22,7 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) {
node={tree.getRoot()} node={tree.getRoot()}
level={0} level={0}
segment="" segment=""
parentSegment=""
/> />
</div> </div>
) )
...@@ -71,19 +31,22 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) { ...@@ -71,19 +31,22 @@ function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) {
function PageSegmentTreeLayerPresentation({ function PageSegmentTreeLayerPresentation({
tree, tree,
segment, segment,
parentSegment,
node, node,
level, level,
}: { }: {
tree: Trie<SegmentNode> tree: Trie<SegmentNode>
segment: string segment: string
parentSegment: string
node: TrieNode<SegmentNode> node: TrieNode<SegmentNode>
level: number level: number
}) { }) {
const pagePath = node.value?.pagePath || '' const pagePath = node.value?.pagePath || ''
const nodeName = node.value?.type const nodeName = node.value?.type
const isFile = !!nodeName
const segments = pagePath.split('/') || [] const segments = pagePath.split('/') || []
const fileName = segments.pop() || '' const fileName = isFile ? segments.pop() || '' : ''
const childrenKeys = Object.keys(node.children) const childrenKeys = Object.keys(node.children)
const sortedChildrenKeys = childrenKeys.sort((a, b) => { const sortedChildrenKeys = childrenKeys.sort((a, b) => {
...@@ -97,65 +60,64 @@ function PageSegmentTreeLayerPresentation({ ...@@ -97,65 +60,64 @@ function PageSegmentTreeLayerPresentation({
return a.localeCompare(b) return a.localeCompare(b)
}) })
// check if it has file children
const hasFileChildren = sortedChildrenKeys.some((key) => {
const childNode = node.children[key]
return !!childNode?.value?.type
})
// If it's the 1st level and contains a file, use 'app' as the folder name
const folderName = level === 1 && isFile ? 'app' : parentSegment
return ( return (
<>
{isFile ? (
<div <div
className="segment-explorer-item" className="segment-explorer-item"
data-nextjs-devtool-segment-explorer-segment={segment} data-nextjs-devtool-segment-explorer-segment={segment}
> >
{fileName ? ( <div
<div className={cx('segment-explorer-item-row')}> className="segment-explorer-item-row"
style={{
// If it's children levels, show indents if there's any file at that level.
// Otherwise it's empty folder, no need to show indents.
...(level > 0 && isFile && { paddingLeft: `${level * 8}px` }),
}}
>
<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 <div className="segment-explorer-filename">
className={cx( {folderName && (
'segment-explorer-line-icon', <span className="segment-explorer-filename--path">
`segment-explorer-line-icon-${nodeName}` {folderName}
</span>
)} )}
> <span className="segment-explorer-filename--name">
{nodeName === 'layout' ? ICONS.layout : ICONS.page} {fileName}
</span> </span>
<span className="segment-explorer-filename-path">{fileName}</span>
</div>
</div> </div>
</div> </div>
) : segment ? (
<div className={'segment-explorer-item-row'}>
<div className="segment-explorer-line">
<div className={`segment-explorer-line-text-${nodeName}`}>
<span
className={cx(
'segment-explorer-line-icon',
`segment-explorer-line-icon-${nodeName}`
)}
></span>
<span className="segment-explorer-filename-path">
{`${segment}/`}
</span>
</div> </div>
</div> </div>
</div> </div>
) : null} ) : null}
<div
className="segment-explorer-segment-children"
data-nextjs-devtool-segment-explorer-level={level}
>
{sortedChildrenKeys.map((childSegment) => { {sortedChildrenKeys.map((childSegment) => {
const child = node.children[childSegment] const child = node.children[childSegment]
if (!child) {
return null
}
return ( return (
child && (
<PageSegmentTreeLayerPresentation <PageSegmentTreeLayerPresentation
key={childSegment} key={childSegment}
segment={childSegment} segment={childSegment}
parentSegment={segment}
tree={tree} tree={tree}
node={child} node={child}
level={level + 1} level={hasFileChildren ? level + 1 : level}
/> />
) )
)
})} })}
</div> </>
</div>
) )
} }
...@@ -177,31 +139,34 @@ export function SegmentsExplorer( ...@@ -177,31 +139,34 @@ export function SegmentsExplorer(
export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css` export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css`
.segment-explorer-content { .segment-explorer-content {
overflow-y: auto; overflow-y: auto;
padding: 0 12px;
font-size: var(--size-14); font-size: var(--size-14);
margin: -12px -8px;
}
.segment-explorer-item {
margin: 4px 0;
}
.segment-explorer-item:nth-child(odd) {
background-color: var(--color-gray-100);
} }
.segment-explorer-item-row { .segment-explorer-item-row {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 8px; padding: 4px 24px;
padding: 2px 0; border-radius: 6px;
} }
[data-nextjs-devtool-segment-explorer-level].segment-explorer-segment-children { .segment-explorer-children--intended {
padding-left: 20px; padding-left: 16px;
}
[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; margin-right: 8px;
} }
.segment-explorer-filename--name {
.segment-explorer-filename-path a { color: var(--color-gray-800);
color: inherit;
text-decoration: inherit;
} }
.segment-explorer-line { .segment-explorer-line {
...@@ -209,15 +174,11 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css` ...@@ -209,15 +174,11 @@ export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css`
cursor: default; cursor: default;
} }
.segment-explorer-line-icon { .segment-explorer-line {
margin-right: 4px; color: var(--color-gray-1000);
}
.segment-explorer-line-icon-page {
color: inherit;
} }
.segment-explorer-line-text-page { [data-nextjs-devtool-segment-explorer-level='odd'] {
color: var(--color-gray-1000); background-color: var(--color-gray-100);
font-weight: 500;
} }
` `
...@@ -26,35 +26,24 @@ describe('segment-explorer', () => { ...@@ -26,35 +26,24 @@ describe('segment-explorer', () => {
it('should render the segment explorer for parallel routes', 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(` expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(`
"app/ "applayout.tsx
layout.tsx parallel-routeslayout.tsx
parallel-routes/ parallel-routespage.tsx
layout.tsx @barlayout.tsx
page.tsx @barpage.tsx
@bar/ @foolayout.tsx
layout.tsx @foopage.tsx"
page.tsx
@foo/
layout.tsx
page.tsx"
`) `)
}) })
it('should render the segment explorer for nested routes', async () => { it('should render the segment explorer for nested routes', async () => {
const browser = await next.browser('/blog/~/grid') const browser = await next.browser('/blog/~/grid')
expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(` expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(`
"app/ "applayout.tsx
layout.tsx (v2)layout.tsx
(v2)/ (team)layout.tsx
layout.tsx (overview)layout.tsx
blog/ gridpage.tsx"
(team)/
layout.tsx
~/
(overview)/
layout.tsx
grid/
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