-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web: Improve create/drop database behavior
- Loading branch information
Showing
3 changed files
with
58 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters