From 646cf7e1cdef85623e9ca718e9cc3b86685a94d5 Mon Sep 17 00:00:00 2001 From: Victor Oliva Date: Fri, 25 Oct 2024 09:41:31 +0200 Subject: [PATCH] refactor: make edit modal generic, out from ExtrinsicsPage specifics --- .../EditCodec/Tree/codec-components.tsx | 21 ++++- .../BinaryEditButton.tsx} | 89 ++++++++++++++----- src/pages/Extrinsics/BinaryDisplay.tsx | 35 +++----- src/pages/Storage/StorageDecode.tsx | 6 +- src/pages/Storage/StorageQuery.tsx | 72 +++++---------- 5 files changed, 120 insertions(+), 103 deletions(-) rename src/{pages/Extrinsics/BinaryEditModal.tsx => components/BinaryEditButton.tsx} (68%) diff --git a/src/codec-components/EditCodec/Tree/codec-components.tsx b/src/codec-components/EditCodec/Tree/codec-components.tsx index 687fab9..1236f33 100644 --- a/src/codec-components/EditCodec/Tree/codec-components.tsx +++ b/src/codec-components/EditCodec/Tree/codec-components.tsx @@ -1,5 +1,5 @@ +import { BinaryEditModal } from "@/components/BinaryEditButton" import { BinaryEdit, Focus, TypeIcon, TypeIcons } from "@/components/Icons" -import { BinaryEditModal } from "@/pages/Extrinsics/BinaryEditModal" import { EditAccountId, EditBigNumber, @@ -212,7 +212,7 @@ export const ItemTitle: FC< {binaryStatus && ( - ) } + +const TreeBinaryEditModal: FC<{ + status: BinaryStatus + open: boolean + path: string + onClose: () => void +}> = ({ status, path, open, onClose }) => ( + +) diff --git a/src/pages/Extrinsics/BinaryEditModal.tsx b/src/components/BinaryEditButton.tsx similarity index 68% rename from src/pages/Extrinsics/BinaryEditModal.tsx rename to src/components/BinaryEditButton.tsx index 7654715..b1db788 100644 --- a/src/pages/Extrinsics/BinaryEditModal.tsx +++ b/src/components/BinaryEditButton.tsx @@ -7,40 +7,74 @@ import { useGenericSynchronizeInput } from "@/components/useSynchroniseInput" import { NOTIN } from "@codec-components" import { Binary } from "@polkadot-api/substrate-bindings" import { Download, FileUp } from "lucide-react" -import { FC, useState } from "react" +import { ComponentProps, FC, useMemo, useState } from "react" import { twMerge } from "tailwind-merge" -import { BinaryStatus } from "@/codec-components/EditCodec/Tree/codec-components" // @ts-expect-error save-as typings not available import { saveAs } from "save-as" +import { BinaryEdit } from "./Icons" -// 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 - path: string - onClose: () => void -}> = ({ status, path, open, onClose }) => ( +export interface BinaryEditProps { + initialValue?: Uint8Array + decode: (value: Uint8Array) => unknown | NOTIN + onValueChange: (value: any) => void + title?: string + fileName?: string +} + +export const BinaryEditButton: FC< + BinaryEditProps & { + iconProps?: ComponentProps + } +> = ({ iconProps, ...props }) => { + const [open, setOpen] = useState(false) + return ( + <> + setOpen(true)} + /> + setOpen(false)} /> + + ) +} + +export const BinaryEditModal: FC< + BinaryEditProps & { + open: boolean + onClose: () => void + } +> = ({ open, ...props }) => ( - {path} + {props.title} - + ) const MAX_DISPLAY_SIZE = 5 * 1024 * 1024 -const BinaryEditModalContent: FC<{ - path: string - status: BinaryStatus - onClose: () => void -}> = ({ path, status, onClose }) => { - const [value, setValue] = useState(status.encodedValue ?? new Uint8Array()) +const BinaryEditModalContent: FC< + BinaryEditProps & { + onClose: () => void + } +> = ({ + fileName, + initialValue = new Uint8Array(), + decode, + onValueChange, + onClose, +}) => { + const [value, setValue] = useState(initialValue) const [error, setError] = useState("") const [inputValue, setInputValue] = useGenericSynchronizeInput( value, @@ -60,12 +94,21 @@ const BinaryEditModalContent: FC<{ const placeholder = value.length > MAX_DISPLAY_SIZE ? "(value is too long to display)" : "" - const isValid = status.decode(value) !== NOTIN + const safeDecode = (value: Uint8Array) => { + try { + return decode(value) + } catch (_) { + return NOTIN + } + } + // eslint-disable-next-line react-hooks/exhaustive-deps + const isValid = useMemo(() => safeDecode(value) !== NOTIN, [value, decode]) const textareaValue = inputValue instanceof Uint8Array ? "" : inputValue const submit = () => { - if (status.decode(value) !== NOTIN) { - status.onValueChanged(status.decode(value)) + const decoded = safeDecode(value) + if (decoded !== NOTIN) { + onValueChange(decoded) onClose() } else { // Shouldn't happen, but it's better than just swallowing that it failed. @@ -87,7 +130,7 @@ const BinaryEditModalContent: FC<{ new Blob([value], { type: "application/octet-stream", }), - `${path.replace(/\./g, "__") || "root"}.dat`, + `${fileName ?? new Date().toISOString()}.dat`, ) } diff --git a/src/pages/Extrinsics/BinaryDisplay.tsx b/src/pages/Extrinsics/BinaryDisplay.tsx index db6ca86..b9f0651 100644 --- a/src/pages/Extrinsics/BinaryDisplay.tsx +++ b/src/pages/Extrinsics/BinaryDisplay.tsx @@ -1,10 +1,8 @@ +import { BinaryEditButton } from "@/components/BinaryEditButton" import { CopyText } from "@/components/Copy" -import { BinaryEdit } from "@/components/Icons" import { NOTIN } from "@codec-components" import { Binary, HexString } from "@polkadot-api/substrate-bindings" -import { FC, useState } from "react" -import { twMerge } from "tailwind-merge" -import { BinaryEditModal } from "./BinaryEditModal" +import { FC } from "react" const DISPLAY_MAX_LEN = 1024 const COPY_MAX_LEN = 5 * 1024 * 1024 @@ -14,8 +12,6 @@ export const BinaryDisplay: FC<{ onValueChanged: (value: any | NOTIN) => boolean decode: (value: Uint8Array | HexString) => any | NOTIN }> = ({ value, isEmpty, onValueChanged, decode }) => { - const [binaryOpen, setBinaryOpen] = useState(false) - const displayLength = DISPLAY_MAX_LEN * 2 + 2 const copyLength = COPY_MAX_LEN * 2 + 2 @@ -44,24 +40,17 @@ export const BinaryDisplay: FC<{ )} - setBinaryOpen(true)} - /> - setBinaryOpen(false)} /> diff --git a/src/pages/Storage/StorageDecode.tsx b/src/pages/Storage/StorageDecode.tsx index b1ee8e8..f2c517d 100644 --- a/src/pages/Storage/StorageDecode.tsx +++ b/src/pages/Storage/StorageDecode.tsx @@ -1,12 +1,12 @@ +import { lookup$ } from "@/chain.state" import { ActionButton } from "@/components/ActionButton" import { NOTIN } from "@codec-components" -import { state, useStateObservable, withDefault } from "@react-rxjs/core" +import { getDynamicBuilder } from "@polkadot-api/metadata-builders" +import { state, useStateObservable } from "@react-rxjs/core" import { createSignal } from "@react-rxjs/utils" import { FC } from "react" import { combineLatest, firstValueFrom, map, of } from "rxjs" import { addStorageSubscription, selectedEntry$ } from "./storage.state" -import { lookup$ } from "@/chain.state" -import { getDynamicBuilder } from "@polkadot-api/metadata-builders" const [valueChange, setValue] = createSignal() const value$ = state(valueChange, "") diff --git a/src/pages/Storage/StorageQuery.tsx b/src/pages/Storage/StorageQuery.tsx index 6399af5..c4465e6 100644 --- a/src/pages/Storage/StorageQuery.tsx +++ b/src/pages/Storage/StorageQuery.tsx @@ -1,9 +1,9 @@ import { lookup$, unsafeApi$ } from "@/chain.state" import { EditCodec } from "@/codec-components/EditCodec" import { ActionButton } from "@/components/ActionButton" +import { BinaryEditButton } from "@/components/BinaryEditButton" import { bytesToString } from "@/components/BinaryInput" import { CopyText } from "@/components/Copy" -import { BinaryEdit } from "@/components/Icons" import SliderToggle from "@/components/Toggle" import { CodecComponentType, @@ -14,7 +14,7 @@ import { getDynamicBuilder } from "@polkadot-api/metadata-builders" import { state, useStateObservable, withDefault } from "@react-rxjs/core" import { createSignal } from "@react-rxjs/utils" import { Binary } from "polkadot-api" -import { FC, useState } from "react" +import { FC } from "react" import { combineLatest, filter, @@ -26,7 +26,6 @@ import { switchMap, } from "rxjs" import { twMerge } from "tailwind-merge" -import { BinaryEditModal } from "../Extrinsics/BinaryEditModal" import { addStorageSubscription, selectedEntry$ } from "./storage.state" export const StorageQuery: FC = () => { @@ -187,7 +186,6 @@ const StorageKeyInput: FC<{ idx: number; type: number; disabled: boolean }> = ({ type, disabled, }) => { - const [binaryOpen, setBinaryOpen] = useState(false) const lookup = useStateObservable(lookupState$) const value = useStateObservable(keyInputValue$(idx)) @@ -236,10 +234,14 @@ const StorageKeyInput: FC<{ idx: number; type: number; disabled: boolean }> = ({ >
{getTypeName()}
- setBinaryOpen(true)} + setKeyValue({ idx, value })} + decode={codec.dec} />
= ({ setKeyValue({ idx, value: value.empty ? NOTIN : value.decoded }) } /> - { - setKeyValue({ idx, value }) - return true - }, - decode: codec.dec, - type: "complete", - }} - open={binaryOpen} - path="" - onClose={() => setBinaryOpen(false)} - /> ) } @@ -293,7 +278,6 @@ const encodedKey$ = state( null, ) const KeyDisplay: FC = () => { - const [binaryOpen, setBinaryOpen] = useState(false) const key = useStateObservable(encodedKey$) const lookup = useStateObservable(lookupState$) const selectedEntry = useStateObservable(selectedEntry$) @@ -320,34 +304,18 @@ const KeyDisplay: FC = () => { {keys.length === keysEnabled && ( - setBinaryOpen(true)} + { + value.forEach((value, idx) => setKeyValue({ idx, value })) + }} + decode={(v) => + codec.keyDecoder( + typeof v === "string" ? v : Binary.fromBytes(v).asHex(), + ) + } /> )} - { - 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)} - /> ) }