Move binary display out from edit mode, add binary edit button to extrinsic
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<svg width="33" height="21" viewBox="0 0 33 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<svg width="33" height="21" viewBox="0 0 33 21" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<rect x="0.75" y="1.25" width="31.5" height="18.5" rx="3.25" stroke="currentColor" stroke-width="1.5"/>
|
||||
<path d="M8.888 15.708C8.072 15.708 7.35467 15.5133 6.736 15.124C6.12267 14.7347 5.64533 14.148 5.304 13.364C4.96267 12.58 4.792 11.5933 4.792 10.404C4.792 9.22 4.96267 8.236 5.304 7.452C5.64533 6.668 6.12267 6.084 6.736 5.7C7.35467 5.316 8.072 5.124 8.888 5.124C9.69867 5.124 10.4107 5.316 11.024 5.7C11.6373 6.084 12.1147 6.668 12.456 7.452C12.8027 8.236 12.976 9.22 12.976 10.404C12.976 11.5933 12.8027 12.58 12.456 13.364C12.1147 14.148 11.6373 14.7347 11.024 15.124C10.4107 15.5133 9.69867 15.708 8.888 15.708ZM8.888 14.308C9.69867 14.308 10.32 13.972 10.752 13.3C11.1893 12.6227 11.408 11.6573 11.408 10.404C11.408 9.156 11.1893 8.19333 10.752 7.516C10.32 6.83867 9.69867 6.5 8.888 6.5C8.07733 6.5 7.45333 6.83867 7.016 7.516C6.584 8.19333 6.368 9.156 6.368 10.404C6.368 11.6573 6.584 12.6227 7.016 13.3C7.45333 13.972 8.07733 14.308 8.888 14.308ZM14.1096 15.5L17.0616 11.66L14.1096 7.78H15.9736L18.0456 10.508L20.0216 7.78H21.8776L18.9256 11.66L21.8696 15.5H20.0136L17.9336 12.764L15.9736 15.5H14.1096ZM22.6744 14.604C22.6744 14.3053 22.7757 14.052 22.9784 13.844C23.1864 13.636 23.4477 13.532 23.7624 13.532C24.077 13.532 24.3437 13.636 24.5624 13.844C24.781 14.052 24.8904 14.3053 24.8904 14.604C24.8904 14.8973 24.781 15.1453 24.5624 15.348C24.3437 15.5453 24.077 15.644 23.7624 15.644C23.4477 15.644 23.1864 15.5427 22.9784 15.34C22.7757 15.1373 22.6744 14.892 22.6744 14.604ZM25.7838 14.604C25.7838 14.3053 25.8851 14.052 26.0878 13.844C26.2958 13.636 26.5571 13.532 26.8718 13.532C27.1864 13.532 27.4531 13.636 27.6718 13.844C27.8904 14.052 27.9998 14.3053 27.9998 14.604C27.9998 14.8973 27.8904 15.1453 27.6718 15.348C27.4531 15.5453 27.1864 15.644 26.8718 15.644C26.5571 15.644 26.2958 15.5427 26.0878 15.34C25.8851 15.1373 25.7838 14.892 25.7838 14.604Z" fill="currentColor"/>
|
||||
</svg>
|
||||
|
||||
|
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
@@ -0,0 +1,17 @@
|
||||
import { Subscribe } from "@react-rxjs/core"
|
||||
import { ComponentType, ReactNode } from "react"
|
||||
import { Observable } from "rxjs"
|
||||
|
||||
export const withSubscribe =
|
||||
<T extends object>(
|
||||
Component: ComponentType<T>,
|
||||
options: Partial<{
|
||||
source$: Observable<unknown>
|
||||
fallback: ReactNode
|
||||
}> = {},
|
||||
) =>
|
||||
(props: T) => (
|
||||
<Subscribe {...options}>
|
||||
<Component {...props} />
|
||||
</Subscribe>
|
||||
)
|
||||
@@ -1,25 +1,23 @@
|
||||
import { FC, useEffect, useRef, useState } from "react"
|
||||
import { metadata, Binary, HexString } from "@polkadot-api/substrate-bindings"
|
||||
import { EditCodec } from "./EditCodec"
|
||||
import { TreeCodec } from "./EditCodec/Tree/index"
|
||||
import {
|
||||
CodecComponentValue,
|
||||
CodecComponentType,
|
||||
CodecComponentUpdate,
|
||||
CodecComponentValue,
|
||||
MetadataType,
|
||||
} from "@codec-components"
|
||||
import { SubtreeFocus } from "./common/SubtreeFocus"
|
||||
import { Circle } from "lucide-react"
|
||||
import { synchronizeScroll } from "./common/scroll"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import { MarkersContextProvider, VisibleWindow } from "./common/Markers"
|
||||
import { CopyText } from "@/components/Copy"
|
||||
import { synchronizeScroll } from "./common/scroll"
|
||||
import { SubtreeFocus } from "./common/SubtreeFocus"
|
||||
import { EditCodec } from "./EditCodec"
|
||||
import { TreeCodec } from "./EditCodec/Tree/index"
|
||||
import { FocusPath } from "./FocusPath"
|
||||
|
||||
export const EditMode: React.FC<{
|
||||
codecType: number
|
||||
metadata: MetadataType
|
||||
initialValue?: HexString
|
||||
}> = ({ codecType, initialValue, metadata: metadataDecoded }) => {
|
||||
value: CodecComponentValue
|
||||
onUpdate: (value: CodecComponentUpdate) => void
|
||||
}> = (props) => {
|
||||
const { metadata, codecType, value } = props
|
||||
const treeRef = useRef<HTMLDivElement | null>(null)
|
||||
const listRef = useRef<HTMLDivElement | null>(null)
|
||||
|
||||
@@ -28,43 +26,12 @@ export const EditMode: React.FC<{
|
||||
return synchronizeScroll(listRef.current, treeRef.current)
|
||||
}, [])
|
||||
|
||||
// interior X1 AccountId doesn't mark as valid??
|
||||
|
||||
const [innerValue, setInnerValue] = useState<CodecComponentValue>({
|
||||
type: CodecComponentType.Initial,
|
||||
value: initialValue,
|
||||
})
|
||||
const [focusingSubtree, setFocusingSubtree] = useState<string[] | null>(null)
|
||||
|
||||
const props = {
|
||||
metadata: metadataDecoded as any,
|
||||
codecType: codecType,
|
||||
value: innerValue,
|
||||
onUpdate: (value: CodecComponentUpdate) => {
|
||||
setInnerValue({ type: CodecComponentType.Updated, value })
|
||||
},
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setInnerValue({
|
||||
type: CodecComponentType.Initial,
|
||||
value: initialValue,
|
||||
})
|
||||
}, [metadata, codecType])
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-start w-full max-w-screen-lg text-polkadot-100 overflow-hidden bg-polkadot-900">
|
||||
<BinaryDisplay
|
||||
value={
|
||||
(innerValue.type === CodecComponentType.Initial
|
||||
? innerValue.value
|
||||
: innerValue.value.empty
|
||||
? null
|
||||
: innerValue.value.encoded) ?? null
|
||||
}
|
||||
/>
|
||||
<div className="flex flex-col items-start overflow-hidden">
|
||||
<FocusPath
|
||||
metadata={metadataDecoded as any}
|
||||
metadata={metadata}
|
||||
typeId={codecType}
|
||||
value={focusingSubtree}
|
||||
onFocus={setFocusingSubtree}
|
||||
@@ -86,7 +53,7 @@ export const EditMode: React.FC<{
|
||||
className="w-96 sticky top-0 pl-2 pb-16 leading-loose overflow-hidden max-sm:hidden"
|
||||
>
|
||||
<div className="relative">
|
||||
{!innerValue.value ? (
|
||||
{!value.value ? (
|
||||
<div className="text-sm text-slate-400">(Empty)</div>
|
||||
) : (
|
||||
<TreeCodec {...props} />
|
||||
@@ -95,7 +62,7 @@ export const EditMode: React.FC<{
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex-1">
|
||||
<div className="p-2 rounded bg-[#041424]">
|
||||
<div className="p-2 rounded bg-polkadot-900">
|
||||
<EditCodec {...props} />
|
||||
</div>
|
||||
</div>
|
||||
@@ -105,43 +72,3 @@ export const EditMode: React.FC<{
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const DISPLAY_MAX_LEN = 1024
|
||||
const COPY_MAX_LEN = 5 * 1024 * 1024
|
||||
const BinaryDisplay: FC<{ value: Uint8Array | HexString | null }> = ({
|
||||
value,
|
||||
}) => {
|
||||
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">
|
||||
{hex ? (
|
||||
hex.slice(0, displayLength) + (exceedsDisplayLength ? "…" : "")
|
||||
) : (
|
||||
<div className="flex flex-row items-center gap-1">
|
||||
<span>Cannot display hex. Complete required values shown by</span>{" "}
|
||||
<Circle size={8} strokeWidth={4} className="text-red-600" />
|
||||
(incomplete), or
|
||||
<Circle size={8} strokeWidth={4} className="text-orange-600" />
|
||||
(partially incomplete)
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,19 @@
|
||||
import { metadata$ } from "@/chain.state"
|
||||
import { CopyText } from "@/components/Copy"
|
||||
import { BinaryEdit } from "@/components/Icons"
|
||||
import { withSubscribe } from "@/components/withSuspense"
|
||||
import {
|
||||
CodecComponentType,
|
||||
CodecComponentValue,
|
||||
NOTIN,
|
||||
} from "@codec-components"
|
||||
import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders"
|
||||
import { Binary, HexString } from "@polkadot-api/substrate-bindings"
|
||||
import { state, useStateObservable } from "@react-rxjs/core"
|
||||
import { FC, useState } from "react"
|
||||
import { map } from "rxjs"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { BinaryEditModal } from "./BinaryEditModal"
|
||||
import { EditMode } from "./EditMode"
|
||||
|
||||
const extrinsicProps$ = state(
|
||||
@@ -14,23 +27,125 @@ const extrinsicProps$ = state(
|
||||
metadata.extrinsic.type,
|
||||
})),
|
||||
),
|
||||
null,
|
||||
)
|
||||
const extrinsicDecoder = extrinsicProps$.pipeState(
|
||||
map(
|
||||
({ metadata, codecType }) =>
|
||||
getDynamicBuilder(getLookupFn(metadata)).buildDefinition(codecType).dec,
|
||||
),
|
||||
)
|
||||
|
||||
export const Extrinsics = () => {
|
||||
export const Extrinsics = withSubscribe(() => {
|
||||
const extrinsicProps = useStateObservable(extrinsicProps$)
|
||||
const decoder = useStateObservable(extrinsicDecoder)
|
||||
|
||||
if (!extrinsicProps) {
|
||||
return <div>Loading metadata...</div>
|
||||
}
|
||||
const [componentValue, setComponentValue] = useState<CodecComponentValue>({
|
||||
type: CodecComponentType.Initial,
|
||||
value:
|
||||
"0x630b040101010031323334353637383934353631363531363531363531363531363531353631350100000108000100002c00000000080000000000",
|
||||
})
|
||||
const binaryValue =
|
||||
(componentValue.type === CodecComponentType.Initial
|
||||
? componentValue.value
|
||||
: componentValue.value.empty
|
||||
? null
|
||||
: componentValue.value.encoded) ?? null
|
||||
|
||||
return (
|
||||
<div className="flex flex-col overflow-hidden">
|
||||
<div>Extrinsics</div>
|
||||
|
||||
<BinaryDisplay
|
||||
value={binaryValue}
|
||||
isEmpty={
|
||||
componentValue.type === CodecComponentType.Initial &&
|
||||
componentValue.value == null
|
||||
}
|
||||
decode={(value) => {
|
||||
try {
|
||||
return decoder(value)
|
||||
} catch (_) {
|
||||
return NOTIN
|
||||
}
|
||||
}}
|
||||
onValueChanged={(decoded) => {
|
||||
setComponentValue({
|
||||
type: CodecComponentType.Updated,
|
||||
value: { empty: false, decoded },
|
||||
})
|
||||
return true
|
||||
}}
|
||||
/>
|
||||
|
||||
<EditMode
|
||||
{...extrinsicProps}
|
||||
initialValue="0x630b040101010031323334353637383934353631363531363531363531363531363531353631350100000108000100002c00000000080000000000"
|
||||
value={componentValue}
|
||||
onUpdate={(value) =>
|
||||
setComponentValue({ type: CodecComponentType.Updated, value })
|
||||
}
|
||||
/>
|
||||
</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>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user