import { Enum } from "@/components/Icons" import { isComplex } from "@/utils/shape" import { EditEnum, NOTIN } from "@codec-components" import { useContext, useLayoutEffect, useState } from "react" import { Portal } from "react-portal" import { scrollToMarker } from "../../common/scroll" import { useSubtreeFocus } from "../../common/SubtreeFocus" import { ChildrenProviders, ItemTitle, TitleContext, useReportBinaryStatus, } from "./codec-components" import { setHovered } from "@/codec-components/common/paths.state" export const CEnum: EditEnum = ({ value, inner, path, shape, type, encodedValue, onValueChanged, decode, }) => { const className = "text-slate-500 hover:text-polkadot-500 cursor-pointer whitespace-nowrap [&:not(:first-child)]:ml-1" const titleContainer = useContext(TitleContext) const titleElement = useAppendTitle(titleContainer, className) useReportBinaryStatus(type, encodedValue, onValueChanged, decode) // We create a title element for the children below if we don't have a parent. const [newElement, setNewElement] = useState(null) const focus = useSubtreeFocus() const sub = focus.getNextPath(path) if (sub) { return inner } if (value === NOTIN) return null const innerPath = [...path, value.type] const pathStr = path.join(".") if (titleContainer) { return ( <> {titleElement ? ( scrollToMarker(innerPath)} onMouseEnter={() => setHovered({ id: pathStr, hover: true })} onMouseLeave={() => setHovered({ id: pathStr, hover: false })} > / {value.type} ) : null} {}} > {inner} ) } const innerShape = shape.value[value.type] const innerEntry = innerShape.type === "lookupEntry" ? innerShape.value : innerShape const innerIsComplex = isComplex(innerEntry.type) return (
scrollToMarker(path)} onZoom={innerIsComplex ? () => focus.setFocus(innerPath) : undefined} binaryStatus={{ encodedValue, onValueChanged, decode, type, }} > {value.type} {}} >
{inner}
) } export const useAppendTitle = ( container: HTMLElement | null, className: string, ) => { const [element, setElement] = useState(null) useLayoutEffect(() => { if (!container) return const element = document.createElement("span") container.appendChild(element) setElement(element) return () => element.remove() }, [container]) useLayoutEffect(() => { if (!element) return element.className = className ?? "" }, [element, className]) return element }