import './segment-boundary-trigger.css' import { useCallback, useState, useRef, useMemo } from 'react' import { Menu } from '@base-ui-components/react/menu' import type { SegmentBoundaryType, SegmentNodeState, } from '../../../userspace/app/segment-explorer-node' import { normalizeBoundaryFilename } from '../../../../server/app-render/segment-explorer-path' import { useClickOutsideAndEscape } from '../errors/dev-tools-indicator/utils' const composeRefs = (...refs: (React.Ref | undefined)[]) => { return (node: HTMLButtonElement | null) => { refs.forEach((ref) => { if (typeof ref === 'function') { ref(node) } else if (ref) { ref.current = node } }) } } export function SegmentBoundaryTrigger({ nodeState, boundaries, }: { nodeState: SegmentNodeState boundaries: Record }) { const currNode = nodeState const { pagePath, boundaryType, setBoundaryType: onSelectBoundary } = currNode const [isOpen, setIsOpen] = useState(false) // TODO: move this shadowRoot ref util to a shared hook or into context const [shadowRoot] = useState(() => { const ownerDocument = document const portalNode = ownerDocument.querySelector('nextjs-portal')! return portalNode.shadowRoot! as ShadowRoot }) const shadowRootRef = useRef(shadowRoot) const triggerRef = useRef(null) const popupRef = useRef(null) // Click outside of popup should close the menu useClickOutsideAndEscape( popupRef, triggerRef, isOpen, () => { setIsOpen(false) }, triggerRef.current?.ownerDocument ) const firstDefinedBoundary = Object.values(boundaries).find((v) => v !== null) const possibleExtension = (firstDefinedBoundary || '').split('.').pop() || 'js' const fileNames = useMemo(() => { return Object.fromEntries( Object.entries(boundaries).map(([key, filePath]) => { const fileName = normalizeBoundaryFilename( (filePath || '').split('/').pop() || `${key}.${possibleExtension}` ) return [key, fileName] }) ) as Record }, [boundaries, possibleExtension]) const fileName = (pagePath || '').split('/').pop() || '' const pageFileName = normalizeBoundaryFilename( boundaryType ? `page.${possibleExtension}` : fileName || `page.${possibleExtension}` ) const triggerOptions = [ { label: fileNames.loading, value: 'loading', icon: , disabled: !boundaries.loading, }, { label: fileNames.error, value: 'error', icon: , disabled: !boundaries.error, }, { label: fileNames['not-found'], value: 'not-found', icon: , disabled: !boundaries['not-found'], }, ] const resetOption = { label: boundaryType ? 'Reset' : pageFileName, value: 'reset', icon: , disabled: boundaryType === null, } const openInEditor = useCallback(({ filePath }: { filePath: string }) => { const params = new URLSearchParams({ file: filePath, isAppRelativePath: '1', }) fetch( `${ process.env.__NEXT_ROUTER_BASEPATH || '' }/__nextjs_launch-editor?${params.toString()}` // Log the failures to console, not track them as console errors in error overlay ).catch(console.warn) }, []) const handleSelect = useCallback( (value: string) => { switch (value) { case 'not-found': case 'loading': case 'error': onSelectBoundary(value) break case 'reset': onSelectBoundary(null) break case 'open-editor': if (pagePath) { openInEditor({ filePath: pagePath }) } break default: break } }, [onSelectBoundary, pagePath, openInEditor] ) const MergedRefTrigger = ( triggerProps: React.ComponentProps<'button'> & { ref?: React.Ref } ) => { const mergedRef = composeRefs(triggerProps.ref, triggerRef) return } const hasBoundary = useMemo(() => { const hasPageOrBoundary = nodeState.type !== 'layout' && nodeState.type !== 'template' return ( hasPageOrBoundary && Object.values(boundaries).some((v) => v !== null) ) }, [nodeState.type, boundaries]) return ( {/* @ts-expect-error remove this expect-error once shadowRoot is supported as container */} { Toggle Overrides {triggerOptions.map((option) => ( handleSelect(option.value)} disabled={option.disabled} > {option.icon} {option.label} ))} } { handleSelect(resetOption.value)} disabled={resetOption.disabled} > {resetOption.icon} {resetOption.label} } ) } function LoadingIcon() { return ( ) } function ErrorIcon() { return ( ) } function NotFoundIcon() { return ( ) } function ResetIcon() { return ( ) } function SwitchIcon(props: React.SVGProps) { return ( ) } function Trigger(props: React.ComponentProps<'button'>) { return ( ) }