|
| 1 | +import type { HTMLProps } from 'react' |
| 2 | +import { css } from '../../../utils/css' |
| 3 | +import type { DevToolsInfoPropsCore } from '../errors/dev-tools-indicator/dev-tools-info/dev-tools-info' |
| 4 | +import { DevToolsInfo } from '../errors/dev-tools-indicator/dev-tools-info/dev-tools-info' |
| 5 | +import { cx } from '../../utils/cx' |
| 6 | +import { LeftArrow } from '../../icons/left-arrow' |
| 7 | +import { |
| 8 | + useSegmentTreeClientState, |
| 9 | + type SegmentNode, |
| 10 | +} from '../../../../../../shared/lib/devtool/app-segment-tree' |
| 11 | +import type { Trie, TrieNode } from '../../../../../../shared/lib/devtool/trie' |
| 12 | + |
| 13 | +const IconLayout = (props: React.SVGProps<SVGSVGElement>) => { |
| 14 | + return ( |
| 15 | + <svg |
| 16 | + {...props} |
| 17 | + viewBox="0 0 16 16" |
| 18 | + fill="none" |
| 19 | + xmlns="http://www.w3.org/2000/svg" |
| 20 | + > |
| 21 | + <path |
| 22 | + 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" |
| 23 | + fill="currentColor" |
| 24 | + /> |
| 25 | + </svg> |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +const IconPage = (props: React.SVGProps<SVGSVGElement>) => { |
| 30 | + return ( |
| 31 | + <svg |
| 32 | + {...props} |
| 33 | + viewBox="0 0 16 16" |
| 34 | + fill="none" |
| 35 | + strokeLinejoin="round" |
| 36 | + xmlns="http://www.w3.org/2000/svg" |
| 37 | + > |
| 38 | + <path |
| 39 | + fillRule="evenodd" |
| 40 | + clipRule="evenodd" |
| 41 | + 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" |
| 42 | + fill="currentColor" |
| 43 | + /> |
| 44 | + </svg> |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +const ICONS = { |
| 49 | + layout: <IconLayout width={16} />, |
| 50 | + page: <IconPage width={16} />, |
| 51 | +} |
| 52 | + |
| 53 | +function PageSegmentTree({ tree }: { tree: Trie<SegmentNode> | undefined }) { |
| 54 | + if (!tree) { |
| 55 | + return null |
| 56 | + } |
| 57 | + return ( |
| 58 | + <div className="segment-explorer-content"> |
| 59 | + <PageSegmentTreeLayerPresentation |
| 60 | + tree={tree} |
| 61 | + node={tree.getRoot()} |
| 62 | + level={0} |
| 63 | + /> |
| 64 | + </div> |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +function PageSegmentTreeLayerPresentation({ |
| 69 | + tree, |
| 70 | + node, |
| 71 | + level, |
| 72 | +}: { |
| 73 | + tree: Trie<SegmentNode> |
| 74 | + node: TrieNode<SegmentNode> |
| 75 | + level: number |
| 76 | +}) { |
| 77 | + const segments = node.value?.pagePath?.split('/') || [] |
| 78 | + const fileName = segments[segments.length - 1] || '' |
| 79 | + const nodeName = node.value?.type |
| 80 | + const pagePathPrefix = segments.slice(0, -1).join('/') |
| 81 | + |
| 82 | + return ( |
| 83 | + <div className="segment-explorer-item"> |
| 84 | + {!fileName || level === 0 ? null : ( |
| 85 | + <div className="segment-explorer-item-row"> |
| 86 | + <div className="segment-explorer-line"> |
| 87 | + <div className={`segment-explorer-line-text-${nodeName}`}> |
| 88 | + <span |
| 89 | + className={cx( |
| 90 | + 'segment-explorer-line-icon', |
| 91 | + `segment-explorer-line-icon-${nodeName}` |
| 92 | + )} |
| 93 | + > |
| 94 | + {nodeName === 'layout' ? ICONS.layout : ICONS.page} |
| 95 | + </span> |
| 96 | + {pagePathPrefix === '' ? '' : `${pagePathPrefix}/`} |
| 97 | + <span className="segment-explorer-filename-path">{fileName}</span> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + |
| 103 | + <div className="tree-node-expanded-rendered-children"> |
| 104 | + {Object.entries(node.children).map( |
| 105 | + ([key, child]) => |
| 106 | + child && ( |
| 107 | + <PageSegmentTreeLayerPresentation |
| 108 | + key={key} |
| 109 | + tree={tree} |
| 110 | + node={child} |
| 111 | + level={level + 1} |
| 112 | + /> |
| 113 | + ) |
| 114 | + )} |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +export function SegmentsExplorer( |
| 121 | + props: DevToolsInfoPropsCore & HTMLProps<HTMLDivElement> |
| 122 | +) { |
| 123 | + const ctx = useSegmentTreeClientState() |
| 124 | + if (!ctx) { |
| 125 | + return null |
| 126 | + } |
| 127 | + |
| 128 | + return ( |
| 129 | + <DevToolsInfo |
| 130 | + title={ |
| 131 | + <> |
| 132 | + <button |
| 133 | + className="segment-explorer-back-button" |
| 134 | + onClick={props.close} |
| 135 | + > |
| 136 | + <LeftArrow /> |
| 137 | + </button> |
| 138 | + {'Segment Explorer'} |
| 139 | + </> |
| 140 | + } |
| 141 | + closeButton={false} |
| 142 | + {...props} |
| 143 | + > |
| 144 | + <PageSegmentTree tree={ctx.tree} /> |
| 145 | + </DevToolsInfo> |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +export const DEV_TOOLS_INFO_RENDER_FILES_STYLES = css` |
| 150 | + .segment-explorer-back-button { |
| 151 | + margin-right: 12px; |
| 152 | + color: var(--color-gray-1000); |
| 153 | + } |
| 154 | + .segment-explorer-back-button svg { |
| 155 | + width: 20px; |
| 156 | + height: 20px; |
| 157 | + } |
| 158 | +
|
| 159 | + .segment-explorer-content { |
| 160 | + overflow-y: auto; |
| 161 | + padding: 0 12px; |
| 162 | + font-size: var(--size-14); |
| 163 | + } |
| 164 | +
|
| 165 | + .segment-explorer-item-row { |
| 166 | + display: flex; |
| 167 | + align-items: center; |
| 168 | + gap: 8px; |
| 169 | + padding: 2px 0; |
| 170 | + } |
| 171 | +
|
| 172 | + .segment-explorer-filename-path { |
| 173 | + display: inline-block; |
| 174 | +
|
| 175 | + &:hover { |
| 176 | + color: var(--color-gray-1000); |
| 177 | + text-decoration: none; |
| 178 | + } |
| 179 | + } |
| 180 | +
|
| 181 | + .segment-explorer-filename-path a { |
| 182 | + color: inherit; |
| 183 | + text-decoration: inherit; |
| 184 | + } |
| 185 | +
|
| 186 | + .segment-explorer-line { |
| 187 | + white-space: pre; |
| 188 | + } |
| 189 | +
|
| 190 | + .segment-explorer-line-icon { |
| 191 | + margin-right: 4px; |
| 192 | + } |
| 193 | + .segment-explorer-line-icon-page { |
| 194 | + color: inherit; |
| 195 | + } |
| 196 | + .segment-explorer-line-icon-layout { |
| 197 | + color: var(--color-gray-1-00); |
| 198 | + } |
| 199 | +
|
| 200 | + .segment-explorer-line-text-page { |
| 201 | + color: var(--color-blue-900); |
| 202 | + font-weight: 500; |
| 203 | + } |
| 204 | +` |
0 commit comments