From c31f004a9fd9f884f0b3365b7954e270bb5953b3 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Mon, 27 Nov 2023 16:09:39 -0800 Subject: [PATCH] web: If one database automatically connect --- .../pageComponents/DatabasesPage/index.tsx | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/web/components/pageComponents/DatabasesPage/index.tsx b/web/components/pageComponents/DatabasesPage/index.tsx index d459ed22..5719ed9e 100644 --- a/web/components/pageComponents/DatabasesPage/index.tsx +++ b/web/components/pageComponents/DatabasesPage/index.tsx @@ -1,42 +1,58 @@ import CreateDatabase from "@components/CreateDatabase"; +import ErrorMsg from "@components/ErrorMsg"; +import Loader from "@components/Loader"; import MainLayout from "@components/layouts/MainLayout"; import Link from "@components/links/Link"; import { useDatabasesQuery } from "@gen/graphql-types"; import { database } from "@lib/urls"; import { FaChevronRight } from "@react-icons/all-files/fa/FaChevronRight"; import cx from "classnames"; +import { useRouter } from "next/router"; +import { useEffect } from "react"; import css from "./index.module.css"; export default function DatabasesPage() { const res = useDatabasesQuery(); + const router = useRouter(); + + useEffect(() => { + if (!res.data) return; + if (res.data.databases.length === 1) { + const { href, as } = database({ databaseName: res.data.databases[0] }); + router.push(href, as).catch(console.error); + } + }, [res.data?.databases]); return ( -

Choose a database

-

- Choose an existing database or create a new database to get started. -

- {res.data?.databases.length ? ( - - ) : ( -

- No databases found. Create a database to get started. + +

Choose a database

+

+ Choose an existing database or create a new database to get started.

- )} - + {res.data?.databases.length ? ( +
    + {res.data.databases.map(db => ( +
  • + +
    + {db} + + Go + +
    + +
  • + ))} +
+ ) : ( +

+ No databases found. Create a database to get started. +

+ )} + + +
); }