|
| 1 | +import { |
| 2 | + dynamicBuilder$, |
| 3 | + lookup$, |
| 4 | + unsafeApi$, |
| 5 | +} from "@/state/chains/chain.state" |
| 6 | +import { |
| 7 | + InlineLookupTypeEdit, |
| 8 | + LookupTypeEdit, |
| 9 | +} from "@/codec-components/LookupTypeEdit" |
| 10 | +import { ActionButton } from "@/components/ActionButton" |
| 11 | +import { ExpandBtn } from "@/components/Expand" |
| 12 | +import { getTypeComplexity } from "@/utils/shape" |
| 13 | +import { state, useStateObservable, withDefault } from "@react-rxjs/core" |
| 14 | +import { createSignal } from "@react-rxjs/utils" |
| 15 | +import { Circle, Dot } from "lucide-react" |
| 16 | +import { FC, useState } from "react" |
| 17 | +import { |
| 18 | + combineLatest, |
| 19 | + filter, |
| 20 | + firstValueFrom, |
| 21 | + map, |
| 22 | + scan, |
| 23 | + startWith, |
| 24 | + switchMap, |
| 25 | +} from "rxjs" |
| 26 | +import { twMerge } from "tailwind-merge" |
| 27 | +import { addViewFnCall, selectedEntry$ } from "./viewFns.state" |
| 28 | + |
| 29 | +export const ViewFnQuery: FC = () => { |
| 30 | + const selectedEntry = useStateObservable(selectedEntry$) |
| 31 | + const isReady = useStateObservable(isReady$) |
| 32 | + |
| 33 | + if (!selectedEntry) return null |
| 34 | + |
| 35 | + const submit = async () => { |
| 36 | + const [entry, unsafeApi, inputValues, builder] = await firstValueFrom( |
| 37 | + combineLatest([ |
| 38 | + selectedEntry$, |
| 39 | + unsafeApi$, |
| 40 | + inputValues$, |
| 41 | + dynamicBuilder$, |
| 42 | + ]), |
| 43 | + ) |
| 44 | + const decodedValues = inputValues.map((v, i) => |
| 45 | + builder |
| 46 | + .buildDefinition(selectedEntry.inputs[i].type) |
| 47 | + .dec(v as Uint8Array), |
| 48 | + ) |
| 49 | + const promise = unsafeApi.view[entry!.pallet][entry!.name](...decodedValues) |
| 50 | + |
| 51 | + addViewFnCall({ |
| 52 | + name: `${entry!.pallet}.${entry!.name}(…)`, |
| 53 | + promise, |
| 54 | + type: entry!.output, |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="flex flex-col gap-4 items-start w-full"> |
| 60 | + <ViewFnInputValues /> |
| 61 | + <ActionButton disabled={!isReady} onClick={submit}> |
| 62 | + Call |
| 63 | + </ActionButton> |
| 64 | + </div> |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +const [inputValueChange$, setInputValue] = createSignal<{ |
| 69 | + idx: number |
| 70 | + value: Uint8Array | "partial" | null |
| 71 | +}>() |
| 72 | +const inputValues$ = selectedEntry$.pipeState( |
| 73 | + filter((v) => !!v), |
| 74 | + map((v) => v.inputs), |
| 75 | + switchMap((inputs) => { |
| 76 | + const values: Array<Uint8Array | "partial" | null> = inputs.map(() => null) |
| 77 | + return inputValueChange$.pipe( |
| 78 | + scan((acc, change) => { |
| 79 | + const newValue = [...acc] |
| 80 | + newValue[change.idx] = change.value |
| 81 | + return newValue |
| 82 | + }, values), |
| 83 | + startWith(values), |
| 84 | + ) |
| 85 | + }), |
| 86 | + withDefault([] as Array<Uint8Array | "partial" | null>), |
| 87 | +) |
| 88 | + |
| 89 | +const isReady$ = inputValues$.pipeState( |
| 90 | + map((inputValues) => inputValues.every((v) => v instanceof Uint8Array)), |
| 91 | + withDefault(false), |
| 92 | +) |
| 93 | + |
| 94 | +const ViewFnInputValues: FC = () => { |
| 95 | + const selectedEntry = useStateObservable(selectedEntry$) |
| 96 | + if (!selectedEntry || !selectedEntry.inputs.length) return null |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className="w-full"> |
| 100 | + Inputs |
| 101 | + <ol className="flex flex-col gap-2"> |
| 102 | + {selectedEntry.inputs.map((input, idx) => ( |
| 103 | + <RuntimeValueInput |
| 104 | + key={idx} |
| 105 | + idx={idx} |
| 106 | + name={input.name} |
| 107 | + type={input.type} |
| 108 | + /> |
| 109 | + ))} |
| 110 | + </ol> |
| 111 | + </div> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +const inputValue$ = state( |
| 116 | + (idx: number) => inputValues$.pipe(map((v) => v[idx])), |
| 117 | + null, |
| 118 | +) |
| 119 | +const lookupState$ = state(lookup$, null) |
| 120 | +const RuntimeValueInput: FC<{ idx: number; name: string; type: number }> = ({ |
| 121 | + idx, |
| 122 | + type, |
| 123 | + name, |
| 124 | +}) => { |
| 125 | + const value = useStateObservable(inputValue$(idx)) |
| 126 | + const [expanded, setExpanded] = useState(false) |
| 127 | + const lookup = useStateObservable(lookupState$) |
| 128 | + |
| 129 | + if (!lookup) return null |
| 130 | + const shape = lookup(type) |
| 131 | + const complexity = getTypeComplexity(shape) |
| 132 | + |
| 133 | + return ( |
| 134 | + <li key={idx} className="border rounded p-2 w-full"> |
| 135 | + <div |
| 136 | + className={twMerge( |
| 137 | + "flex items-center select-none", |
| 138 | + complexity !== "inline" && "cursor-pointer", |
| 139 | + )} |
| 140 | + onClick={() => setExpanded((e) => !e)} |
| 141 | + > |
| 142 | + {complexity === "inline" ? ( |
| 143 | + <Dot size={16} /> |
| 144 | + ) : ( |
| 145 | + <ExpandBtn expanded={expanded} /> |
| 146 | + )} |
| 147 | + <Circle |
| 148 | + size={8} |
| 149 | + strokeWidth={4} |
| 150 | + className={twMerge( |
| 151 | + "mr-1", |
| 152 | + value === null |
| 153 | + ? "text-red-600" |
| 154 | + : value === "partial" |
| 155 | + ? "text-orange-600" |
| 156 | + : "text-green-600", |
| 157 | + )} |
| 158 | + /> |
| 159 | + <div className="text-foreground/80">{name}</div> |
| 160 | + {complexity === "inline" ? ( |
| 161 | + <div className="px-2"> |
| 162 | + <InlineLookupTypeEdit |
| 163 | + type={type} |
| 164 | + value={value} |
| 165 | + onValueChange={(value) => setInputValue({ idx, value })} |
| 166 | + /> |
| 167 | + </div> |
| 168 | + ) : null} |
| 169 | + </div> |
| 170 | + {expanded && complexity !== "inline" && ( |
| 171 | + <div className="py-2 max-h-[60svh] overflow-hidden flex flex-col justify-stretch"> |
| 172 | + <LookupTypeEdit |
| 173 | + type={type} |
| 174 | + value={value} |
| 175 | + onValueChange={(value) => setInputValue({ idx, value })} |
| 176 | + tree={complexity === "tree"} |
| 177 | + /> |
| 178 | + </div> |
| 179 | + )} |
| 180 | + </li> |
| 181 | + ) |
| 182 | +} |
0 commit comments