Files
qapi/src/codec-components/EditCodec/Tree/CEnum.tsx
T
2024-11-06 13:42:12 +02:00

121 lines
3.3 KiB
TypeScript

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<HTMLElement | null>(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 ? (
<Portal node={titleElement}>
<span
onClick={() => scrollToMarker(innerPath)}
onMouseEnter={() => setHovered({ id: pathStr, hover: true })}
onMouseLeave={() => setHovered({ id: pathStr, hover: false })}
>
/ {value.type}
</span>
</Portal>
) : null}
<ChildrenProviders
titleElement={titleContainer ?? newElement}
onValueChange={() => {}}
>
{inner}
</ChildrenProviders>
</>
)
}
const innerShape = shape.value[value.type]
const innerEntry =
innerShape.type === "lookupEntry" ? innerShape.value : innerShape
const innerIsComplex = isComplex(innerEntry.type)
return (
<div className="before:absolute before:border-l before:h-4 before:border-polkadot-700">
<ItemTitle
icon={Enum}
path={path.join(".")}
titleRef={setNewElement}
onNavigate={() => scrollToMarker(path)}
onZoom={innerIsComplex ? () => focus.setFocus(innerPath) : undefined}
binaryStatus={{
encodedValue,
onValueChanged,
decode,
type,
}}
>
{value.type}
</ItemTitle>
<ChildrenProviders
titleElement={titleContainer ?? newElement}
onValueChange={() => {}}
>
<div className="pl-6">{inner}</div>
</ChildrenProviders>
</div>
)
}
export const useAppendTitle = (
container: HTMLElement | null,
className: string,
) => {
const [element, setElement] = useState<HTMLElement | null>(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
}