improve inline visualisation of ViewCodec

This commit is contained in:
Victor Oliva
2024-10-28 09:06:57 +01:00
parent fd312876bf
commit d1a9646b21
8 changed files with 166 additions and 71 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ export const CBytes: ViewBytes = ({ value, encodedValue }) => {
useReportBinary(encodedValue)
return (
<div className="min-w-80 border-none p-0 outline-none bg-transparent flex-1">
<div className="min-w-80 border-none p-0 outline-none bg-transparent flex-1 overflow-hidden text-ellipsis">
{bytesToString(value)}
</div>
)
+6 -3
View File
@@ -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<{
) : (
<span className="flex items-center py-1 gap-1">
{hasParentTitle && <ItemMarker />}
<Dot size={16} />
<span className="select-none flex items-center gap-1">
<span className="opacity-75">{name}</span>
<span ref={setTitleElement} />
@@ -74,8 +76,9 @@ const StructItem: React.FC<{
{title}
{isComplexShape && (
<div
className={clsx(
"flex flex-row pl-2 pb-2",
className={twMerge(
"flex flex-row pb-2",
hasParentTitle ? "pl-4" : "pl-2",
isExpanded ? "" : "hidden",
)}
>
@@ -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 (
<ChildProvider titleElement={null}>
@@ -26,6 +36,7 @@ const ListItemComponent: FC<
<CopyChildBinary visible={isActive} />
</div>
}
inline={!isComplexShape}
>
{children}
</ListItem>
@@ -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 (
<ul className="w-full">
{innerComponents.map((jsx, idx) => (
<ListItemComponent key={idx} idx={idx} path={[...path, String(idx)]}>
{jsx}
</ListItemComponent>
))}
{innerComponents.length ? (
innerComponents.map((jsx, idx) => (
<ListItemComponent
key={idx}
idx={idx}
path={[...path, String(idx)]}
field={shape.type === "tuple" ? shape.value[idx] : shape.value}
value={value[idx]}
>
{jsx}
</ListItemComponent>
))
) : (
<span className="text-sm text-slate-400">(Empty)</span>
)}
</ul>
)
}
export const CArray: ViewArray = ListComponent
export const CSequence: ViewSequence = ListComponent
export const CTuple: ViewTuple = ListComponent
export const CArray: ViewArray = (props) => <ListComponent {...props} />
export const CSequence: ViewSequence = (props) => <ListComponent {...props} />
export const CTuple: ViewTuple = (props) => <ListComponent {...props} />
+4
View File
@@ -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
}
+53 -28
View File
@@ -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 ? (
<div className="flex items-center">
<Marker id={path} />
<span className="flex items-center py-1 gap-1 mr-2">
<Dot size={16} />
Item {idx + 1}.
</span>
<div className="flex-1">{children}</div>
{onDelete ? (
<button
className="cursor-pointer text-gray-200 ml-2 hover:text-polkadot-400"
onClick={() => onDelete()}
>
<Trash2 size={16} />
</button>
) : null}
{actions}
</div>
) : (
<div className="flex items-center">
<Marker id={path} />
<span
className="cursor-pointer flex items-center py-1 gap-1"
onClick={() => toggleCollapsed(pathStr)}
>
<ExpandBtn expanded={!isCollapsed} />
Item {idx + 1}.
</span>
{onDelete ? (
<button
className="cursor-pointer text-gray-200 ml-2 hover:text-polkadot-400"
onClick={() => onDelete()}
>
<Trash2 size={16} />
</button>
) : null}
{actions}
</div>
)
return (
<li
className={twMerge(
@@ -31,33 +72,17 @@ export const ListItem: React.FC<{
onMouseEnter={() => setHovered({ id: pathStr, hover: true })}
onMouseLeave={() => setHovered({ id: pathStr, hover: false })}
>
<div className="flex items-stretch">
<Marker id={path} />
<span
className="cursor-pointer flex items-center py-1 gap-1"
onClick={() => toggleCollapsed(pathStr)}
{title}
{inline ? null : (
<div
className={clsx(
"flex-row p-2 items-center border border-polkadot-700 rounded ",
isCollapsed ? "hidden" : "",
)}
>
<ExpandBtn expanded={!isCollapsed} />
Item {idx + 1}.
</span>
{onDelete ? (
<button
className="cursor-pointer text-gray-200 ml-2 hover:text-polkadot-400"
onClick={() => onDelete()}
>
<Trash2 size={16} />
</button>
) : null}
{actions}
</div>
<div
className={clsx(
"flex-row p-2 items-center border border-polkadot-700 rounded ",
isCollapsed ? "hidden" : "",
)}
>
{children}
</div>
{children}
</div>
)}
</li>
)
}
+28 -17
View File
@@ -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 (
<li className="border rounded border-polkadot-200 p-2">
<div className="flex justify-between items-center pb-1 overflow-hidden">
@@ -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",
},
]}
/>
<button>
<Trash2
size={20}
className="text-polkadot-400 cursor-pointer hover:text-polkadot-500"
onClick={() => removeStorageSubscription(subscription)}
/>
<button onClick={() => toggleSubscriptionPause(subscription)}>
{storageSubscription.paused ? (
<PlayCircle {...iconButtonProps} />
) : (
<PauseCircle {...iconButtonProps} />
)}
</button>
<button onClick={() => removeStorageSubscription(subscription)}>
<Trash2 {...iconButtonProps} />
</button>
</div>
</div>
@@ -89,12 +98,14 @@ const ResultDisplay: FC<{
if (storageSubscription.single) {
return (
<ValueDisplay
mode={mode}
storageSubscription={storageSubscription}
value={storageSubscription.result}
title={"Result"}
/>
<div className="max-h-[60svh] overflow-auto">
<ValueDisplay
mode={mode}
storageSubscription={storageSubscription}
value={storageSubscription.result}
title={"Result"}
/>
</div>
)
}
+40 -12
View File
@@ -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<string>()
export const [togglePause$, toggleSubscriptionPause] =
createKeyedSignal<string>()
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<StorageSubscription> =>
stream.pipe(
map((result) => ({
...props,
result,
})),
startWith(props),
),
),
switchMap(({ stream, ...props }): Observable<StorageSubscription> => {
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<StorageSubscription> =>
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,
)
+1
View File
@@ -1,6 +1,7 @@
import { EnumVar, StructVar, Var } from "@polkadot-api/metadata-builders"
const complexTypes = new Set<Var["type"]>([
"tuple",
"array",
"sequence",
"struct",