Skip to content

Commit 722f4b4

Browse files
authored
fix(explorer): type-based sorting (#3340)
1 parent 5340394 commit 722f4b4

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/explorer": patch
3+
---
4+
5+
The columns in the Explore tab table are now sorted correctly according to their types.

packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/TablesViewer.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,26 @@ import { Button } from "../../../../../../components/ui/Button";
1818
import { Input } from "../../../../../../components/ui/Input";
1919
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../../../../../../components/ui/Table";
2020
import { cn } from "../../../../../../utils";
21-
import { TableData, useTableDataQuery } from "../../../../queries/useTableDataQuery";
21+
import { TData, TDataRow, useTableDataQuery } from "../../../../queries/useTableDataQuery";
2222
import { EditableTableCell } from "./EditableTableCell";
23+
import { typeSortingFn } from "./utils/typeSortingFn";
2324

2425
const initialSortingState: SortingState = [];
25-
const initialRows: TableData["rows"] = [];
26+
const initialRows: TData["rows"] = [];
2627

2728
export function TablesViewer({ table, query }: { table?: TableType; query?: string }) {
28-
const {
29-
data: tableData,
30-
isLoading: isTableDataLoading,
31-
isFetched,
32-
isError,
33-
error,
34-
} = useTableDataQuery({ table, query });
35-
const isLoading = isTableDataLoading || !isFetched;
36-
29+
const { data: tableData, isLoading: isTDataLoading, isFetched, isError, error } = useTableDataQuery({ table, query });
30+
const isLoading = isTDataLoading || !isFetched;
3731
const [globalFilter, setGlobalFilter] = useQueryState("filter", parseAsString.withDefault(""));
3832
const [sorting, setSorting] = useQueryState("sort", parseAsJson<SortingState>().withDefault(initialSortingState));
3933

40-
const tableColumns: ColumnDef<Record<string, unknown>>[] = useMemo(() => {
34+
const tableColumns: ColumnDef<TDataRow>[] = useMemo(() => {
4135
if (!table || !tableData) return [];
4236

4337
return tableData.columns.map((name) => {
44-
const type = table?.schema[name]?.type;
38+
const schema = table?.schema[name];
39+
const type = schema?.type;
40+
4541
return {
4642
accessorKey: name,
4743
header: ({ column }) => {
@@ -57,6 +53,7 @@ export function TablesViewer({ table, query }: { table?: TableType; query?: stri
5753
</Button>
5854
);
5955
},
56+
sortingFn: (rowA, rowB, columnId) => typeSortingFn(rowA, rowB, columnId, type),
6057
cell: ({ row }) => {
6158
const namespace = table?.namespace;
6259
const keySchema = getKeySchema(table);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { bigIntSort } from "@latticexyz/common/utils";
2+
import { AbiType } from "@latticexyz/config";
3+
import { Row } from "@tanstack/react-table";
4+
import { TDataRow } from "../../../../../queries/useTableDataQuery";
5+
6+
export function typeSortingFn(rowA: Row<TDataRow>, rowB: Row<TDataRow>, columnId: string, type?: AbiType) {
7+
const a = rowA.getValue(columnId);
8+
const b = rowB.getValue(columnId);
9+
10+
if (type?.startsWith("uint") || type?.startsWith("int")) {
11+
const aBig = BigInt(a?.toString() || "0");
12+
const bBig = BigInt(b?.toString() || "0");
13+
return bigIntSort(aBig, bBig);
14+
}
15+
16+
if (typeof a === "bigint" || typeof b === "bigint") {
17+
const aBig = BigInt(a?.toString() || "0");
18+
const bBig = BigInt(b?.toString() || "0");
19+
return bigIntSort(aBig, bBig);
20+
}
21+
22+
if (a == null) return 1;
23+
if (b == null) return -1;
24+
25+
return a < b ? -1 : a > b ? 1 : 0;
26+
}

packages/explorer/src/app/(explorer)/queries/useTableDataQuery.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ type Props = {
1111
query: string | undefined;
1212
};
1313

14-
export type TableData = {
14+
export type TDataRow = Record<string, unknown>;
15+
export type TData = {
1516
columns: string[];
16-
rows: Record<string, unknown>[];
17+
rows: TDataRow[];
1718
};
1819

1920
export function useTableDataQuery({ table, query }: Props) {
2021
const { chainName, worldAddress } = useParams();
2122
const { id: chainId } = useChain();
2223
const decodedQuery = decodeURIComponent(query ?? "");
2324

24-
return useQuery<DozerResponse, Error, TableData | undefined>({
25+
return useQuery<DozerResponse, Error, TData | undefined>({
2526
queryKey: ["tableData", chainName, worldAddress, decodedQuery],
2627
queryFn: async () => {
2728
const indexer = indexerForChainId(chainId);
@@ -45,7 +46,7 @@ export function useTableDataQuery({ table, query }: Props) {
4546

4647
return data;
4748
},
48-
select: (data: DozerResponse): TableData | undefined => {
49+
select: (data: DozerResponse): TData | undefined => {
4950
if (!table || !data?.result?.[0]) return undefined;
5051

5152
const result = data.result[0];

0 commit comments

Comments
 (0)