Skip to content

Commit

Permalink
feat(explorer): show explore table error message (#3286)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
karooolis and holic authored Oct 14, 2024
1 parent 60c1f80 commit af72530
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-buckets-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/explorer": patch
---

Display error messages for failed queries within the Explore tab's table viewer.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Hex } from "viem";
import { useEffect } from "react";
import { useChain } from "../../../../hooks/useChain";
import { usePrevious } from "../../../../hooks/usePrevious";
import { useTableDataQuery } from "../../../../queries/useTableDataQuery";
import { useTablesQuery } from "../../../../queries/useTablesQuery";
import { constructTableName } from "../../../../utils/constructTableName";
import { indexerForChainId } from "../../../../utils/indexerForChainId";
Expand All @@ -24,7 +23,6 @@ export function Explorer() {

const { data: tables } = useTablesQuery();
const table = tables?.find(({ tableId }) => tableId === selectedTableId);
const { data: tableData, isLoading, isFetched } = useTableDataQuery({ table, query });

useEffect(() => {
if (table && (!query || prevSelectedTableId !== selectedTableId)) {
Expand All @@ -42,7 +40,7 @@ export function Explorer() {
<>
{indexer.type !== "sqlite" && <SQLEditor />}
<TableSelector tables={tables} />
<TablesViewer table={table} tableData={tableData} isLoading={isLoading || !isFetched} />
<TablesViewer table={table} query={query} />
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrowUpDownIcon, LoaderIcon } from "lucide-react";
import { ArrowUpDownIcon, LoaderIcon, TriangleAlertIcon } from "lucide-react";
import { parseAsJson, parseAsString, useQueryState } from "nuqs";
import { useMemo } from "react";
import { Schema, Table as TableType } from "@latticexyz/config";
Expand All @@ -17,21 +17,23 @@ import { internalNamespaces } from "../../../../../../common";
import { Button } from "../../../../../../components/ui/Button";
import { Input } from "../../../../../../components/ui/Input";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../../../../../../components/ui/Table";
import { TableData } from "../../../../queries/useTableDataQuery";
import { cn } from "../../../../../../utils";
import { TableData, useTableDataQuery } from "../../../../queries/useTableDataQuery";
import { EditableTableCell } from "./EditableTableCell";

const initialSortingState: SortingState = [];
const initialRows: TableData["rows"] = [];

export function TablesViewer({
table,
tableData,
isLoading,
}: {
table?: TableType;
tableData?: TableData;
isLoading: boolean;
}) {
export function TablesViewer({ table, query }: { table?: TableType; query?: string }) {
const {
data: tableData,
isLoading: isTableDataLoading,
isFetched,
isError,
error,
} = useTableDataQuery({ table, query });
const isLoading = isTableDataLoading || !isFetched;

const [globalFilter, setGlobalFilter] = useQueryState("filter", parseAsString.withDefault(""));
const [sorting, setSorting] = useQueryState("sort", parseAsJson<SortingState>().withDefault(initialSortingState));

Expand Down Expand Up @@ -109,7 +111,11 @@ export function TablesViewer({
/>
</div>

<div className="rounded-md border">
<div
className={cn("rounded-md border", {
"border-red-400": isError,
})}
>
{isLoading && (
<div className="flex h-24 items-center justify-center">
<LoaderIcon className="h-5 w-5 animate-spin" />
Expand Down Expand Up @@ -144,8 +150,19 @@ export function TablesViewer({
))
) : (
<TableRow>
<TableCell colSpan={tableColumns.length} className="h-24 text-center">
No results.
<TableCell
colSpan={tableColumns.length}
className={cn("h-24 text-center", {
"text-red-400": isError,
})}
>
{isError ? (
<div className="flex items-center justify-center gap-x-2">
<TriangleAlertIcon /> Query error: {error.message}
</div>
) : (
"No results."
)}
</TableCell>
</TableRow>
)}
Expand Down
12 changes: 10 additions & 2 deletions packages/explorer/src/app/(explorer)/queries/useTableDataQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export function useTableDataQuery({ table, query }: Props) {
]),
});

return response.json();
const data = await response.json();
if (!response.ok) {
throw new Error(data.msg || "Network response was not ok");
}

return data;
},
select: (data: DozerResponse) => {
if (!table || !data?.result?.[0]) return;
Expand All @@ -58,6 +63,9 @@ export function useTableDataQuery({ table, query }: Props) {
};
},
enabled: !!table && !!query,
refetchInterval: 1_000,
refetchInterval: (query) => {
if (query.state.error) return false;
return 1000;
},
});
}

0 comments on commit af72530

Please sign in to comment.