feat: highlight binary sections that are hovered

This commit is contained in:
Victor Oliva
2024-10-26 20:02:45 +02:00
parent 9138617922
commit 0e62be8a03
10 changed files with 326 additions and 77 deletions
@@ -0,0 +1,36 @@
import { EditEnum, NOTIN } from "@codec-components"
import { u8 } from "@polkadot-api/substrate-bindings"
import { useStateObservable } from "@react-rxjs/core"
import { isActive$ } from "../common/paths.state"
import { useSubtreeFocus } from "../common/SubtreeFocus"
import {
headerHighlight,
highlight,
MissingData,
toConcatHex,
} from "./codec-components"
export const CEnum: EditEnum = ({ value, inner, path, shape }) => {
const isActive = useStateObservable(isActive$(path.join(".")))
const focus = useSubtreeFocus()
const sub = focus.getNextPath(path)
if (sub) {
return inner
}
if (value === NOTIN) return <MissingData />
const entry = shape.value[value.type]
if (!entry) {
console.log(shape.value, value.type)
throw new Error("Type not found??")
}
return (
<span className={highlight(isActive)}>
<span className={headerHighlight(isActive)}>
{toConcatHex(u8.enc(entry.idx))}
</span>
{inner}
</span>
)
}
@@ -0,0 +1,28 @@
import { EditStruct, NOTIN } from "@codec-components"
import { useStateObservable } from "@react-rxjs/core"
import React from "react"
import { isActive$ } from "../common/paths.state"
import { useSubtreeFocus } from "../common/SubtreeFocus"
import { highlight, MissingData } from "./codec-components"
export const CStruct: EditStruct = ({ innerComponents, value, path }) => {
const isActive = useStateObservable(isActive$(path.join(".")))
const focus = useSubtreeFocus()
const sub = focus.getNextPath(path)
if (sub) {
const field = Object.entries(innerComponents).find(([key]) => key === sub)
return field?.[1]
}
if (value === NOTIN) {
return <MissingData />
}
return (
<span className={highlight(isActive)}>
{Object.entries(innerComponents).map(([key, jsx]) => (
<React.Fragment key={key}>{jsx}</React.Fragment>
))}
</span>
)
}
@@ -0,0 +1,60 @@
import {
EditArray,
EditComplexCodecComponentProps,
EditSequence,
EditTuple,
NOTIN,
} from "@codec-components"
import { compact } from "@polkadot-api/substrate-bindings"
import { useStateObservable } from "@react-rxjs/core"
import React, { ReactNode } from "react"
import { isActive$ } from "../common/paths.state"
import { useSubtreeFocus } from "../common/SubtreeFocus"
import {
headerHighlight,
highlight,
MissingData,
toConcatHex,
} from "./codec-components"
export const CSequence: EditSequence = (props) => <ListDisplay {...props} />
export const CArray: EditArray = (props) => (
<ListDisplay {...props} fixedLength />
)
export const CTuple: EditTuple = (props) => (
<ListDisplay {...props} fixedLength />
)
export const ListDisplay: React.FC<
EditComplexCodecComponentProps<unknown[]> & {
innerComponents: ReactNode[]
fixedLength?: boolean
}
> = (props) => {
const { innerComponents, fixedLength, value, path } = props
const isActive = useStateObservable(isActive$(path.join(".")))
const focus = useSubtreeFocus()
const sub = focus.getNextPath(path)
if (sub) {
return innerComponents[Number(sub)]
}
if (value === NOTIN) {
return <MissingData />
}
return (
<span className={highlight(isActive)}>
{fixedLength ? null : (
<span className={headerHighlight(isActive)}>
{toConcatHex(compact.enc(value.length))}
</span>
)}
{innerComponents.map((jsx, idx) => (
<React.Fragment key={idx}>{jsx}</React.Fragment>
))}
</span>
)
}
@@ -0,0 +1,87 @@
import {
EditAccountId,
EditBigNumber,
EditBool,
EditBytes,
EditEthAccount,
EditNumber,
EditOption,
EditPrimitiveComponentProps,
EditResult,
EditStr,
EditVoid,
NOTIN,
} from "@codec-components"
import { u8 } from "@polkadot-api/substrate-bindings"
import { toHex } from "@polkadot-api/utils"
import { useStateObservable } from "@react-rxjs/core"
import { FC } from "react"
import { isActive$ } from "../common/paths.state"
import { useSubtreeFocus } from "../common/SubtreeFocus"
export const CVoid: EditVoid = () => null
const CPrimitive: FC<EditPrimitiveComponentProps<any>> = ({
encodedValue,
path,
}) => {
const isActive = useStateObservable(isActive$(path.join(".")))
return encodedValue ? (
<span className={highlight(isActive)}>
<span className={headerHighlight(isActive)}>
{toConcatHex(encodedValue)}
</span>
</span>
) : (
<MissingData />
)
}
export const CBool: EditBool = CPrimitive
export const CStr: EditStr = CPrimitive
export const CEthAccount: EditEthAccount = CPrimitive
export const CBigNumber: EditBigNumber = CPrimitive
export const CNumber: EditNumber = CPrimitive
export const CAccountId: EditAccountId = CPrimitive
export const CBytes: EditBytes = CPrimitive
export const COption: EditOption = ({ path, value, inner }) => {
const focus = useSubtreeFocus()
const sub = focus.getNextPath(path)
if (sub) {
return inner
}
if (value === NOTIN) return <MissingData />
// TODO Looking at scale-ts, Option<Boolean> seems to be 00, 01 or 02! (None, Some(true), Some(false))
// but can't find it at the docs? https://docs.substrate.io/reference/scale-codec/
return (
<span>
{toConcatHex(u8.enc(value ? 1 : 0))}
{inner}
</span>
)
}
export const CResult: EditResult = ({ value, inner, path }) => {
const focus = useSubtreeFocus()
const sub = focus.getNextPath(path)
if (sub) return inner
if (value === NOTIN) return <MissingData />
return (
<span>
{toConcatHex(u8.enc(value.success ? 0 : 1))}
{inner}
</span>
)
}
export const MissingData = () => (
<span className="mx-0.5 text-slate-400 text-sm">(…)</span>
)
export const toConcatHex = (value: Uint8Array) => toHex(value).slice(2)
export const highlight = (isActive: boolean) =>
isActive ? "text-polkadot-400 mx-1" : ""
export const headerHighlight = (isActive: boolean) =>
isActive ? "text-polkadot-500" : ""
@@ -0,0 +1,4 @@
export * from "./CEnum"
export * from "./CStruct"
export * from "./codec-components"
export * from "./ListComponents"
@@ -0,0 +1,5 @@
import { getCodecComponent } from "@codec-components"
import * as treeComponents from "./components"
export const BinaryViewCodec = getCodecComponent(treeComponents)
+1
View File
@@ -180,6 +180,7 @@ const BinaryEditModalContent: FC<
!isValid && "opacity-50",
)}
onClick={submit}
disabled={!isValid}
>
OK
</button>
+18 -6
View File
@@ -1,12 +1,24 @@
import { FC } from "react"
import { ComponentProps, FC } from "react"
import { ChevronRight } from "lucide-react"
import { twMerge as clsx } from "tailwind-merge"
import { twMerge } from "tailwind-merge"
export const ExpandBtn: FC<{
expanded: boolean
}> = ({ expanded }) => (
export const ExpandBtn: FC<
{
expanded: boolean
direction?: "horizontal" | "vertical"
} & ComponentProps<typeof ChevronRight>
> = ({ expanded, direction = "horizontal", ...props }) => (
<ChevronRight
size={16}
className={clsx("transition-transform mt-[1px]", expanded && "rotate-90")}
{...props}
className={twMerge(
"transition-transform mt-[1px]",
direction === "horizontal"
? expanded && "rotate-90"
: expanded
? "-rotate-90"
: "rotate-90",
props.className,
)}
/>
)
+76 -42
View File
@@ -1,57 +1,91 @@
import { BinaryViewCodec } from "@/codec-components/BinaryViewCodec"
import { BinaryEditButton } from "@/components/BinaryEditButton"
import { CopyText } from "@/components/Copy"
import { NOTIN } from "@codec-components"
import { ExpandBtn } from "@/components/Expand"
import {
CodecComponentType,
CodecComponentUpdate,
CodecComponentValue,
MetadataType,
NOTIN,
} from "@codec-components"
import { Binary, HexString } from "@polkadot-api/substrate-bindings"
import { FC } from "react"
import { toHex } from "@polkadot-api/utils"
import { FC, useState } from "react"
import { twMerge } from "tailwind-merge"
const DISPLAY_MAX_LEN = 1024
const COPY_MAX_LEN = 5 * 1024 * 1024
export const BinaryDisplay: FC<{
value: Uint8Array | HexString | null
isEmpty: boolean
onValueChanged: (value: any | NOTIN) => boolean
decode: (value: Uint8Array | HexString) => any | NOTIN
}> = ({ value, isEmpty, onValueChanged, decode }) => {
const displayLength = DISPLAY_MAX_LEN * 2 + 2
const copyLength = COPY_MAX_LEN * 2 + 2
const hex = value
? typeof value === "string"
? value.slice(0, copyLength)
: Binary.fromBytes(value.slice(0, COPY_MAX_LEN)).asHex()
: null
const exceedsDisplayLength = hex && hex.length > displayLength
const exceedsCopyLength = !!hex && hex.length >= COPY_MAX_LEN * 2 + 2
const copyDisabled = hex === null || exceedsCopyLength
codecType: number
metadata: MetadataType
value: CodecComponentValue
onUpdate: (value: CodecComponentUpdate) => void
codec: {
enc: (value: any | NOTIN) => Uint8Array
dec: (value: Uint8Array | HexString) => any | NOTIN
}
}> = ({ codecType, metadata, value, onUpdate, codec }) => {
const [wrap, setWrap] = useState(false)
const encoded = (() => {
if (value.type === CodecComponentType.Initial) {
if (!value.value) return null
return typeof value.value === "string"
? Binary.fromHex(value.value).asBytes()
: value.value
}
if (value.value.empty || !value.value.encoded) return null
return value.value.encoded
})()
const hex = encoded ? toHex(encoded) : null
const isEmpty =
(value.type === CodecComponentType.Initial && !value.value) ||
(value.type === CodecComponentType.Updated && value.value.empty)
return (
<div className="px-2 w-full">
<div className="px-3 py-2 gap-2 rounded flex flex-row items-center bg-polkadot-800">
<CopyText text={hex ?? ""} disabled={copyDisabled} />
<div className="text-sm tabular-nums max-h-12 overflow-hidden whitespace-nowrap text-ellipsis flex-1">
{hex ? (
hex.slice(0, displayLength) + (exceedsDisplayLength ? "…" : "")
) : (
<div className="px-3 py-2 gap-2 rounded flex flex-row bg-polkadot-800 items-start">
<CopyText text={hex ?? ""} disabled={!hex} className="h-5" />
<div
className={twMerge(
"text-sm tabular-nums overflow-hidden flex-1",
wrap ? "break-words" : "whitespace-nowrap text-ellipsis h-5",
)}
>
{isEmpty ? (
<div className="flex flex-row items-center gap-1 text-slate-400">
{isEmpty
? "Start by filling out your extrinsic, or enter a binary using the edit binary button at the end of this line."
: "Cannot display hex. Complete the required values."}
Start by filling out your extrinsic, or enter a binary using the
edit binary button at the end of this line.
</div>
) : (
<>
0x
<BinaryViewCodec
codecType={codecType}
metadata={metadata}
value={value}
/>
</>
)}
</div>
<BinaryEditButton
initialValue={
typeof value === "string"
? Binary.fromHex(value).asBytes()
: (value ?? undefined)
}
onValueChange={onValueChanged}
decode={decode}
iconProps={{
size: 24,
}}
/>
<div className="flex gap-2 items-center h-5">
<ExpandBtn
expanded={wrap}
direction="vertical"
className="cursor-pointer"
onClick={() => setWrap((v) => !v)}
/>
<BinaryEditButton
initialValue={encoded ?? undefined}
onValueChange={(decoded) => {
const encoded = codec.enc(decoded)
onUpdate({ empty: false, decoded, encoded })
return true
}}
decode={codec.dec}
iconProps={{
size: 24,
}}
/>
</div>
</div>
</div>
)
+11 -29
View File
@@ -2,11 +2,7 @@ import { metadata$ } from "@/chain.state"
import { ActionButton } from "@/components/ActionButton"
import { ButtonGroup } from "@/components/ButtonGroup"
import { withSubscribe } from "@/components/withSuspense"
import {
CodecComponentType,
CodecComponentValue,
NOTIN,
} from "@codec-components"
import { CodecComponentType, CodecComponentValue } from "@codec-components"
import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders"
import { Binary } from "@polkadot-api/substrate-bindings"
import { state, useStateObservable } from "@react-rxjs/core"
@@ -28,17 +24,16 @@ const extrinsicProps$ = state(
})),
),
)
const extrinsicDecoder = extrinsicProps$.pipeState(
map(
({ metadata, codecType }) =>
getDynamicBuilder(getLookupFn(metadata)).buildDefinition(codecType).dec,
const extrinsicCodec = extrinsicProps$.pipeState(
map(({ metadata, codecType }) =>
getDynamicBuilder(getLookupFn(metadata)).buildDefinition(codecType),
),
)
export const Extrinsics = withSubscribe(() => {
const [viewMode, setViewMode] = useState<"edit" | "json">("edit")
const extrinsicProps = useStateObservable(extrinsicProps$)
const decoder = useStateObservable(extrinsicDecoder)
const codec = useStateObservable(extrinsicCodec)
const [componentValue, setComponentValue] = useState<CodecComponentValue>({
type: CodecComponentType.Initial,
@@ -57,25 +52,12 @@ export const Extrinsics = withSubscribe(() => {
<div>Extrinsics</div>
<BinaryDisplay
value={binaryValue}
isEmpty={
componentValue.type === CodecComponentType.Initial &&
componentValue.value == null
{...extrinsicProps}
value={componentValue}
codec={codec}
onUpdate={(value) =>
setComponentValue({ type: CodecComponentType.Updated, value })
}
decode={(value) => {
try {
return decoder(value)
} catch (_) {
return NOTIN
}
}}
onValueChanged={(decoded) => {
setComponentValue({
type: CodecComponentType.Updated,
value: { empty: false, decoded },
})
return true
}}
/>
<div className="flex flex-row justify-between px-2">
@@ -112,7 +94,7 @@ export const Extrinsics = withSubscribe(() => {
? Binary.fromHex(binaryValue).asBytes()
: binaryValue
}
decode={decoder}
decode={codec.dec}
/>
)}
</div>