Skip to content

Commit

Permalink
web: Improve create/drop database behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Nov 28, 2023
1 parent c31f004 commit d447806
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
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;
}
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

0 comments on commit d447806

Please sign in to comment.