From d1a9646b211111f12b0ae0cfbb15d2478010d220 Mon Sep 17 00:00:00 2001 From: Victor Oliva Date: Mon, 28 Oct 2024 09:06:57 +0100 Subject: [PATCH] improve inline visualisation of ViewCodec --- src/codec-components/ViewCodec/CBytes.tsx | 2 +- src/codec-components/ViewCodec/CStruct.tsx | 9 ++- .../ViewCodec/ListComponents.tsx | 43 +++++++--- src/codec-components/ViewCodec/utils.ts | 4 + src/codec-components/common/ListItem.tsx | 81 ++++++++++++------- src/pages/Storage/StorageSubscriptions.tsx | 45 +++++++---- src/pages/Storage/storage.state.ts | 52 +++++++++--- src/utils/shape.ts | 1 + 8 files changed, 166 insertions(+), 71 deletions(-) diff --git a/src/codec-components/ViewCodec/CBytes.tsx b/src/codec-components/ViewCodec/CBytes.tsx index 48a702d..7e20c09 100644 --- a/src/codec-components/ViewCodec/CBytes.tsx +++ b/src/codec-components/ViewCodec/CBytes.tsx @@ -6,7 +6,7 @@ export const CBytes: ViewBytes = ({ value, encodedValue }) => { useReportBinary(encodedValue) return ( -
+
{bytesToString(value)}
) diff --git a/src/codec-components/ViewCodec/CStruct.tsx b/src/codec-components/ViewCodec/CStruct.tsx index b07decd..92874e5 100644 --- a/src/codec-components/ViewCodec/CStruct.tsx +++ b/src/codec-components/ViewCodec/CStruct.tsx @@ -3,7 +3,7 @@ import { ViewStruct } from "@codec-components" import { Var } from "@polkadot-api/metadata-builders" import { useStateObservable } from "@react-rxjs/core" import React, { useContext, useState } from "react" -import { twMerge as clsx, twMerge } from "tailwind-merge" +import { twMerge } from "tailwind-merge" import { Marker } from "../common/Markers" import { isActive$, @@ -16,6 +16,7 @@ import { ItemMarker } from "../EditCodec/Tree/codec-components" import { CopyChildBinary, useReportBinary } from "./CopyBinary" import { ChildProvider, TitleContext } from "./TitleContext" import { isComplexNested } from "./utils" +import { Dot } from "lucide-react" const StructItem: React.FC<{ name: string @@ -49,6 +50,7 @@ const StructItem: React.FC<{ ) : ( {hasParentTitle && } + {name} @@ -74,8 +76,9 @@ const StructItem: React.FC<{ {title} {isComplexShape && (
diff --git a/src/codec-components/ViewCodec/ListComponents.tsx b/src/codec-components/ViewCodec/ListComponents.tsx index 8dd8694..15f5ae2 100644 --- a/src/codec-components/ViewCodec/ListComponents.tsx +++ b/src/codec-components/ViewCodec/ListComponents.tsx @@ -6,15 +6,25 @@ import { isActive$ } from "../common/paths.state" import { useSubtreeFocus } from "../common/SubtreeFocus" import { CopyChildBinary } from "./CopyBinary" import { ChildProvider } from "./TitleContext" +import { + ArrayVar, + SequenceVar, + TupleVar, + Var, +} from "@polkadot-api/metadata-builders" +import { isComplexNested } from "./utils" const ListItemComponent: FC< PropsWithChildren<{ idx: number path: string[] + field: Var + value: unknown }> -> = ({ idx, path, children }) => { +> = ({ idx, path, children, field, value }) => { const pathStr = path.join(".") const isActive = useStateObservable(isActive$(pathStr)) + const isComplexShape = isComplexNested(field, value) return ( @@ -26,6 +36,7 @@ const ListItemComponent: FC<
} + inline={!isComplexShape} > {children} @@ -36,7 +47,9 @@ const ListItemComponent: FC< const ListComponent: FC<{ innerComponents: ReactNode[] path: string[] -}> = ({ innerComponents, path }) => { + shape: ArrayVar | TupleVar | SequenceVar + value: unknown[] +}> = ({ innerComponents, path, shape, value }) => { const focus = useSubtreeFocus() const sub = focus.getNextPath(path) if (sub) { @@ -45,15 +58,25 @@ const ListComponent: FC<{ return (
    - {innerComponents.map((jsx, idx) => ( - - {jsx} - - ))} + {innerComponents.length ? ( + innerComponents.map((jsx, idx) => ( + + {jsx} + + )) + ) : ( + (Empty) + )}
) } -export const CArray: ViewArray = ListComponent -export const CSequence: ViewSequence = ListComponent -export const CTuple: ViewTuple = ListComponent +export const CArray: ViewArray = (props) => +export const CSequence: ViewSequence = (props) => +export const CTuple: ViewTuple = (props) => diff --git a/src/codec-components/ViewCodec/utils.ts b/src/codec-components/ViewCodec/utils.ts index 6e90fa1..b9edf94 100644 --- a/src/codec-components/ViewCodec/utils.ts +++ b/src/codec-components/ViewCodec/utils.ts @@ -10,5 +10,9 @@ export const isComplexNested = (field: Var, value: any): boolean => { if (field.type === "option") return value == null ? false : isComplexNested(field.value, value) + if (field.type === "sequence" && value.length === 0) { + return false + } + return true } diff --git a/src/codec-components/common/ListItem.tsx b/src/codec-components/common/ListItem.tsx index 886aed6..3009094 100644 --- a/src/codec-components/common/ListItem.tsx +++ b/src/codec-components/common/ListItem.tsx @@ -1,6 +1,6 @@ import { ExpandBtn } from "@/components/Expand" import { useStateObservable } from "@react-rxjs/core" -import { Trash2 } from "lucide-react" +import { Dot, Trash2 } from "lucide-react" import { twMerge as clsx, twMerge } from "tailwind-merge" import { Marker } from "./Markers" import { @@ -17,11 +17,52 @@ export const ListItem: React.FC<{ path: string[] onDelete?: () => void actions?: ReactNode -}> = ({ idx, onDelete, children, path, actions }) => { + inline?: boolean +}> = ({ idx, onDelete, children, path, actions, inline }) => { const pathStr = path.join(".") const isActive = useStateObservable(isActive$(pathStr)) const isCollapsed = useStateObservable(isCollapsed$(pathStr)) + const title = inline ? ( +
+ + + + Item {idx + 1}. + +
{children}
+ {onDelete ? ( + + ) : null} + {actions} +
+ ) : ( +
+ + toggleCollapsed(pathStr)} + > + + Item {idx + 1}. + + {onDelete ? ( + + ) : null} + {actions} +
+ ) + return (
  • setHovered({ id: pathStr, hover: true })} onMouseLeave={() => setHovered({ id: pathStr, hover: false })} > -
    - - toggleCollapsed(pathStr)} + {title} + {inline ? null : ( +
    - - Item {idx + 1}. - - {onDelete ? ( - - ) : null} - {actions} -
    -
    - {children} -
    + {children} +
    + )}
  • ) } diff --git a/src/pages/Storage/StorageSubscriptions.tsx b/src/pages/Storage/StorageSubscriptions.tsx index 39d74d3..277b83d 100644 --- a/src/pages/Storage/StorageSubscriptions.tsx +++ b/src/pages/Storage/StorageSubscriptions.tsx @@ -6,7 +6,7 @@ import { JsonDisplay } from "@/components/JsonDisplay" import { CodecComponentType, NOTIN } from "@codec-components" import { getDynamicBuilder } from "@polkadot-api/metadata-builders" import { state, useStateObservable } from "@react-rxjs/core" -import { Trash2 } from "lucide-react" +import { PauseCircle, PlayCircle, Trash2 } from "lucide-react" import { FC, useMemo, useState } from "react" import { Virtuoso } from "react-virtuoso" import { map } from "rxjs" @@ -16,6 +16,7 @@ import { storageSubscription$, storageSubscriptionKeys$, stringifyArg, + toggleSubscriptionPause, } from "./storage.state" export const StorageSubscriptions: FC = () => { @@ -44,6 +45,11 @@ const StorageSubscriptionBox: FC<{ subscription: string }> = ({ ) if (!storageSubscription) return null + const iconButtonProps = { + size: 20, + className: "text-polkadot-400 cursor-pointer hover:text-polkadot-500", + } + return (
  • @@ -55,22 +61,25 @@ const StorageSubscriptionBox: FC<{ subscription: string }> = ({ value={mode} onValueChange={setMode as any} items={[ - { - value: "json", - content: "JSON", - }, { value: "decoded", content: "Decoded", }, + { + value: "json", + content: "JSON", + }, ]} /> - +
  • @@ -89,12 +98,14 @@ const ResultDisplay: FC<{ if (storageSubscription.single) { return ( - +
    + +
    ) } diff --git a/src/pages/Storage/storage.state.ts b/src/pages/Storage/storage.state.ts index e882f5b..e8fce19 100644 --- a/src/pages/Storage/storage.state.ts +++ b/src/pages/Storage/storage.state.ts @@ -7,7 +7,16 @@ import { toKeySet, } from "@react-rxjs/utils" import { Binary } from "polkadot-api" -import { map, Observable, startWith, switchMap, takeUntil } from "rxjs" +import { + combineLatest, + distinctUntilChanged, + map, + Observable, + scan, + startWith, + switchMap, + takeUntil, +} from "rxjs" import { v4 as uuid } from "uuid" export type StorageMetadataEntry = { @@ -30,28 +39,38 @@ export const [newStorageSubscription$, addStorageSubscription] = createSignal<{ }>() export const [removeStorageSubscription$, removeStorageSubscription] = createKeyedSignal() +export const [togglePause$, toggleSubscriptionPause] = + createKeyedSignal() export type StorageSubscription = { name: string args: unknown[] | null type: number single: boolean + paused: boolean } & ({ result: unknown } | {}) const [getStorageSubscription$, storageSubscriptionKeyChange$] = partitionByKey( newStorageSubscription$, () => uuid(), (src$, id) => src$.pipe( - switchMap( - ({ stream, ...props }): Observable => - stream.pipe( - map((result) => ({ - ...props, - result, - })), - startWith(props), - ), - ), + switchMap(({ stream, ...props }): Observable => { + const paused$ = togglePause$(id).pipe( + scan((v) => !v, false), + startWith(false), + ) + const result$ = stream.pipe( + map((result) => ({ + ...props, + result, + paused: false, + })), + startWith(props), + ) + return combineLatest([paused$, result$]).pipe( + map(([paused, result]) => ({ ...result, paused })), + ) + }), takeUntil(removeStorageSubscription$(id)), ), ) @@ -66,7 +85,16 @@ export const storageSubscriptionKeys$ = state( export const storageSubscription$ = state( (key: string): Observable => - getStorageSubscription$(key), + getStorageSubscription$(key).pipe( + // Don't propagate if paused + distinctUntilChanged((prev, current) => { + // if it's not paused, mark it as "not equal" for the update to go through + if (!current.paused) return false + // otherwise, don't propagate if we were previously paused + // but do propagate if we weren't: Because we still need to show the "paused" status. + return prev.paused === current.paused + }), + ), null, ) diff --git a/src/utils/shape.ts b/src/utils/shape.ts index 5ca6475..b35ddd0 100644 --- a/src/utils/shape.ts +++ b/src/utils/shape.ts @@ -1,6 +1,7 @@ import { EnumVar, StructVar, Var } from "@polkadot-api/metadata-builders" const complexTypes = new Set([ + "tuple", "array", "sequence", "struct",