From 37118174b68ab945db8191809a0a136827db580c Mon Sep 17 00:00:00 2001 From: Victor Oliva Date: Wed, 1 May 2024 17:29:54 +0200 Subject: [PATCH] show types on variants, reuse checksumBuilder --- src/CommonTypes/CommonType.tsx | 44 +++++++++++++++++++++---- src/CommonTypes/Export.tsx | 16 +++++---- src/CommonTypes/commonTypes.state.ts | 15 ++++----- src/CommonTypes/typeReferences.state.ts | 12 +++---- src/api/metadatas.ts | 5 +++ 5 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/CommonTypes/CommonType.tsx b/src/CommonTypes/CommonType.tsx index fce3fb6..7d55b96 100644 --- a/src/CommonTypes/CommonType.tsx +++ b/src/CommonTypes/CommonType.tsx @@ -1,4 +1,5 @@ -import { TextField } from "@radix-ui/themes" +import { EnumVar } from "@polkadot-api/metadata-builders" +import { TextField, Tooltip } from "@radix-ui/themes" import { useStateObservable } from "@react-rxjs/core" import { FC, useMemo, useState } from "react" import { twMerge } from "tailwind-merge" @@ -86,10 +87,8 @@ export const CommonType: FC<{

Variants

@@ -108,6 +107,39 @@ export const CommonType: FC<{ ) } +const Variant: FC<{ + name: string + value: EnumVar["value"][string] +}> = ({ name, value }) => { + const valueType = + value.type === "void" ? "void" : stringifyCircular(value.value) + + return ( + +
  • + {name} +
  • +
    + ) +} +const stringifyCircular = (value: unknown) => { + const seen = new Set() + try { + return JSON.stringify(value, (_, value) => { + if (typeof value === "object" && value !== null) { + if (seen.has(value)) { + return + } + seen.add(value) + } + return value + }) + } catch (ex) { + console.error(ex) + return `{serialization error}` + } +} + const Chain: FC<{ chain: string types: EnumEntry[] @@ -162,7 +194,7 @@ const ChainReferences: FC<{ chain: string; checksum: string }> = ({ const ReferenceList: FC<{ references: string[] }> = ({ references }) => ( diff --git a/src/CommonTypes/Export.tsx b/src/CommonTypes/Export.tsx index 9ccf64a..39e79d9 100644 --- a/src/CommonTypes/Export.tsx +++ b/src/CommonTypes/Export.tsx @@ -15,7 +15,7 @@ export type RepositoryEntry = { paths: string[] type: string } -const allNewKnownTypes$ = state( +const allKnownTypes$ = state( combineLatest({ chains: combineKeys(selectedChains$, chainTypes$), names: commonTypeNames$, @@ -24,8 +24,6 @@ const allNewKnownTypes$ = state( const result: Record = {} Object.entries(names).forEach(([checksum, name]) => { - if (!name) return - const chainsWithType = Array.from(chains.keys()).filter( (chain) => checksum in chains.get(chain)!, ) @@ -66,16 +64,20 @@ const onlyChanges$ = state( ) const newKnownTypes$ = state( - combineLatest([allNewKnownTypes$, onlyChanges$]).pipe( - map(([allNewKnownTypes, onlyChanges]) => + combineLatest([allKnownTypes$, onlyChanges$]).pipe( + map(([allKnownTypes, onlyChanges]) => onlyChanges ? Object.fromEntries( - Object.entries(allNewKnownTypes).filter( + Object.entries(allKnownTypes).filter( ([checksum, entry]) => entry.name !== getCurrentKnownType(checksum), ), ) - : allNewKnownTypes, + : Object.fromEntries( + Object.entries(allKnownTypes).filter( + ([, entry]) => entry.name !== null, + ), + ), ), ), {}, diff --git a/src/CommonTypes/commonTypes.state.ts b/src/CommonTypes/commonTypes.state.ts index 868b3dd..ead8315 100644 --- a/src/CommonTypes/commonTypes.state.ts +++ b/src/CommonTypes/commonTypes.state.ts @@ -1,9 +1,5 @@ import { knownTypesRepository } from "@polkadot-api/codegen" -import { - LookupEntry, - getChecksumBuilder, - getLookupFn, -} from "@polkadot-api/metadata-builders" +import { LookupEntry, getLookupFn } from "@polkadot-api/metadata-builders" import { V14, V15 } from "@polkadot-api/substrate-bindings" import { state, withDefault } from "@react-rxjs/core" import { combineKeys, createSignal, mergeWithKey } from "@react-rxjs/utils" @@ -14,9 +10,10 @@ import { distinctUntilChanged, map, scan, + withLatestFrom, } from "rxjs" import { selectedChains$ } from "../ChainPicker" -import { metadatas } from "../api/metadatas" +import { checksumBuilders, metadatas } from "../api/metadatas" import { persistSubscription } from "../lib/persistSubscription" export type MetadataEntry = @@ -24,11 +21,11 @@ export type MetadataEntry = export type EnumEntry = LookupEntry & { type: "enum"; entry: MetadataEntry } export const chainTypes$ = state( (chain: string) => - metadatas[chain].pipe( + checksumBuilders[chain].pipe( + withLatestFrom(metadatas[chain]), catchError(() => EMPTY), - map((metadata) => { + map(([checksumBuilder, metadata]) => { const lookup = getLookupFn(metadata.lookup) - const checksumBuilder = getChecksumBuilder(metadata) const result: Record = {} for (let i = 0; i < metadata.lookup.length; i++) { diff --git a/src/CommonTypes/typeReferences.state.ts b/src/CommonTypes/typeReferences.state.ts index d96d348..f97e232 100644 --- a/src/CommonTypes/typeReferences.state.ts +++ b/src/CommonTypes/typeReferences.state.ts @@ -1,12 +1,11 @@ import { EnumVar, LookupEntry, - getChecksumBuilder, getLookupFn, } from "@polkadot-api/metadata-builders" import { state } from "@react-rxjs/core" -import { map } from "rxjs" -import { metadatas } from "../api/metadatas" +import { map, withLatestFrom } from "rxjs" +import { checksumBuilders, metadatas } from "../api/metadatas" type References = { direct: string[]; indirect: string[] } const emptyReferences: References = { direct: [], indirect: [] } @@ -21,13 +20,13 @@ const removeDuplicates = (v: Array) => [...new Set(v)] */ export const typeReferences$ = state( (chain: string) => - metadatas[chain].pipe( - map((metadata) => { + checksumBuilders[chain].pipe( + withLatestFrom(metadatas[chain]), + map(([checksumBuilder, metadata]) => { const result: Record< string, { inputs: References; outputs: References; backRefs: References } > = {} - const checksumBuilder = getChecksumBuilder(metadata) const lookup = getLookupFn(metadata.lookup) const referenceCache: Record = {} @@ -185,6 +184,7 @@ export const typeReferences$ = state( function getEnumInputs(entry: EnumVar["value"][string]): LookupEntry[] { switch (entry.type) { case "lookupEntry": + case "array": return [entry.value] case "struct": return Object.values(entry.value) diff --git a/src/api/metadatas.ts b/src/api/metadatas.ts index 840f091..964e887 100644 --- a/src/api/metadatas.ts +++ b/src/api/metadatas.ts @@ -20,6 +20,7 @@ import { import { selectedChains$ } from "../ChainPicker" import { persistSubscription } from "../lib/persistSubscription" import { chains } from "./smoldot" +import { getChecksumBuilder } from "@polkadot-api/metadata-builders" export const [changeUseCache$, setUseCache] = createSignal() export const useCache$ = state(changeUseCache$, true) @@ -61,6 +62,10 @@ export const metadatas = mapObject(chains, (chain$, key) => { ) }) +export const checksumBuilders = mapObject(metadatas, (metadata$) => + metadata$.pipe(map(getChecksumBuilder), persistSubscription()), +) + export enum LoadStatus { Idle = "Idle", Loading = "Loading",