Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(explorer): pause/resume live query #3445

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-rocks-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/explorer": patch
---

SQL live queries in the Explore view table can now be paused and resumed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useParams } from "next/navigation";
import { parseAsString, useQueryState } from "nuqs";
import { Hex } from "viem";
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useChain } from "../../../../hooks/useChain";
import { usePrevious } from "../../../../hooks/usePrevious";
import { useTablesQuery } from "../../../../queries/useTablesQuery";
Expand All @@ -17,6 +17,7 @@ export function Explorer() {
const { worldAddress } = useParams();
const { id: chainId } = useChain();
const indexer = indexerForChainId(chainId);
const [isPaused, setIsPaused] = useState(true);
const [query, setQuery] = useQueryState("query", parseAsString.withDefault(""));
const [selectedTableId] = useQueryState("tableId");
const prevSelectedTableId = usePrevious(selectedTableId);
Expand All @@ -40,8 +41,8 @@ export function Explorer() {
return (
<div className="space-y-4">
<TableSelector tables={tables} />
{indexer.type !== "sqlite" && <SQLEditor table={table} />}
<TablesViewer table={table} query={query} />
{indexer.type !== "sqlite" && <SQLEditor table={table} isPaused={isPaused} setIsPaused={setIsPaused} />}
<TablesViewer table={table} query={query} isPaused={isPaused} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { CommandIcon, CornerDownLeft } from "lucide-react";
import { CommandIcon, CornerDownLeft, PauseIcon, PlayIcon } from "lucide-react";
import { KeyCode, KeyMod, editor } from "monaco-editor/esm/vs/editor/editor.api";
import { useQueryState } from "nuqs";
import { useEffect, useRef, useState } from "react";
Expand All @@ -16,9 +16,11 @@ import { useQueryValidator } from "./useQueryValidator";

type Props = {
table?: Table;
isPaused: boolean;
setIsPaused: (isPaused: boolean) => void;
};

export function SQLEditor({ table }: Props) {
export function SQLEditor({ table, isPaused, setIsPaused }: Props) {
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
const containerRef = useRef<HTMLDivElement>(null);
const [isFocused, setIsFocused] = useState(false);
Expand Down Expand Up @@ -98,7 +100,11 @@ export function SQLEditor({ table }: Props) {
/>
</div>

<div className="flex justify-end">
<div className="flex justify-end gap-2">
<Button variant="ghost" size="icon" onClick={() => setIsPaused(!isPaused)}>
karooolis marked this conversation as resolved.
Show resolved Hide resolved
{isPaused ? <PlayIcon className="h-4 w-4" /> : <PauseIcon className="h-4 w-4" />}
</Button>

<Button className="flex gap-2 pl-4 pr-3" type="submit">
Run
<span className="flex items-center gap-0.5 text-white/60">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ import { typeSortingFn } from "./utils/typeSortingFn";
const initialSortingState: SortingState = [];
const initialRows: TData["rows"] = [];

export function TablesViewer({ table, query }: { table?: TableType; query?: string }) {
const { data: tableData, isLoading: isTDataLoading, isFetched, isError, error } = useTableDataQuery({ table, query });
type Props = {
table?: TableType;
query?: string;
isPaused: boolean;
};

export function TablesViewer({ table, query, isPaused }: Props) {
const {
data: tableData,
isLoading: isTDataLoading,
isFetched,
isError,
error,
} = useTableDataQuery({ table, query, isPaused });
const isLoading = isTDataLoading || !isFetched;
const [globalFilter, setGlobalFilter] = useQueryState("filter", parseAsString.withDefault(""));
const [sorting, setSorting] = useQueryState("sort", parseAsJson<SortingState>().withDefault(initialSortingState));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { indexerForChainId } from "../utils/indexerForChainId";
type Props = {
table: Table | undefined;
query: string | undefined;
isPaused: boolean;
};

export type TDataRow = Record<string, unknown>;
Expand All @@ -17,7 +18,7 @@ export type TData = {
rows: TDataRow[];
};

export function useTableDataQuery({ table, query }: Props) {
export function useTableDataQuery({ table, query, isPaused }: Props) {
const { chainName, worldAddress } = useParams();
const { id: chainId } = useChain();
const decodedQuery = decodeURIComponent(query ?? "");
Expand Down Expand Up @@ -83,6 +84,7 @@ export function useTableDataQuery({ table, query }: Props) {
enabled: !!table && !!query,
refetchInterval: (query) => {
if (query.state.error) return false;
else if (isPaused) return false;
return 1000;
},
});
Expand Down
Loading