feat: add json view
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { ButtonHTMLAttributes, FC } from "react"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export const ActionButton: FC<ButtonHTMLAttributes<HTMLButtonElement>> = (
|
||||
props,
|
||||
) => (
|
||||
<button
|
||||
{...props}
|
||||
className={twMerge(
|
||||
"text-polkadot-500 border rounded border-polkadot-500 px-2 py-1",
|
||||
"cursor-pointer select-none hover:bg-polkadot-500 hover:text-polkadot-200",
|
||||
props.disabled && "opacity-50 pointer-events-none",
|
||||
props.className,
|
||||
)}
|
||||
/>
|
||||
)
|
||||
@@ -149,14 +149,18 @@ const parseValue = (value: string | Binary): Binary | NOTIN => {
|
||||
return Binary.fromText(value)
|
||||
}
|
||||
|
||||
const textDecoder = new TextDecoder("utf-8", { fatal: true })
|
||||
const serializeValue = (value: Binary): string | Binary => {
|
||||
const bytes = value.asBytes()
|
||||
if (bytes.length === 0) return ""
|
||||
if (bytes.length > 5 * 1024 * 1024) return value
|
||||
|
||||
return bytesToString(value)
|
||||
}
|
||||
|
||||
const textDecoder = new TextDecoder("utf-8", { fatal: true })
|
||||
export const bytesToString = (value: Binary) => {
|
||||
try {
|
||||
return textDecoder.decode(bytes)
|
||||
return textDecoder.decode(value.asBytes())
|
||||
} catch (_) {
|
||||
return value.asHex()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import * as ToggleGroup from "@radix-ui/react-toggle-group"
|
||||
import { FC, ReactNode } from "react"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export const ButtonGroup: FC<{
|
||||
items: Array<{
|
||||
value: string
|
||||
content: ReactNode
|
||||
disabled?: boolean
|
||||
}>
|
||||
value: string
|
||||
onValueChange: (value: string) => void
|
||||
btnWidth?: number
|
||||
}> = ({ items, value, onValueChange, btnWidth }) => {
|
||||
return (
|
||||
<ToggleGroup.Root
|
||||
type="single"
|
||||
value={value}
|
||||
onValueChange={(v) => v && onValueChange(v)}
|
||||
className="inline-flex bg-polkadot-800 rounded"
|
||||
>
|
||||
{items.map(({ value, content, disabled }) => (
|
||||
<ToggleGroup.Item
|
||||
value={value}
|
||||
key={value}
|
||||
className={twMerge(
|
||||
"bg-white text-polkadot-500 ml-[1px] px-2",
|
||||
"first:ml-0 first:rounded-l",
|
||||
"last:rounded-r",
|
||||
"hover:bg-polkadot-100",
|
||||
"data-[state=on]:bg-polkadot-500 data-[state=on]:text-white",
|
||||
disabled && "opacity-50 pointer-events-none",
|
||||
)}
|
||||
disabled={disabled}
|
||||
style={{
|
||||
minWidth: btnWidth,
|
||||
}}
|
||||
>
|
||||
{content}
|
||||
</ToggleGroup.Item>
|
||||
))}
|
||||
</ToggleGroup.Root>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { CopyText } from "@/components/Copy"
|
||||
import { BinaryEdit } from "@/components/Icons"
|
||||
import { NOTIN } from "@codec-components"
|
||||
import { Binary, HexString } from "@polkadot-api/substrate-bindings"
|
||||
import { FC, useState } from "react"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { BinaryEditModal } from "./BinaryEditModal"
|
||||
|
||||
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 [binaryOpen, setBinaryOpen] = useState(false)
|
||||
|
||||
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
|
||||
|
||||
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="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."}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<BinaryEdit
|
||||
size={24}
|
||||
className={twMerge("cursor-pointer hover:text-polkadot-300")}
|
||||
onClick={() => setBinaryOpen(true)}
|
||||
/>
|
||||
<BinaryEditModal
|
||||
status={{
|
||||
encodedValue:
|
||||
typeof value === "string"
|
||||
? Binary.fromHex(value).asBytes()
|
||||
: (value ?? undefined),
|
||||
onValueChanged,
|
||||
decode,
|
||||
type: "complete",
|
||||
}}
|
||||
open={binaryOpen}
|
||||
path=""
|
||||
onClose={() => setBinaryOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { metadata$ } from "@/chain.state"
|
||||
import { CopyText } from "@/components/Copy"
|
||||
import { BinaryEdit } from "@/components/Icons"
|
||||
import { ActionButton } from "@/components/ActionButton"
|
||||
import { ButtonGroup } from "@/components/ButtonGroup"
|
||||
import { withSubscribe } from "@/components/withSuspense"
|
||||
import {
|
||||
CodecComponentType,
|
||||
@@ -8,13 +8,13 @@ import {
|
||||
NOTIN,
|
||||
} from "@codec-components"
|
||||
import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders"
|
||||
import { Binary, HexString } from "@polkadot-api/substrate-bindings"
|
||||
import { Binary } from "@polkadot-api/substrate-bindings"
|
||||
import { state, useStateObservable } from "@react-rxjs/core"
|
||||
import { FC, useState } from "react"
|
||||
import { useState } from "react"
|
||||
import { map } from "rxjs"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { BinaryEditModal } from "./BinaryEditModal"
|
||||
import { BinaryDisplay } from "./BinaryDisplay"
|
||||
import { EditMode } from "./EditMode"
|
||||
import { JsonMode } from "./JsonMode"
|
||||
|
||||
const extrinsicProps$ = state(
|
||||
metadata$.pipe(
|
||||
@@ -36,6 +36,7 @@ const extrinsicDecoder = extrinsicProps$.pipeState(
|
||||
)
|
||||
|
||||
export const Extrinsics = withSubscribe(() => {
|
||||
const [viewMode, setViewMode] = useState<"edit" | "json">("edit")
|
||||
const extrinsicProps = useStateObservable(extrinsicProps$)
|
||||
const decoder = useStateObservable(extrinsicDecoder)
|
||||
|
||||
@@ -52,7 +53,7 @@ export const Extrinsics = withSubscribe(() => {
|
||||
: componentValue.value.encoded) ?? null
|
||||
|
||||
return (
|
||||
<div className="flex flex-col overflow-hidden">
|
||||
<div className="flex flex-col overflow-hidden gap-2">
|
||||
<div>Extrinsics</div>
|
||||
|
||||
<BinaryDisplay
|
||||
@@ -77,75 +78,43 @@ export const Extrinsics = withSubscribe(() => {
|
||||
}}
|
||||
/>
|
||||
|
||||
<EditMode
|
||||
{...extrinsicProps}
|
||||
value={componentValue}
|
||||
onUpdate={(value) =>
|
||||
setComponentValue({ type: CodecComponentType.Updated, value })
|
||||
}
|
||||
/>
|
||||
<div className="flex flex-row justify-between px-2">
|
||||
<ButtonGroup
|
||||
value={viewMode}
|
||||
onValueChange={setViewMode as any}
|
||||
items={[
|
||||
{
|
||||
value: "edit",
|
||||
content: "Edit",
|
||||
},
|
||||
{
|
||||
value: "json",
|
||||
content: "JSON",
|
||||
disabled: !binaryValue,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<ActionButton disabled>Submit extrinsic</ActionButton>
|
||||
</div>
|
||||
|
||||
{viewMode === "edit" ? (
|
||||
<EditMode
|
||||
{...extrinsicProps}
|
||||
value={componentValue}
|
||||
onUpdate={(value) =>
|
||||
setComponentValue({ type: CodecComponentType.Updated, value })
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<JsonMode
|
||||
value={
|
||||
typeof binaryValue === "string"
|
||||
? Binary.fromHex(binaryValue).asBytes()
|
||||
: binaryValue
|
||||
}
|
||||
decode={decoder}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
const DISPLAY_MAX_LEN = 1024
|
||||
const COPY_MAX_LEN = 5 * 1024 * 1024
|
||||
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 [binaryOpen, setBinaryOpen] = useState(false)
|
||||
|
||||
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
|
||||
|
||||
return (
|
||||
<div className="p-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="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."}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<BinaryEdit
|
||||
size={24}
|
||||
className={twMerge("cursor-pointer hover:text-polkadot-300")}
|
||||
onClick={() => setBinaryOpen(true)}
|
||||
/>
|
||||
<BinaryEditModal
|
||||
status={{
|
||||
encodedValue:
|
||||
typeof value === "string"
|
||||
? Binary.fromHex(value).asBytes()
|
||||
: (value ?? undefined),
|
||||
onValueChanged,
|
||||
decode,
|
||||
type: "complete",
|
||||
}}
|
||||
open={binaryOpen}
|
||||
path=""
|
||||
onClose={() => setBinaryOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { bytesToString } from "@/components/BinaryInput"
|
||||
import { Binary } from "@polkadot-api/substrate-bindings"
|
||||
import { FC, useMemo } from "react"
|
||||
import ReactJson from "react18-json-view"
|
||||
import "react18-json-view/src/style.css"
|
||||
import "react18-json-view/src/dark.css"
|
||||
import "./jsonView.css"
|
||||
|
||||
export const JsonMode: FC<{
|
||||
decode: (value: Uint8Array) => unknown
|
||||
value: Uint8Array | null
|
||||
}> = ({ decode, value }) => {
|
||||
const src = useMemo(
|
||||
() => (value ? decode(value) : null),
|
||||
// value
|
||||
// ? JSON.parse(
|
||||
// JSON.stringify(decode(value), (_, v) =>
|
||||
// typeof v === "bigint"
|
||||
// ? `${v}n`
|
||||
// : v instanceof Binary
|
||||
// ? bytesToString(v)
|
||||
// : v,
|
||||
// ),
|
||||
// )
|
||||
// : null,
|
||||
[value, decode],
|
||||
)
|
||||
|
||||
console.log(src)
|
||||
|
||||
return (
|
||||
<div className="overflow-auto p-2 bg-polkadot-900 text-sm">
|
||||
<ReactJson
|
||||
src={src}
|
||||
dark
|
||||
theme="a11y"
|
||||
// customizeNode={({ node }) => {
|
||||
// if (node instanceof Binary) {
|
||||
// return <>{node.asHex()}</>
|
||||
// }
|
||||
// }}
|
||||
replacer={(v) => (v instanceof Binary ? bytesToString(v) : v)}
|
||||
customizeCopy={(v) =>
|
||||
JSON.stringify(
|
||||
v,
|
||||
(_, v) =>
|
||||
typeof v === "bigint"
|
||||
? `${v}n`
|
||||
: v instanceof Binary
|
||||
? bytesToString(v)
|
||||
: v,
|
||||
2,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
.json-view .json-view--copy {
|
||||
opacity: 0.4;
|
||||
}
|
||||
.json-view .json-view--copy:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
.json-view .jv-button {
|
||||
padding: 0 0.3rem;
|
||||
}
|
||||
Reference in New Issue
Block a user