import { useMemo, useState } from 'react' import { CollapseIcon } from '../../icons/collapse-icon' /** * * Format component stack into pseudo HTML * component stack is an array of strings, e.g.: ['p', 'p', 'Page', ...] * * For html tags mismatch, it will render it for the code block * * ``` *
* {`
*
*
*
* `}
*
* ```
*
* For text mismatch, it will render it for the code block
*
* ```
* *{` ** ``` * * For bad text under a tag it will render it for the code block, * e.g. "Mismatched Text" under* * `}* "Server Text" (green) * "Client Text" (red) *
** * ``` *
*{` ** ``` * */ export function PseudoHtmlDiff({ reactOutputComponentDiff, }: { reactOutputComponentDiff: string }) { const [isDiffCollapsed, toggleCollapseHtml] = useState(true) const htmlComponents = useMemo(() => { const componentStacks: React.ReactNode[] = [] const reactComponentDiffLines = reactOutputComponentDiff.split('\n') reactComponentDiffLines.forEach((line, index) => { const isDiffLine = line[0] === '+' || line[0] === '-' const isHighlightedLine = line[0] === '>' const hasSign = isDiffLine || isHighlightedLine const sign = hasSign ? line[0] : '' const signIndex = hasSign ? line.indexOf(sign) : -1 const [prefix, suffix] = hasSign ? [line.slice(0, signIndex), line.slice(signIndex + 1)] : [line, ''] if (isDiffLine) { componentStacks.push( {/* Slice 2 spaces for the icon */} {prefix} {sign} {suffix} {'\n'} ) } else { // In general, if it's not collapsed, show the whole diff componentStacks.push( {prefix} {sign} {suffix} {'\n'} ) } }) return componentStacks }, [reactOutputComponentDiff]) return (* * `}*** "Mismatched Text" (red) *
*) }{htmlComponents}