From 29b34a076acfdcf8305b5cae4e30e837eae398bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Jedli=C4=8Dka?= Date: Thu, 19 Oct 2023 22:54:45 +0200 Subject: [PATCH 01/21] universal search --- .../update-networks/sources/runtime-spec.ts | 26 +- src/components/NetworkSelect.tsx | 22 +- src/components/ResultLayout.tsx | 11 +- src/components/SearchInput.tsx | 2 +- src/components/TabbedContent.tsx | 8 +- src/components/blocks/BlocksTable.tsx | 24 +- src/components/search/SearchResultsTable.tsx | 163 +++++++++ src/hooks/useNetworks.ts | 4 +- src/hooks/useRootLoaderData.ts | 6 +- src/hooks/useSearch.ts | 13 + src/index.tsx | 1 - src/model/root-loader-data.ts | 2 +- src/networks.json | 21 -- src/react-app-env.d.ts | 6 + src/router.tsx | 128 +++---- src/screens/account.tsx | 4 +- src/screens/block.tsx | 4 +- src/screens/call.tsx | 4 +- src/screens/error.tsx | 28 ++ src/screens/event.tsx | 4 +- src/screens/extrinsic.tsx | 4 +- src/screens/network.tsx | 4 +- src/screens/runtime.tsx | 5 +- src/screens/search.tsx | 294 ++++++++++++---- src/services/accountService.ts | 1 - src/services/balancesService.ts | 6 +- src/services/blocksService.ts | 2 +- src/services/callsService.ts | 48 ++- src/services/eventsService.ts | 54 ++- src/services/extrinsicsService.ts | 43 +-- src/services/networksService.ts | 8 +- src/services/searchService.ts | 315 ++++++++++++++++++ src/services/statsService.ts | 2 +- src/services/transfersService.ts | 2 +- src/utils/assert.ts | 6 +- src/utils/extractConnectionItems.ts | 14 +- src/utils/string.ts | 2 +- src/workers/runtimeSpecWorker.runtime.ts | 12 +- 38 files changed, 1002 insertions(+), 301 deletions(-) create mode 100644 src/components/search/SearchResultsTable.tsx create mode 100644 src/hooks/useSearch.ts create mode 100644 src/screens/error.tsx create mode 100644 src/services/searchService.ts diff --git a/scripts/update-networks/sources/runtime-spec.ts b/scripts/update-networks/sources/runtime-spec.ts index e0a21bb0..8f958811 100644 --- a/scripts/update-networks/sources/runtime-spec.ts +++ b/scripts/update-networks/sources/runtime-spec.ts @@ -1,6 +1,7 @@ import { Metadata, TypeRegistry } from "@polkadot/types"; -import { getRuntimeSpec } from "../../../src/services/runtimeSpecService"; +import { getLatestRuntimeSpecVersion } from "../../../src/services/runtimeSpecService"; +import { fetchArchive } from "../../../src/services/fetchService"; import { Network, SourceData, SourceType } from "../model"; import { log } from "../utils/log"; @@ -12,10 +13,29 @@ export async function getRuntimeSpecData(network: Network): Promise }; try { - const runtimeSpec = await getRuntimeSpec(network.name, "latest"); + const latestRuntimeSpecVersion = await getLatestRuntimeSpecVersion(network.name); + + const response = await fetchArchive<{metadata: {hex: `0x${string}`}[]}>( + network.name, ` + query ($specVersion: Int!) { + metadata(where: {specVersion_eq: $specVersion}, orderBy: specVersion_DESC) { + hex + } + } + `, + { + specVersion: latestRuntimeSpecVersion + } + ); + + const metadataHex = response.metadata[0]?.hex; + + if (!metadataHex) { + throw new Error("Not found"); + } const registry = new TypeRegistry(); - const metadata = new Metadata(registry, runtimeSpec.hex); + const metadata = new Metadata(registry, metadataHex); registry.setMetadata(metadata); const latestMetadata = metadata.asLatest; diff --git a/src/components/NetworkSelect.tsx b/src/components/NetworkSelect.tsx index 15411c67..1dac9a4b 100644 --- a/src/components/NetworkSelect.tsx +++ b/src/components/NetworkSelect.tsx @@ -1,6 +1,7 @@ /** @jsxImportSource @emotion/react */ import { useCallback, useEffect } from "react"; import { Divider, ListItemIcon, ListItemText, ListSubheader, MenuItem, Select, SelectProps } from "@mui/material"; +import { BlurOn as AllIcon } from "@mui/icons-material"; import { css } from "@emotion/react"; import { useNetworks } from "../hooks/useNetworks"; @@ -34,24 +35,20 @@ const iconStyle = css` `; type NetworkSelectProps = Omit & { - value?: string; - onChange?: (value: string, isUserAction: boolean) => void; + value: string | undefined; + onChange: (value: string, isUserAction: boolean) => void; }; const NetworkSelect = (props: NetworkSelectProps) => { const { value, onChange, ...selectProps } = props; - const networks = useNetworks(); - const networkGroups = useNetworkGroups(); useEffect(() => { - const network = networks.find((it) => it.name === value); - - if (!network && onChange && networks.length > 0) { - onChange(networkGroups[0]!.networks[0]!.name, false); + if (!value) { + onChange("*", false); } - }, [value, onChange, networkGroups]); + }, [value, onChange]); const handleNetworkChange = useCallback( (e: any) => { @@ -67,6 +64,13 @@ const NetworkSelect = (props: NetworkSelectProps) => { value={value || ""} css={selectStyle} > + + + + + All networks + + {networkGroups.map((group, index) => [ index > 0 && , diff --git a/src/components/ResultLayout.tsx b/src/components/ResultLayout.tsx index 0067414a..6e3d4d30 100644 --- a/src/components/ResultLayout.tsx +++ b/src/components/ResultLayout.tsx @@ -157,13 +157,7 @@ const footerStyle = css` } `; -export type ResultLayoutLoaderData = { - network?: Network; -}; - export const ResultLayout = () => { - const {network} = useLoaderData() as ResultLayoutLoaderData; - return (
@@ -176,13 +170,12 @@ export const ResultLayout = () => {
- +
- {network && } - {!network && } +