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

Some web UI fixes #55

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 3 additions & 22 deletions web/components/SqlDataTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import {
} from "@gen/graphql-types";
import useSessionQueryHistory from "@hooks/useSessionQueryHistory";
import { SqlQueryParams } from "@lib/params";
import { isMutation } from "@lib/parseSqlQuery";
import { refetchSqlUpdateQueriesCacheEvict } from "@lib/refetchQueries";
import { useEffect, useState } from "react";
import { useState } from "react";
import SqlMessage from "./SqlMessage";
import { isReadOnlyDatabaseRevisionError } from "./SqlMessage/utils";
import WorkingDiff from "./WorkingDiff";
import css from "./index.module.css";
import useSqlQuery from "./useSqlQuery";

type Props = {
params: SqlQueryParams;
Expand All @@ -37,25 +36,7 @@ type InnerProps = Props & {
};

function Inner(props: InnerProps) {
const { addMutation } = useSessionQueryHistory();
const isMut = isMutation(props.params.q);

useEffect(() => {
if (!isMut) return;
addMutation(props.params.q);
}, [props.params.q]);

useEffect(() => {
if (!isMut) return;
if (props.gqlError) return;
// Need timeout here so that queries are not refetched before sql query has
// time to finish
setTimeout(() => {
props.client
.refetchQueries(refetchSqlUpdateQueriesCacheEvict)
.catch(console.error);
}, 300);
}, [props.gqlError, isMut, props.client]);
const isMut = useSqlQuery(props.params, props.client, props.gqlError);

const msg = <SqlMessage {...props} rowsLen={props.rows?.length ?? 0} />;
return (
Expand Down
54 changes: 54 additions & 0 deletions web/components/SqlDataTable/useSqlQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
ApolloClient,
ApolloError,
NormalizedCacheObject,
} from "@apollo/client";
import useSessionQueryHistory from "@hooks/useSessionQueryHistory";
import { SqlQueryParams } from "@lib/params";
import { isMutation } from "@lib/parseSqlQuery";
import { refetchSqlUpdateQueriesCacheEvict } from "@lib/refetchQueries";
import { databases } from "@lib/urls";
import { useRouter } from "next/router";
import { useEffect } from "react";

export default function useSqlQuery(
params: SqlQueryParams,
client: ApolloClient<NormalizedCacheObject>,
gqlError?: ApolloError,
): boolean {
const router = useRouter();
const { addMutation } = useSessionQueryHistory();
const isMut = isMutation(params.q);

useEffect(() => {
if (!isMut) return;
addMutation(params.q);
}, [params.q]);

useEffect(() => {
if (!isMut) return;
if (gqlError) return;
// Need timeout here so that queries are not refetched before sql query has
// time to finish
setTimeout(() => {
client
.refetchQueries(refetchSqlUpdateQueriesCacheEvict)
.catch(console.error);
}, 300);
}, [gqlError, isMut, client]);

useEffect(() => {
if (gqlError) {
return;
}
const lower = params.q.toLowerCase();
if (
lower.includes("drop database") &&
lower.includes(params.databaseName)
) {
router.push(databases.hrefPathname()).catch(console.error);
}
}, [params.q, params.databaseName, gqlError]);

return isMut;
}
64 changes: 40 additions & 24 deletions web/components/pageComponents/DatabasesPage/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<MainLayout>
<h1>Choose a database</h1>
<p className={css.desc}>
Choose an existing database or create a new database to get started.
</p>
{res.data?.databases.length ? (
<ul>
{res.data.databases.map(db => (
<li key={db}>
<Link {...database({ databaseName: db })}>
<div className={css.database}>
<span>{db}</span>
<span className={css.go}>
Go <FaChevronRight />
</span>
</div>
</Link>
</li>
))}
</ul>
) : (
<p className={cx(css.noDbs, css.database)}>
No databases found. Create a database to get started.
<Loader loaded={!res.loading}>
<h1>Choose a database</h1>
<p className={css.desc}>
Choose an existing database or create a new database to get started.
</p>
)}
<CreateDatabase buttonClassName={css.button} />
{res.data?.databases.length ? (
<ul>
{res.data.databases.map(db => (
<li key={db}>
<Link {...database({ databaseName: db })}>
<div className={css.database}>
<span>{db}</span>
<span className={css.go}>
Go <FaChevronRight />
</span>
</div>
</Link>
</li>
))}
</ul>
) : (
<p className={cx(css.noDbs, css.database)}>
No databases found. Create a database to get started.
</p>
)}
<CreateDatabase buttonClassName={css.button} />
<ErrorMsg err={res.error} />
</Loader>
</MainLayout>
);
}
1 change: 1 addition & 0 deletions web/lib/refetchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const refetchSqlUpdateQueriesCacheEvict: RefetchOptions = {
"schemaDiff",
"doltProcedures",
"doltSchemas",
"databases",
].forEach(fieldName => {
cache.evict({ fieldName });
});
Expand Down