diff --git a/src/components/icons/binary.svg b/src/components/icons/binary.svg index f7750a2..7c53c3f 100644 --- a/src/components/icons/binary.svg +++ b/src/components/icons/binary.svg @@ -1,4 +1,4 @@ - + diff --git a/src/components/withSuspense.tsx b/src/components/withSuspense.tsx new file mode 100644 index 0000000..0a8caac --- /dev/null +++ b/src/components/withSuspense.tsx @@ -0,0 +1,17 @@ +import { Subscribe } from "@react-rxjs/core" +import { ComponentType, ReactNode } from "react" +import { Observable } from "rxjs" + +export const withSubscribe = + ( + Component: ComponentType, + options: Partial<{ + source$: Observable + fallback: ReactNode + }> = {}, + ) => + (props: T) => ( + + + + ) diff --git a/src/pages/Extrinsics/EditMode.tsx b/src/pages/Extrinsics/EditMode.tsx index 1204028..742d5af 100644 --- a/src/pages/Extrinsics/EditMode.tsx +++ b/src/pages/Extrinsics/EditMode.tsx @@ -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(null) const listRef = useRef(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({ - type: CodecComponentType.Initial, - value: initialValue, - }) const [focusingSubtree, setFocusingSubtree] = useState(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 ( -
- +
- {!innerValue.value ? ( + {!value.value ? (
(Empty)
) : ( @@ -95,7 +62,7 @@ export const EditMode: React.FC<{
-
+
@@ -105,43 +72,3 @@ export const EditMode: React.FC<{
) } - -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 ( -
-
- -
- {hex ? ( - hex.slice(0, displayLength) + (exceedsDisplayLength ? "…" : "") - ) : ( -
- Cannot display hex. Complete required values shown by{" "} - - (incomplete), or - - (partially incomplete) -
- )} -
-
-
- ) -} diff --git a/src/pages/Extrinsics/Extrinsics.tsx b/src/pages/Extrinsics/Extrinsics.tsx index c020f84..d82d52f 100644 --- a/src/pages/Extrinsics/Extrinsics.tsx +++ b/src/pages/Extrinsics/Extrinsics.tsx @@ -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
Loading metadata...
- } + const [componentValue, setComponentValue] = useState({ + type: CodecComponentType.Initial, + value: + "0x630b040101010031323334353637383934353631363531363531363531363531363531353631350100000108000100002c00000000080000000000", + }) + const binaryValue = + (componentValue.type === CodecComponentType.Initial + ? componentValue.value + : componentValue.value.empty + ? null + : componentValue.value.encoded) ?? null return (
Extrinsics
+ + { + try { + return decoder(value) + } catch (_) { + return NOTIN + } + }} + onValueChanged={(decoded) => { + setComponentValue({ + type: CodecComponentType.Updated, + value: { empty: false, decoded }, + }) + return true + }} + /> + + setComponentValue({ type: CodecComponentType.Updated, value }) + } />
) +}) + +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 ( +
+
+ +
+ {hex ? ( + hex.slice(0, displayLength) + (exceedsDisplayLength ? "…" : "") + ) : ( +
+ {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."} +
+ )} +
+ setBinaryOpen(true)} + /> + setBinaryOpen(false)} + /> +
+
+ ) }