From e33760a020eb1023305177864f5e326b2dc21f65 Mon Sep 17 00:00:00 2001 From: ejmg Date: Wed, 17 Jan 2024 21:22:17 -0600 Subject: [PATCH] basic skeleton for enumerating existing ibc connections + api endpoint --- src/app/api/ibc/connections/route.ts | 44 ++++++++++++++++++ src/app/ibc/connections/page.tsx | 45 ++++++++++++++++++- .../ibc/connections/ConnectionsTable.tsx | 20 +++++++++ src/components/ibc/connections/columns.tsx | 24 ++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/app/api/ibc/connections/route.ts create mode 100644 src/components/ibc/connections/ConnectionsTable.tsx create mode 100644 src/components/ibc/connections/columns.tsx diff --git a/src/app/api/ibc/connections/route.ts b/src/app/api/ibc/connections/route.ts new file mode 100644 index 0000000..cedf05f --- /dev/null +++ b/src/app/api/ibc/connections/route.ts @@ -0,0 +1,44 @@ +import db from "@/lib/db"; +import { type NextRequest } from "next/server"; + +export async function GET(_req: NextRequest) { + console.log("SUCCESS: GET /api/ibc/connections"); + try { + console.log("Querying indexer for IBC connections"); + + const connectionsQuery = await db.events.findMany({ + select: { + attributes: { + select: { + key: true, + value: true, + }, + where: { + key: { + equals: "connection_id", + }, + }, + }, + }, + where: { + type: { + equals: "connection_open_init", + }, + }, + orderBy: { + rowid: "desc", + }, + }); + + console.log("Successfully queried for IBC connections.", connectionsQuery); + console.log(connectionsQuery[0].attributes); + + const connections = connectionsQuery.map(({ attributes }) => attributes).flat(); + + return new Response(JSON.stringify(connections)); + + } catch (error) { + console.error("GET request failed.", error); + return new Response("Could not query IBC Connections.", { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/ibc/connections/page.tsx b/src/app/ibc/connections/page.tsx index 1f0d717..6dff903 100644 --- a/src/app/ibc/connections/page.tsx +++ b/src/app/ibc/connections/page.tsx @@ -1,7 +1,50 @@ +"use client"; + +import ConnectionsTable from "@/components/ibc/connections/ConnectionsTable"; +import { useQuery } from "@tanstack/react-query"; +import axios from "axios"; + const Page = () => { + const { data , isFetched, isError } = useQuery({ + queryFn: async () => { + console.log("Fetching: GET /api/ibc/connections"); + const { data } = await axios.get("/api/ibc/connections"); + console.log("Fetched result:", data); + // TODO: enforce validation + return data; + }, + queryKey: ["IbcConnections"], + retry: false, + meta: { + errorMessage: "Failed to query for IBC Connections. Please try again.", + }, + }); + + if (isError) { + return ( +
+

No results found.

+
+ ); + } + return (
-

IBC Connections are not yet implemented...

+ {isFetched ? ( +
+

IBC Connections

+ {// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + data ? ( +
+ +
+ ) : ( +

No results

+ )} +
+ ) : ( +

loading...

+ )}
); }; diff --git a/src/components/ibc/connections/ConnectionsTable.tsx b/src/components/ibc/connections/ConnectionsTable.tsx new file mode 100644 index 0000000..0b3d58c --- /dev/null +++ b/src/components/ibc/connections/ConnectionsTable.tsx @@ -0,0 +1,20 @@ +import { columns } from "./columns"; +import { DataTable } from "../../ui/data-table"; +import { type FC } from "react"; + +interface Props { + data: Array<{ + key: string, + value: string | null, + }>, +} + +const ConnectionsTable : FC = ({ data }) => { + return ( +
+ +
+ ); +}; + +export default ConnectionsTable; \ No newline at end of file diff --git a/src/components/ibc/connections/columns.tsx b/src/components/ibc/connections/columns.tsx new file mode 100644 index 0000000..194b68d --- /dev/null +++ b/src/components/ibc/connections/columns.tsx @@ -0,0 +1,24 @@ +"use client"; + +import { type ColumnDef } from "@tanstack/react-table"; +import Link from "next/link"; + +export type ConnectionsColumns = Record; + +// TODO formating, styling, etc +export const columns : Array> = [ + { + id: "connectionId", + // accessorFn: (row) => row[0].value, + accessorKey: "value", + header: () =>
Channel ID
, + cell: ({ getValue }) => { + // Precondition: value should never be null for `connection_id` + const connectionId = getValue() as string; + return {connectionId}; + }, + }, +]; \ No newline at end of file