From 463ced5f30d1f8f70617ffb6bf447042a5bcd583 Mon Sep 17 00:00:00 2001 From: karooolis Date: Sat, 28 Jun 2025 16:04:58 +0300 Subject: [PATCH 1/3] add block height param --- packages/explorer/package.json | 1 + .../[worldAddress]/explore/SQLEditor.tsx | 62 ++++++++++++++++++- .../interact/content/encodeFunctionArgs.ts | 25 +++++++- .../(explorer)/queries/useTableDataQuery.ts | 3 + .../explorer/src/components/LatestBlock.tsx | 2 +- .../explorer/src/components/ui/Slider.tsx | 26 ++++++++ 6 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 packages/explorer/src/components/ui/Slider.tsx diff --git a/packages/explorer/package.json b/packages/explorer/package.json index 3dee0f4f51..8667664533 100644 --- a/packages/explorer/package.json +++ b/packages/explorer/package.json @@ -54,6 +54,7 @@ "@radix-ui/react-popover": "^1.1.1", "@radix-ui/react-select": "^2.1.1", "@radix-ui/react-separator": "^1.1.0", + "@radix-ui/react-slider": "^1.3.5", "@radix-ui/react-slot": "^1.1.0", "@radix-ui/react-tooltip": "^1.1.6", "@radix-ui/themes": "^3.0.5", diff --git a/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx b/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx index 96f016a6f7..236042ce82 100644 --- a/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx +++ b/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx @@ -1,7 +1,10 @@ "use client"; -import { CommandIcon, CornerDownLeft, LoaderIcon, PauseIcon, PlayIcon } from "lucide-react"; +import { ClockIcon, CommandIcon, CornerDownLeft, LoaderIcon, PauseIcon, PlayIcon } from "lucide-react"; import { KeyCode, KeyMod, editor } from "monaco-editor/esm/vs/editor/editor.api"; +import { useQueryState } from "nuqs"; +import { useConfig } from "wagmi"; +import { getBlock } from "wagmi/actions"; import { useEffect, useRef, useState } from "react"; import { useForm } from "react-hook-form"; import { Table } from "@latticexyz/config"; @@ -9,7 +12,9 @@ import Editor from "@monaco-editor/react"; import { Tooltip } from "../../../../../../components/Tooltip"; import { Button } from "../../../../../../components/ui/Button"; import { Form, FormField } from "../../../../../../components/ui/Form"; +import { Input } from "../../../../../../components/ui/Input"; import { cn } from "../../../../../../utils"; +import { useChain } from "../../../../hooks/useChain"; import { useTableDataQuery } from "../../../../queries/useTableDataQuery"; import { PAGE_SIZE_OPTIONS, monacoOptions } from "./consts"; import { usePaginationState } from "./hooks/usePaginationState"; @@ -31,6 +36,12 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) { const [isUserTriggeredRefetch, setIsUserTriggeredRefetch] = useState(false); const [pagination, setPagination] = usePaginationState(); const [query, setQuery] = useSQLQueryState(); + const [blockHeight, setBlockHeight] = useQueryState("blockHeight"); + const [blockTimestamp, setBlockTimestamp] = useState(null); + const [isLoadingBlock, setIsLoadingBlock] = useState(false); + + const wagmiConfig = useConfig(); + const { id: chainId } = useChain(); const validateQuery = useQueryValidator(table); const { data: tableData, refetch, isRefetching: isTableDataRefetching } = useTableDataQuery({ table, isLiveQuery }); @@ -76,6 +87,32 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) { form.reset({ query }); }, [query, form]); + // Fetch block timestamp when blockHeight changes + useEffect(() => { + const fetchBlockTimestamp = async () => { + if (!blockHeight || !wagmiConfig || !chainId) { + setBlockTimestamp(null); + return; + } + + setIsLoadingBlock(true); + try { + const block = await getBlock(wagmiConfig, { + chainId, + blockNumber: BigInt(blockHeight), + }); + setBlockTimestamp(Number(block.timestamp)); + } catch (error) { + console.error("Failed to fetch block timestamp:", error); + setBlockTimestamp(null); + } finally { + setIsLoadingBlock(false); + } + }; + + fetchBlockTimestamp(); + }, [blockHeight, wagmiConfig, chainId]); + const updateHeight = () => { if (editorRef.current) { const contentHeight = Math.min(200, editorRef.current.getContentHeight()); @@ -182,6 +219,29 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) { ) : null} + setBlockHeight(e.target.value)} + /> + + {blockHeight && ( +
+ + {isLoadingBlock ? ( + Loading... + ) : blockTimestamp ? ( + + {new Date(blockTimestamp * 1000).toLocaleString()} + + ) : ( + Invalid block + )} +
+ )} +