diff --git a/src/codec-components/LookupTypeEdit/LookupTypeEdit.tsx b/src/codec-components/LookupTypeEdit/LookupTypeEdit.tsx index 49166af..7a9f2c7 100644 --- a/src/codec-components/LookupTypeEdit/LookupTypeEdit.tsx +++ b/src/codec-components/LookupTypeEdit/LookupTypeEdit.tsx @@ -3,6 +3,7 @@ import { byteArraysAreEqual } from "@/utils/byteArray" import { CodecComponentType, CodecComponentValue } from "@codec-components" import { getDynamicBuilder } from "@polkadot-api/metadata-builders" import { state, useStateObservable } from "@react-rxjs/core" +import { Codec } from "polkadot-api" import { ComponentProps, FC, useEffect, useRef, useState } from "react" import { map } from "rxjs" import { @@ -14,8 +15,8 @@ import { synchronizeScroll } from "../common/scroll" import { SubtreeFocus } from "../common/SubtreeFocus" import { EditCodec } from "../EditCodec" import { TreeCodec } from "../EditCodec/Tree" -import { FocusPath } from "./FocusPath" import { BinaryDisplay } from "./BinaryDisplay" +import { FocusPath } from "./FocusPath" const editTypeMetadataProps$ = state( lookup$.pipe( @@ -31,22 +32,93 @@ export const LookupTypeEdit: FC<{ type: number value: Uint8Array | "partial" | null onValueChange: (value: Uint8Array | "partial" | null) => void -}> = ({ type, value, onValueChange }) => { + tree?: boolean +}> = ({ type, value, onValueChange, tree = true }) => { const treeRef = useRef(null) const listRef = useRef(null) - const [componentValue, setComponentValue] = useState({ - type: CodecComponentType.Initial, - value: value instanceof Uint8Array ? value : undefined, - }) const [focusingSubtree, setFocusingSubtree] = useState(null) - const metadataProps = useStateObservable(editTypeMetadataProps$) - const codec = metadataProps?.builder.buildDefinition(type) useEffect(() => { if (!listRef.current || !treeRef.current) return return synchronizeScroll(listRef.current, treeRef.current) }, []) + const codecProps = useCodecProps(type, value, onValueChange) + + if (!codecProps) return null + + return ( +
+ + {tree && ( + + )} + {tree && ( +
+ {/* This element is presentational only: Adds a connecting border between the FocusPath and the tree below */} +
+
+ )} + + +
+ {tree && ( +
+
+ + +
+
+ )} +
+
+ + +
+
+
+
+
+
+ ) +} + +export const InlineLookupTypeEdit: FC<{ + type: number + value: Uint8Array | "partial" | null + onValueChange: (value: Uint8Array | "partial" | null) => void +}> = ({ type, value, onValueChange }) => { + const props = useCodecProps(type, value, onValueChange) + if (!props) return null + + return +} + +const useCodecProps = ( + type: number, + value: Uint8Array | "partial" | null, + onValueChange: (value: Uint8Array | "partial" | null) => void, +): (ComponentProps & { codec: Codec }) | null => { + const [componentValue, setComponentValue] = useState({ + type: CodecComponentType.Initial, + value: value instanceof Uint8Array ? value : undefined, + }) + const metadataProps = useStateObservable(editTypeMetadataProps$) + const codec = metadataProps?.builder.buildDefinition(type) + // Synchronize componentValue out to `props.value` useEffect(() => { if (componentValue.type === CodecComponentType.Initial) return @@ -127,7 +199,8 @@ export const LookupTypeEdit: FC<{ if (!metadataProps || !codec) return null - const codecProps: ComponentProps = { + return { + codec, codecType: type, metadata: metadataProps.metadata, value: componentValue, @@ -137,46 +210,4 @@ export const LookupTypeEdit: FC<{ value, }), } - - return ( -
- - -
- {/* This element is presentational only: Adds a connecting border between the FocusPath and the tree below */} -
-
- - -
-
-
- - -
-
-
-
- - -
-
-
-
-
-
- ) } diff --git a/src/components/DocsRenderer.tsx b/src/components/DocsRenderer.tsx new file mode 100644 index 0000000..4978b33 --- /dev/null +++ b/src/components/DocsRenderer.tsx @@ -0,0 +1,28 @@ +import { FC } from "react" +import { twMerge } from "tailwind-merge" + +export const DocsRenderer: FC<{ docs: string[] }> = ({ docs }) => { + if (!docs.length) return null + if (docs.length === 1) { + return ( +
+

{docs[0]}

+
+ ) + } + + const normalizedDocs = + docs.length > 1 ? ["/*", ...docs.map((v) => ` *${v}`), " */"] : docs + return ( +
1 && "text-sm font-mono whitespace-pre", + )} + > + {normalizedDocs.map((d, i) => ( +

{d}

+ ))} +
+ ) +} diff --git a/src/pages/RuntimeCalls/RuntimeCallQuery.tsx b/src/pages/RuntimeCalls/RuntimeCallQuery.tsx index 7a841bb..fb74c45 100644 --- a/src/pages/RuntimeCalls/RuntimeCallQuery.tsx +++ b/src/pages/RuntimeCalls/RuntimeCallQuery.tsx @@ -1,10 +1,15 @@ import { lookup$, unsafeApi$ } from "@/chain.state" -import { LookupTypeEdit } from "@/codec-components/LookupTypeEdit" +import { + InlineLookupTypeEdit, + LookupTypeEdit, +} from "@/codec-components/LookupTypeEdit" import { ActionButton } from "@/components/ActionButton" import { ExpandBtn } from "@/components/Expand" +import { getTypeComplexity } from "@/utils/shape" import { getDynamicBuilder } from "@polkadot-api/metadata-builders" import { state, useStateObservable, withDefault } from "@react-rxjs/core" import { createSignal } from "@react-rxjs/utils" +import { Circle, Dot } from "lucide-react" import { FC, useState } from "react" import { combineLatest, @@ -15,9 +20,8 @@ import { startWith, switchMap, } from "rxjs" -import { addRuntimeCallQuery, selectedEntry$ } from "./runtimeCalls.state" -import { Circle } from "lucide-react" import { twMerge } from "tailwind-merge" +import { addRuntimeCallQuery, selectedEntry$ } from "./runtimeCalls.state" export const RuntimeCallQuery: FC = () => { const selectedEntry = useStateObservable(selectedEntry$) @@ -109,6 +113,7 @@ const inputValue$ = state( (idx: number) => inputValues$.pipe(map((v) => v[idx])), null, ) +const lookupState$ = state(lookup$, null) const RuntimeValueInput: FC<{ idx: number; name: string; type: number }> = ({ idx, type, @@ -116,14 +121,26 @@ const RuntimeValueInput: FC<{ idx: number; name: string; type: number }> = ({ }) => { const value = useStateObservable(inputValue$(idx)) const [expanded, setExpanded] = useState(false) + const lookup = useStateObservable(lookupState$) + + if (!lookup) return null + const shape = lookup(type) + const complexity = getTypeComplexity(shape) return (
  • setExpanded((e) => !e)} > - + {complexity === "inline" ? ( + + ) : ( + + )} = ({ )} />
    {name}
    + {complexity === "inline" ? ( +
    + setInputValue({ idx, value })} + /> +
    + ) : null}
    - {expanded && ( -
    + {expanded && complexity !== "inline" && ( +
    setInputValue({ idx, value })} + tree={complexity === "tree"} />
    )} diff --git a/src/pages/RuntimeCalls/RuntimeCalls.tsx b/src/pages/RuntimeCalls/RuntimeCalls.tsx index 8431c5e..cc239e9 100644 --- a/src/pages/RuntimeCalls/RuntimeCalls.tsx +++ b/src/pages/RuntimeCalls/RuntimeCalls.tsx @@ -7,6 +7,7 @@ import { map } from "rxjs" import { RuntimeCallQuery } from "./RuntimeCallQuery" import { selectedEntry$, setSelectedMethod } from "./runtimeCalls.state" import { RuntimeCallResults } from "./RuntimeCallResults" +import { DocsRenderer } from "@/components/DocsRenderer" const metadataRuntimeCalls$ = state( lookup$.pipe( @@ -84,11 +85,7 @@ export const RuntimeCalls = withSubscribe(() => { {entry?.docs.length && (
    Docs -
    - {entry.docs.map((d, i) => ( -

    {d}

    - ))} -
    +
    )} diff --git a/src/pages/Storage/Storage.tsx b/src/pages/Storage/Storage.tsx index 7622d45..69be087 100644 --- a/src/pages/Storage/Storage.tsx +++ b/src/pages/Storage/Storage.tsx @@ -9,6 +9,7 @@ import { selectedEntry$, setSelectedEntry } from "./storage.state" import { StorageDecode } from "./StorageDecode" import { StorageQuery } from "./StorageQuery" import { StorageSubscriptions } from "./StorageSubscriptions" +import { DocsRenderer } from "@/components/DocsRenderer" const metadataStorage$ = state( lookup$.pipe( @@ -131,11 +132,7 @@ export const Storage = withSubscribe(() => { {selectedEntry?.docs.length && (
    Docs -
    - {selectedEntry.docs.map((d, i) => ( -

    {d}

    - ))} -
    +
    )} diff --git a/src/utils/shape.ts b/src/utils/shape.ts index b35ddd0..9c62609 100644 --- a/src/utils/shape.ts +++ b/src/utils/shape.ts @@ -62,3 +62,57 @@ export const getFinalType = (shape: any, name: string) => { return innerType.type } } + +type TypeComplexity = "inline" | "multiple" | "tree" +export const getTypeComplexity = (lookupType: Var): TypeComplexity => { + switch (lookupType.type) { + case "array": + case "sequence": + if ( + lookupType.value.type === "primitive" && + lookupType.value.value === "u8" + ) { + return "inline" + } + return maxComplexity("multiple", getTypeComplexity(lookupType.value)) + case "tuple": + return reduceMaxComplexity( + "multiple", + lookupType.value.map((v) => () => getTypeComplexity(v)), + ) + case "enum": + return reduceMaxComplexity( + "inline", + Object.values(lookupType.value).map((v) => () => { + const inner = getTypeComplexity( + v.type === "lookupEntry" ? v.value : v, + ) + return inner === "inline" ? "inline" : "tree" + }), + ) + case "struct": + return reduceMaxComplexity( + "multiple", + Object.values(lookupType.value).map((v) => () => { + const inner = getTypeComplexity(v) + return inner === "inline" ? "inline" : "tree" + }), + ) + case "option": + case "result": + return "multiple" + default: + return "inline" + } +} +const highCx: Array = ["tree", "multiple"] +const maxComplexity = (a: TypeComplexity, b: TypeComplexity) => + highCx.find((c) => a === c || b == c) ?? "inline" +const reduceMaxComplexity = ( + initial: TypeComplexity, + fns: Array<() => TypeComplexity>, +) => + fns.reduce((acc, fn) => { + if (acc === "tree") return acc + return maxComplexity(acc, fn()) + }, initial)