Commit 7f7c87bd authored by Jiachi Liu's avatar Jiachi Liu Committed by RobPruzan

[segment explorer] capture defined boundaries

parent 4b850b2f
...@@ -507,6 +507,7 @@ export default function OuterLayoutRouter({ ...@@ -507,6 +507,7 @@ export default function OuterLayoutRouter({
forbidden, forbidden,
unauthorized, unauthorized,
gracefullyDegrade, gracefullyDegrade,
segmentViewBoundaries,
}: { }: {
parallelRouterKey: string parallelRouterKey: string
error: ErrorComponent | undefined error: ErrorComponent | undefined
...@@ -519,6 +520,7 @@ export default function OuterLayoutRouter({ ...@@ -519,6 +520,7 @@ export default function OuterLayoutRouter({
forbidden: React.ReactNode | undefined forbidden: React.ReactNode | undefined
unauthorized: React.ReactNode | undefined unauthorized: React.ReactNode | undefined
gracefullyDegrade?: boolean gracefullyDegrade?: boolean
segmentViewBoundaries?: React.ReactNode
}) { }) {
const context = useContext(LayoutRouterContext) const context = useContext(LayoutRouterContext)
if (!context) { if (!context) {
...@@ -659,13 +661,14 @@ export default function OuterLayoutRouter({ ...@@ -659,13 +661,14 @@ export default function OuterLayoutRouter({
) )
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const SegmentStateProvider = ( const { SegmentStateProvider } =
require('../../next-devtools/userspace/app/segment-explorer-node') as typeof import('../../next-devtools/userspace/app/segment-explorer-node') require('../../next-devtools/userspace/app/segment-explorer-node') as typeof import('../../next-devtools/userspace/app/segment-explorer-node')
)
.SegmentStateProvider as typeof import('../../next-devtools/userspace/app/segment-explorer-node').SegmentStateProvider as typeof import('../../next-devtools/userspace/app/segment-explorer-node').SegmentStateProvider
child = ( child = (
<SegmentStateProvider key={stateKey}>{child}</SegmentStateProvider> <SegmentStateProvider key={stateKey}>
{child}
{segmentViewBoundaries}
</SegmentStateProvider>
) )
} }
......
...@@ -5,9 +5,11 @@ import type { SegmentNodeState } from '../../../userspace/app/segment-explorer-n ...@@ -5,9 +5,11 @@ import type { SegmentNodeState } from '../../../userspace/app/segment-explorer-n
export function SegmentBoundaryTrigger({ export function SegmentBoundaryTrigger({
onSelectBoundary, onSelectBoundary,
offset, offset,
boundaries,
}: { }: {
onSelectBoundary: SegmentNodeState['setBoundaryType'] onSelectBoundary: SegmentNodeState['setBoundaryType']
offset: number offset: number
boundaries: Record<'not-found' | 'loading' | 'error', string | null>
}) { }) {
const [shadowRoot] = useState<ShadowRoot>(() => { const [shadowRoot] = useState<ShadowRoot>(() => {
const ownerDocument = document const ownerDocument = document
...@@ -17,9 +19,24 @@ export function SegmentBoundaryTrigger({ ...@@ -17,9 +19,24 @@ export function SegmentBoundaryTrigger({
const shadowRootRef = useRef<ShadowRoot>(shadowRoot) const shadowRootRef = useRef<ShadowRoot>(shadowRoot)
const triggerOptions = [ const triggerOptions = [
{ label: 'Trigger Loading', value: 'loading', icon: <LoadingIcon /> }, {
{ label: 'Trigger Error', value: 'error', icon: <ErrorIcon /> }, label: 'Trigger Loading',
{ label: 'Trigger Not Found', value: 'not-found', icon: <NotFoundIcon /> }, value: 'loading',
icon: <LoadingIcon />,
disabled: !boundaries.loading,
},
{
label: 'Trigger Error',
value: 'error',
icon: <ErrorIcon />,
disabled: !boundaries.error,
},
{
label: 'Trigger Not Found',
value: 'not-found',
icon: <NotFoundIcon />,
disabled: !boundaries['not-found'],
},
] ]
const resetOption = { const resetOption = {
...@@ -74,6 +91,7 @@ export function SegmentBoundaryTrigger({ ...@@ -74,6 +91,7 @@ export function SegmentBoundaryTrigger({
key={option.value} key={option.value}
className="segment-boundary-dropdown-item" className="segment-boundary-dropdown-item"
onClick={() => handleSelect(option.value)} onClick={() => handleSelect(option.value)}
disabled={option.disabled}
> >
{option.icon} {option.icon}
{option.label} {option.label}
...@@ -274,9 +292,14 @@ export const styles = ` ...@@ -274,9 +292,14 @@ export const styles = `
width: 100%; width: 100%;
} }
.segment-boundary-dropdown-item[data-disabled] {
color: var(--color-gray-400);
cursor: not-allowed;
}
.segment-boundary-dropdown-item svg { .segment-boundary-dropdown-item svg {
margin-right: 12px; margin-right: 12px;
color: var(--color-gray-900); color: currentColor;
} }
.segment-boundary-dropdown-item:hover { .segment-boundary-dropdown-item:hover {
......
...@@ -133,6 +133,23 @@ function PageSegmentTreeLayerPresentation({ ...@@ -133,6 +133,23 @@ function PageSegmentTreeLayerPresentation({
} }
const hasFilesChildren = filesChildrenKeys.length > 0 const hasFilesChildren = filesChildrenKeys.length > 0
const boundaries: Record<'not-found' | 'loading' | 'error', string | null> = {
'not-found': null,
loading: null,
error: null,
}
filesChildrenKeys.forEach((childKey) => {
const childNode = node.children[childKey]
if (!childNode || !childNode.value) return
if (childNode.value.type.startsWith('boundary:')) {
const boundaryType = childNode.value.type.split(':')[1] as
| 'not-found'
| 'loading'
| 'error'
boundaries[boundaryType] = childNode.value.pagePath || null
}
})
return ( return (
<> <>
...@@ -165,6 +182,11 @@ function PageSegmentTreeLayerPresentation({ ...@@ -165,6 +182,11 @@ function PageSegmentTreeLayerPresentation({
if (!childNode || !childNode.value) { if (!childNode || !childNode.value) {
return null return null
} }
// If it's boundary node, which marks the existence of the boundary not the rendered status,
// we don't need to present in the rendered files.
if (childNode.value.type.startsWith('boundary:')) {
return null
}
const filePath = childNode.value.pagePath const filePath = childNode.value.pagePath
const lastSegment = filePath.split('/').pop() || '' const lastSegment = filePath.split('/').pop() || ''
const isBuiltin = filePath.startsWith(BUILTIN_PREFIX) const isBuiltin = filePath.startsWith(BUILTIN_PREFIX)
...@@ -208,6 +230,7 @@ function PageSegmentTreeLayerPresentation({ ...@@ -208,6 +230,7 @@ function PageSegmentTreeLayerPresentation({
<SegmentBoundaryTrigger <SegmentBoundaryTrigger
offset={6} offset={6}
onSelectBoundary={pageChild.value.setBoundaryType} onSelectBoundary={pageChild.value.setBoundaryType}
boundaries={boundaries}
/> />
)} )}
</div> </div>
......
...@@ -97,6 +97,7 @@ function createTrie<Value = string>({ ...@@ -97,6 +97,7 @@ function createTrie<Value = string>({
function remove(value: Value) { function remove(value: Value) {
let currentNode = root let currentNode = root
const segments = getCharacters(value) const segments = getCharacters(value)
const stack: TrieNode<Value>[] = [] const stack: TrieNode<Value>[] = []
let found = true let found = true
for (const segment of segments) { for (const segment of segments) {
......
...@@ -31,15 +31,14 @@ function SegmentTrieNode({ ...@@ -31,15 +31,14 @@ function SegmentTrieNode({
pagePath: string pagePath: string
}): React.ReactNode { }): React.ReactNode {
const { boundaryType, setBoundaryType } = useSegmentState() const { boundaryType, setBoundaryType } = useSegmentState()
const nodeState: SegmentNodeState = useMemo( const nodeState: SegmentNodeState = useMemo(() => {
() => ({ return {
type, type,
pagePath, pagePath,
boundaryType, boundaryType,
setBoundaryType, setBoundaryType,
}), }
[type, pagePath, boundaryType, setBoundaryType] }, [type, pagePath, boundaryType, setBoundaryType])
)
// Use `useLayoutEffect` to ensure the state is updated during suspense. // Use `useLayoutEffect` to ensure the state is updated during suspense.
// `useEffect` won't work as the state is preserved during suspense. // `useEffect` won't work as the state is preserved during suspense.
...@@ -143,7 +142,10 @@ export function SegmentStateProvider({ children }: { children: ReactNode }) { ...@@ -143,7 +142,10 @@ export function SegmentStateProvider({ children }: { children: ReactNode }) {
return ( return (
<SegmentStateContext.Provider <SegmentStateContext.Provider
key={errorBoundaryKey} key={errorBoundaryKey}
value={{ boundaryType, setBoundaryType: setBoundaryTypeAndReload }} value={{
boundaryType,
setBoundaryType: setBoundaryTypeAndReload,
}}
> >
{children} {children}
</SegmentStateContext.Provider> </SegmentStateContext.Provider>
......
...@@ -410,15 +410,16 @@ async function createComponentTreeInternal({ ...@@ -410,15 +410,16 @@ async function createComponentTreeInternal({
<MetadataOutlet ready={getMetadataReady} /> <MetadataOutlet ready={getMetadataReady} />
) )
const notFoundElement = await createBoundaryConventionElement({ const [notFoundElement, notFoundFilePath] =
ctx, await createBoundaryConventionElement({
conventionName: 'not-found', ctx,
Component: NotFound, conventionName: 'not-found',
styles: notFoundStyles, Component: NotFound,
tree, styles: notFoundStyles,
}) tree,
})
const forbiddenElement = await createBoundaryConventionElement({
const [forbiddenElement] = await createBoundaryConventionElement({
ctx, ctx,
conventionName: 'forbidden', conventionName: 'forbidden',
Component: Forbidden, Component: Forbidden,
...@@ -426,7 +427,7 @@ async function createComponentTreeInternal({ ...@@ -426,7 +427,7 @@ async function createComponentTreeInternal({
tree, tree,
}) })
const unauthorizedElement = await createBoundaryConventionElement({ const [unauthorizedElement] = await createBoundaryConventionElement({
ctx, ctx,
conventionName: 'unauthorized', conventionName: 'unauthorized',
Component: Unauthorized, Component: Unauthorized,
...@@ -546,8 +547,8 @@ async function createComponentTreeInternal({ ...@@ -546,8 +547,8 @@ async function createComponentTreeInternal({
) )
const templateFilePath = getConventionPathByType(tree, dir, 'template') const templateFilePath = getConventionPathByType(tree, dir, 'template')
const errorFilePath = getConventionPathByType(tree, dir, 'error') const errorFilePath = getConventionPathByType(tree, dir, 'error')
const loadingFilePath = getConventionPathByType(tree, dir, 'loading')
const wrappedErrorStyles = const wrappedErrorStyles =
isSegmentViewEnabled && errorFilePath ? ( isSegmentViewEnabled && errorFilePath ? (
...@@ -558,6 +559,34 @@ async function createComponentTreeInternal({ ...@@ -558,6 +559,34 @@ async function createComponentTreeInternal({
errorStyles errorStyles
) )
// Add a suffix to avoid conflict with the segment view node representing rendered file.
// existence: not-found.tsx@boundary
// rendered: not-found.tsx
const fileNameSuffix = '@boundary'
const segmentViewBoundaries = isSegmentViewEnabled ? (
<>
{notFoundFilePath && (
<SegmentViewNode
type={'boundary:not-found'}
pagePath={notFoundFilePath + fileNameSuffix}
/>
)}
{loadingFilePath && (
<SegmentViewNode
type={'boundary:loading'}
pagePath={loadingFilePath + fileNameSuffix}
/>
)}
{errorFilePath && (
<SegmentViewNode
type={'boundary:error'}
pagePath={errorFilePath + fileNameSuffix}
/>
)}
{/* do not surface forbidden and unauthorized boundaries yet as they're unstable */}
</>
) : null
return [ return [
parallelRouteKey, parallelRouteKey,
<LayoutRouter <LayoutRouter
...@@ -581,6 +610,7 @@ async function createComponentTreeInternal({ ...@@ -581,6 +610,7 @@ async function createComponentTreeInternal({
notFound={notFoundComponent} notFound={notFoundComponent}
forbidden={forbiddenComponent} forbidden={forbiddenComponent}
unauthorized={unauthorizedComponent} unauthorized={unauthorizedComponent}
{...(isSegmentViewEnabled && { segmentViewBoundaries })}
// Since gracefullyDegrade only applies to bots, only // Since gracefullyDegrade only applies to bots, only
// pass it when we're in a bot context to avoid extra bytes. // pass it when we're in a bot context to avoid extra bytes.
{...(gracefullyDegrade && { gracefullyDegrade })} {...(gracefullyDegrade && { gracefullyDegrade })}
...@@ -603,8 +633,8 @@ async function createComponentTreeInternal({ ...@@ -603,8 +633,8 @@ async function createComponentTreeInternal({
} }
let loadingElement = Loading ? <Loading key="l" /> : null let loadingElement = Loading ? <Loading key="l" /> : null
const loadingFilePath = getConventionPathByType(tree, dir, 'loading')
if (isSegmentViewEnabled && loadingElement) { if (isSegmentViewEnabled && loadingElement) {
const loadingFilePath = getConventionPathByType(tree, dir, 'loading')
if (loadingFilePath) { if (loadingFilePath) {
loadingElement = ( loadingElement = (
<SegmentViewNode <SegmentViewNode
...@@ -1098,12 +1128,14 @@ async function createBoundaryConventionElement({ ...@@ -1098,12 +1128,14 @@ async function createBoundaryConventionElement({
</> </>
) : undefined ) : undefined
const pagePath = getConventionPathByType(tree, dir, conventionName)
const wrappedElement = const wrappedElement =
isSegmentViewEnabled && element ? ( isSegmentViewEnabled && element ? (
<SegmentViewNode <SegmentViewNode
key={cacheNodeKey + '-' + conventionName} key={cacheNodeKey + '-' + conventionName}
type={conventionName} type={conventionName}
pagePath={getConventionPathByType(tree, dir, conventionName)!} pagePath={pagePath!}
> >
{element} {element}
</SegmentViewNode> </SegmentViewNode>
...@@ -1111,5 +1143,5 @@ async function createBoundaryConventionElement({ ...@@ -1111,5 +1143,5 @@ async function createBoundaryConventionElement({
element element
) )
return wrappedElement return [wrappedElement, pagePath] as const
} }
export default function NotFound() {
return <div>Not Found @foo</div>
}
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