edit key binary
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user