edit key binary

This commit is contained in:
Victor Oliva
2024-10-24 16:27:09 +02:00
parent 687a2b537d
commit 004953421d
4 changed files with 74 additions and 7 deletions
+2
View File
@@ -13,6 +13,8 @@ import { BinaryStatus } from "@/codec-components/EditCodec/Tree/codec-components
// @ts-expect-error save-as typings not available
import { saveAs } from "save-as"
// TODO refactor this out into a component not tied to `Extrinsics`
// if possible, make it the edit button itself, internally handling the `open/close` state of the modal.
export const BinaryEditModal: FC<{
status: BinaryStatus
open: boolean
+9 -6
View File
@@ -6,6 +6,7 @@ import { state, useStateObservable } from "@react-rxjs/core"
import { FC, useEffect, useState } from "react"
import { map } from "rxjs"
import { selectedEntry$, setSelectedEntry } from "./storage.state"
import { StorageDecode } from "./StorageDecode"
import { StorageQuery } from "./StorageQuery"
import { StorageSubscriptions } from "./StorageSubscriptions"
@@ -29,12 +30,18 @@ const metadataStorage$ = state(
export const Storage = withSubscribe(() => {
const { lookup, entries } = useStateObservable(metadataStorage$)
const [pallet, setPallet] = useState<string | null>(null)
const [entry, setEntry] = useState<string | null>(null)
const [pallet, setPallet] = useState<string | null>("System")
const [entry, setEntry] = useState<string | null>("Account")
const selectedPallet =
(pallet && lookup.metadata.pallets.find((p) => p.name === pallet)) || null
useEffect(
() => setEntry(selectedPallet?.storage?.items[0]?.name ?? null),
// eslint-disable-next-line react-hooks/exhaustive-deps
[selectedPallet?.name],
)
useEffect(() => {
const storageEntry = (
(entry &&
@@ -143,7 +150,3 @@ const StorageEntry: FC = () => {
</>
)
}
const StorageDecode: FC = () => {
return null
}
+18
View File
@@ -0,0 +1,18 @@
import { ActionButton } from "@/components/ActionButton"
import { FC, useState } from "react"
export const StorageDecode: FC = () => {
const [value, setValue] = useState("")
return (
<div className="w-full">
<textarea
value={value}
onChange={(e) => setValue(e.target.value)}
className="w-full bg-polkadot-100 p-2 rounded tabular-nums text-polkadot-800"
placeholder="Enter hex …"
/>
<ActionButton>Decode</ActionButton>
</div>
)
}
+45 -1
View File
@@ -30,8 +30,11 @@ import { BinaryEditModal } from "../Extrinsics/BinaryEditModal"
import { addStorageSubscription, selectedEntry$ } from "./storage.state"
export const StorageQuery: FC = () => {
const selectedEntry = useStateObservable(selectedEntry$)
const isReady = useStateObservable(isReady$)
if (!selectedEntry) return null
const submit = async () => {
const [entry, unsafeApi, keyValues, keysEnabled] = await firstValueFrom(
combineLatest([selectedEntry$, unsafeApi$, keyValues$, keysEnabled$]),
@@ -63,8 +66,8 @@ export const StorageQuery: FC = () => {
return (
<div className="p-2 flex flex-col gap-4 items-start w-full">
<StorageKeysInput />
<KeyDisplay />
<StorageKeysInput />
<ActionButton disabled={!isReady} onClick={submit}>
Query
</ActionButton>
@@ -290,7 +293,19 @@ const encodedKey$ = state(
null,
)
const KeyDisplay: FC = () => {
const [binaryOpen, setBinaryOpen] = useState(false)
const key = useStateObservable(encodedKey$)
const lookup = useStateObservable(lookupState$)
const selectedEntry = useStateObservable(selectedEntry$)
const keys = useStateObservable(keys$)
const keysEnabled = useStateObservable(keysEnabled$)
if (!lookup || !selectedEntry) return null
const codec = getDynamicBuilder(lookup).buildStorage(
selectedEntry.pallet,
selectedEntry.entry,
)
return (
<div className="flex w-full overflow-hidden bg-polkadot-800 p-2 gap-2 rounded items-center">
@@ -304,6 +319,35 @@ const KeyDisplay: FC = () => {
{key ?? "Fill in all the storage keys to calculate the encoded key"}
</div>
<CopyText text={key ?? ""} disabled={key === null} />
{keys.length === keysEnabled && (
<BinaryEdit
size={20}
className="cursor-pointer hover:text-polkadot-300"
onClick={() => setBinaryOpen(true)}
/>
)}
<BinaryEditModal
status={{
encodedValue: key ? Binary.fromHex(key).asBytes() : undefined,
onValueChanged: (value: any[]) => {
value.forEach((value, idx) => setKeyValue({ idx, value }))
return true
},
decode: (v) => {
try {
return codec.keyDecoder(
typeof v === "string" ? v : Binary.fromBytes(v).asHex(),
)
} catch (_) {
return NOTIN
}
},
type: "complete",
}}
open={binaryOpen}
path=""
onClose={() => setBinaryOpen(false)}
/>
</div>
)
}