runtime-calls: inline inputs if they are simple

This commit is contained in:
Victor Oliva
2024-10-29 13:24:27 +01:00
parent a785456908
commit 53333623b2
6 changed files with 202 additions and 68 deletions
@@ -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<HTMLDivElement | null>(null)
const listRef = useRef<HTMLDivElement | null>(null)
const [componentValue, setComponentValue] = useState<CodecComponentValue>({
type: CodecComponentType.Initial,
value: value instanceof Uint8Array ? value : undefined,
})
const [focusingSubtree, setFocusingSubtree] = useState<string[] | null>(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 (
<div className="flex flex-col items-start overflow-hidden">
<BinaryDisplay {...codecProps} className="pb-2" />
{tree && (
<FocusPath
metadata={codecProps.metadata}
typeId={type}
value={focusingSubtree}
onFocus={setFocusingSubtree}
/>
)}
{tree && (
<div className="px-2">
{/* This element is presentational only: Adds a connecting border between the FocusPath and the tree below */}
<div className="border-l border-polkadot-700 h-2 max-sm:border-none" />
</div>
)}
<SubtreeFocus
value={{ callback: setFocusingSubtree, path: focusingSubtree }}
>
<MarkersContextProvider>
<div
ref={listRef}
className="flex flex-row overflow-auto w-full gap-2"
>
{tree && (
<div
ref={treeRef}
className="w-96 sticky top-0 pl-2 pb-16 leading-loose overflow-hidden max-sm:hidden"
>
<div className="relative">
<TreeCodec {...codecProps} />
<VisibleWindow />
</div>
</div>
)}
<div className="flex-1">
<div className="p-2 rounded bg-polkadot-900">
<Marker id={[]} />
<EditCodec {...codecProps} />
</div>
</div>
</div>
</MarkersContextProvider>
</SubtreeFocus>
</div>
)
}
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 <EditCodec {...props} />
}
const useCodecProps = (
type: number,
value: Uint8Array | "partial" | null,
onValueChange: (value: Uint8Array | "partial" | null) => void,
): (ComponentProps<typeof EditCodec> & { codec: Codec<any> }) | null => {
const [componentValue, setComponentValue] = useState<CodecComponentValue>({
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<typeof EditCodec> = {
return {
codec,
codecType: type,
metadata: metadataProps.metadata,
value: componentValue,
@@ -137,46 +210,4 @@ export const LookupTypeEdit: FC<{
value,
}),
}
return (
<div className="flex flex-col items-start overflow-hidden">
<BinaryDisplay {...codecProps} codec={codec} className="pb-2" />
<FocusPath
metadata={metadataProps.metadata}
typeId={type}
value={focusingSubtree}
onFocus={setFocusingSubtree}
/>
<div className="px-2">
{/* This element is presentational only: Adds a connecting border between the FocusPath and the tree below */}
<div className="border-l border-polkadot-700 h-2 max-sm:border-none" />
</div>
<SubtreeFocus
value={{ callback: setFocusingSubtree, path: focusingSubtree }}
>
<MarkersContextProvider>
<div
ref={listRef}
className="flex flex-row overflow-auto w-full gap-2"
>
<div
ref={treeRef}
className="w-96 sticky top-0 pl-2 pb-16 leading-loose overflow-hidden max-sm:hidden"
>
<div className="relative">
<TreeCodec {...codecProps} />
<VisibleWindow />
</div>
</div>
<div className="flex-1">
<div className="p-2 rounded bg-polkadot-900">
<Marker id={[]} />
<EditCodec {...codecProps} />
</div>
</div>
</div>
</MarkersContextProvider>
</SubtreeFocus>
</div>
)
}
+28
View File
@@ -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 (
<div className="text-sm text-slate-400">
<p>{docs[0]}</p>
</div>
)
}
const normalizedDocs =
docs.length > 1 ? ["/*", ...docs.map((v) => ` *${v}`), " */"] : docs
return (
<div
className={twMerge(
"text-slate-400 max-h-20 overflow-auto",
normalizedDocs.length > 1 && "text-sm font-mono whitespace-pre",
)}
>
{normalizedDocs.map((d, i) => (
<p key={i}>{d}</p>
))}
</div>
)
}
+34 -7
View File
@@ -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 (
<li key={idx} className="border rounded p-2 w-full">
<div
className="flex items-center cursor-pointer select-none"
className={twMerge(
"flex items-center select-none",
complexity !== "inline" && "cursor-pointer",
)}
onClick={() => setExpanded((e) => !e)}
>
<ExpandBtn expanded={expanded} />
{complexity === "inline" ? (
<Dot size={16} />
) : (
<ExpandBtn expanded={expanded} />
)}
<Circle
size={8}
strokeWidth={4}
@@ -137,13 +154,23 @@ const RuntimeValueInput: FC<{ idx: number; name: string; type: number }> = ({
)}
/>
<div className="text-polkadot-200">{name}</div>
{complexity === "inline" ? (
<div className="px-2">
<InlineLookupTypeEdit
type={type}
value={value}
onValueChange={(value) => setInputValue({ idx, value })}
/>
</div>
) : null}
</div>
{expanded && (
<div className="py-2 max-h-[60svh] overflow-hidden flex">
{expanded && complexity !== "inline" && (
<div className="py-2 max-h-[60svh] overflow-hidden flex flex-col justify-stretch">
<LookupTypeEdit
type={type}
value={value}
onValueChange={(value) => setInputValue({ idx, value })}
tree={complexity === "tree"}
/>
</div>
)}
+2 -5
View File
@@ -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 && (
<div className="w-full">
Docs
<div className="text-sm text-slate-400 max-h-20 overflow-auto">
{entry.docs.map((d, i) => (
<p key={i}>{d}</p>
))}
</div>
<DocsRenderer docs={entry.docs} />
</div>
)}
<RuntimeCallQuery />
+2 -5
View File
@@ -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 && (
<div className="w-full">
Docs
<div className="text-sm text-slate-400 max-h-20 overflow-auto">
{selectedEntry.docs.map((d, i) => (
<p key={i}>{d}</p>
))}
</div>
<DocsRenderer docs={selectedEntry.docs} />
</div>
)}
<StorageEntry />
+54
View File
@@ -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<TypeComplexity> = ["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)