Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

move useFreeProjectLimitCheck to react-query

Alaister Younga79e08127268
diff --git a/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx b/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx
index 1392bbd62e9..8181045826d 100644
--- a/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx
+++ b/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx
@@ -4,11 +4,12 @@ import { Modal, Button, IconPauseCircle } from 'ui'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { Project } from 'types'
-import { checkPermissions, useStore, useFlag, useFreeProjectLimitCheck } from 'hooks'
+import { checkPermissions, useStore, useFlag } from 'hooks'
import { post } from 'lib/common/fetch'
import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { DeleteProjectButton } from 'components/interfaces/Settings/General'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
+import { useFreeProjectLimitCheckQuery } from 'data/organizations/free-project-limit-check-query'
interface Props {
project: Project
@@ -19,7 +20,7 @@ const ProjectPausedState: FC<Props> = ({ project }) => {
const orgSlug = ui.selectedOrganization?.slug
const kpsEnabled = useFlag('initWithKps')
- const { membersExceededLimit } = useFreeProjectLimitCheck(orgSlug)
+ const { data: membersExceededLimit } = useFreeProjectLimitCheckQuery({ slug: orgSlug })
const hasMembersExceedingFreeTierLimit = (membersExceededLimit || []).length > 0
const [showConfirmRestore, setShowConfirmRestore] = useState(false)
@@ -49,7 +50,7 @@ const ProjectPausedState: FC<Props> = ({ project }) => {
return (
<>
<div className="space-y-4">
- <div className="mx-auto mb-16 w-full max-w-7xl">
+ <div className="w-full mx-auto mb-16 max-w-7xl">
<div className="mx-6 flex h-[500px] items-center justify-center rounded border border-scale-400 bg-scale-300 p-8">
<div className="grid w-[420px] gap-4">
<div className="mx-auto flex max-w-[300px] items-center justify-center space-x-4 lg:space-x-8">
@@ -112,14 +113,14 @@ const ProjectPausedState: FC<Props> = ({ project }) => {
header="Your organization has members who have exceeded their free project limits"
onCancel={() => setShowFreeProjectLimitWarning(false)}
>
- <div className="space-y-4 py-4">
+ <div className="py-4 space-y-4">
<Modal.Content>
<div className="space-y-2">
<p className="text-sm text-scale-1100">
The following members have reached their maximum limits for the number of active
free tier projects within organizations where they are an administrator or owner:
</p>
- <ul className="list-disc pl-5 text-sm text-scale-1100">
+ <ul className="pl-5 text-sm list-disc text-scale-1100">
{(membersExceededLimit || []).map((member, idx: number) => (
<li key={`member-${idx}`}>
{member.username || member.primary_email} (Limit: {member.free_project_limit}{' '}
diff --git a/studio/data/organizations/free-project-limit-check-query.ts b/studio/data/organizations/free-project-limit-check-query.ts
new file mode 100644
index 00000000000..182e38ab065
--- /dev/null
+++ b/studio/data/organizations/free-project-limit-check-query.ts
@@ -0,0 +1,69 @@
+import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'lib/common/fetch'
+import { API_URL } from 'lib/constants'
+import { useCallback } from 'react'
+import { organizationKeys } from './keys'
+
+export type MemberWithFreeProjectLimit = {
+ free_project_limit: number
+ primary_email: string
+ username: string
+}
+
+export type FreeProjectLimitCheckVariables = {
+ slug?: string
+}
+
+export type FreeProjectLimitCheckResponse = MemberWithFreeProjectLimit[]
+
+export async function getFreeProjectLimitCheck(
+ { slug }: FreeProjectLimitCheckVariables,
+ signal?: AbortSignal
+) {
+ if (!slug) {
+ throw new Error('slug is required')
+ }
+
+ const response = await get(
+ `${API_URL}/organizations/${slug}/members/reached-free-project-limit`,
+ {
+ signal,
+ }
+ )
+ if (response.error) {
+ throw response.error
+ }
+
+ return response as FreeProjectLimitCheckResponse
+}
+
+export type FreeProjectLimitCheckData = Awaited<ReturnType<typeof getFreeProjectLimitCheck>>
+export type FreeProjectLimitCheckError = unknown
+
+export const useFreeProjectLimitCheckQuery = <TData = FreeProjectLimitCheckData>(
+ { slug }: FreeProjectLimitCheckVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<FreeProjectLimitCheckData, FreeProjectLimitCheckError, TData> = {}
+) =>
+ useQuery<FreeProjectLimitCheckData, FreeProjectLimitCheckError, TData>(
+ organizationKeys.freeProjectLimitCheck(slug),
+ ({ signal }) => getFreeProjectLimitCheck({ slug }, signal),
+ {
+ enabled: enabled && typeof slug !== 'undefined',
+ ...options,
+ }
+ )
+
+export const useFreeProjectLimitCheckPrefetch = ({ slug }: FreeProjectLimitCheckVariables) => {
+ const client = useQueryClient()
+
+ return useCallback(() => {
+ if (slug) {
+ client.prefetchQuery(organizationKeys.freeProjectLimitCheck(slug), ({ signal }) =>
+ getFreeProjectLimitCheck({ slug }, signal)
+ )
+ }
+ }, [slug])
+}
diff --git a/studio/data/organizations/keys.ts b/studio/data/organizations/keys.ts
index 11952afb649..5b7250c028d 100644
--- a/studio/data/organizations/keys.ts
+++ b/studio/data/organizations/keys.ts
@@ -1,4 +1,6 @@
export const organizationKeys = {
detail: (slug: string | undefined) => ['organizations', slug, 'detail'] as const,
roles: (slug: string | undefined) => ['organizations', slug, 'roles'] as const,
+ freeProjectLimitCheck: (slug: string | undefined) =>
+ ['organizations', slug, 'free-project-limit-check'] as const,
}
diff --git a/studio/hooks/queries/index.ts b/studio/hooks/queries/index.ts
index 391035b5bac..14a5c4db7f1 100644
--- a/studio/hooks/queries/index.ts
+++ b/studio/hooks/queries/index.ts
@@ -1,5 +1,4 @@
export * from './useJwtSecretUpdateStatus'
-export * from './useFreeProjectLimitCheck'
export * from './useProjectAuthConfig'
export * from './useProjectSubscription'
export * from './useSubscriptionStats'
diff --git a/studio/hooks/queries/useFreeProjectLimitCheck.ts b/studio/hooks/queries/useFreeProjectLimitCheck.ts
deleted file mode 100644
index 3884388ebdd..00000000000
--- a/studio/hooks/queries/useFreeProjectLimitCheck.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import useSWR from 'swr'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
-
-export interface MemberWithFreeProjectLimit {
- free_project_limit: number
- primary_email: string
- username: string
-}
-
-export function useFreeProjectLimitCheck(slug?: string) {
- const url = `${API_URL}/organizations/${slug}/members/reached-free-project-limit`
- const { data, error } = useSWR<any>(slug ? url : null, get)
- const anyError = data?.error || error
-
- return {
- membersExceededLimit: anyError ? undefined : (data as MemberWithFreeProjectLimit[]),
- error: anyError,
- isLoading: !anyError && !data,
- isError: !!anyError,
- }
-}
diff --git a/studio/pages/new/[slug].tsx b/studio/pages/new/[slug].tsx
index 0578455f4f4..64bf710eff1 100644
--- a/studio/pages/new/[slug].tsx
+++ b/studio/pages/new/[slug].tsx
@@ -27,8 +27,9 @@ import {
withAuth,
useSubscriptionStats,
checkPermissions,
- useFreeProjectLimitCheck,
+ useParams,
} from 'hooks'
+import { useFreeProjectLimitCheckQuery } from 'data/organizations/free-project-limit-check-query'
import { WizardLayoutWithoutAuth } from 'components/layouts'
import Panel from 'components/ui/Panel'
@@ -42,14 +43,14 @@ import {
const Wizard: NextPageWithLayout = () => {
const router = useRouter()
- const { slug } = router.query
+ const { slug } = useParams()
const { app, ui } = useStore()
const projectCreationDisabled = useFlag('disableProjectCreationAndUpdate')
const kpsEnabled = useFlag('initWithKps')
const subscriptionStats = useSubscriptionStats()
- const { membersExceededLimit, isLoading: isLoadingFreeProjectLimitCheck } =
- useFreeProjectLimitCheck(slug as string)
+ const { data: membersExceededLimit, isLoading: isLoadingFreeProjectLimitCheck } =
+ useFreeProjectLimitCheckQuery({ slug })
const [projectName, setProjectName] = useState('')
const [dbPass, setDbPass] = useState('')
@@ -213,7 +214,7 @@ const Wizard: NextPageWithLayout = () => {
</div>
}
footer={
- <div key="panel-footer" className="flex w-full items-center justify-between">
+ <div key="panel-footer" className="flex items-center justify-between w-full">
<Button type="default" onClick={() => Router.push('/projects')}>
Cancel
</Button>
@@ -242,7 +243,7 @@ const Wizard: NextPageWithLayout = () => {
</p>
</Panel.Content>
{projectCreationDisabled ? (
- <Panel.Content className="border-t border-panel-border-interior-light pb-8 dark:border-panel-border-interior-dark">
+ <Panel.Content className="pb-8 border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">
<DisabledWarningDueToIncident title="Project creation is currently disabled" />
</Panel.Content>
) : (
@@ -296,7 +297,7 @@ const Wizard: NextPageWithLayout = () => {
/>
</Panel.Content>
- <Panel.Content className="Form section-block--body has-inputs-centered border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">
+ <Panel.Content className="border-b Form section-block--body has-inputs-centered border-panel-border-interior-light dark:border-panel-border-interior-dark">
<Input
id="password"
copy={dbPass.length > 0}
@@ -318,7 +319,7 @@ const Wizard: NextPageWithLayout = () => {
/>
</Panel.Content>
- <Panel.Content className="Form section-block--body has-inputs-centered border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">
+ <Panel.Content className="border-b Form section-block--body has-inputs-centered border-panel-border-interior-light dark:border-panel-border-interior-dark">
<Listbox
layout="horizontal"
label="Region"
diff --git a/studio/pages/project/[ref]/settings/billing/update/index.tsx b/studio/pages/project/[ref]/settings/billing/update/index.tsx
index 70859ccf8dd..00851292642 100644
--- a/studio/pages/project/[ref]/settings/billing/update/index.tsx
+++ b/studio/pages/project/[ref]/settings/billing/update/index.tsx
@@ -3,7 +3,8 @@ import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
import { Button, Modal } from 'ui'
-import { useFreeProjectLimitCheck, useStore } from 'hooks'
+import { useStore } from 'hooks'
+import { useFreeProjectLimitCheckQuery } from 'data/organizations/free-project-limit-check-query'
import { get } from 'lib/common/fetch'
import { API_URL, PRICING_TIER_PRODUCT_IDS, STRIPE_PRODUCT_IDS } from 'lib/constants'
@@ -19,7 +20,7 @@ const BillingUpdate: NextPageWithLayout = () => {
const orgSlug = ui.selectedOrganization?.slug
const projectRef = ui.selectedProject?.ref
- const { membersExceededLimit } = useFreeProjectLimitCheck(orgSlug as string)
+ const { data: membersExceededLimit } = useFreeProjectLimitCheckQuery({ slug: orgSlug })
const hasMembersExceedingFreeTierLimit = (membersExceededLimit || []).length > 0
const [isLoadingProducts, setIsLoadingProducts] = useState(false)
@@ -103,7 +104,7 @@ const BillingUpdate: NextPageWithLayout = () => {
if (isLoadingProducts || isEnterprise) {
return (
- <div className="flex h-full w-full items-center justify-center">
+ <div className="flex items-center justify-center w-full h-full">
<Connecting />
</div>
)
@@ -111,7 +112,7 @@ const BillingUpdate: NextPageWithLayout = () => {
return (
<>
- <div className="mx-auto my-10 max-w-6xl px-6">
+ <div className="max-w-6xl px-6 mx-auto my-10">
<PlanSelection
visible={!selectedPlan || (selectedPlan && showConfirmDowngrade)}
tiers={products?.tiers ?? []}
@@ -142,14 +143,14 @@ const BillingUpdate: NextPageWithLayout = () => {
header="Your organization has members who have exceeded their free project limits"
onCancel={() => setShowDowngradeError(false)}
>
- <div className="space-y-4 py-4">
+ <div className="py-4 space-y-4">
<Modal.Content>
<div className="space-y-2">
<p className="text-sm text-scale-1100">
The following members have reached their maximum limits for the number of active
free tier projects within organizations where they are an administrator or owner:
</p>
- <ul className="list-disc pl-5 text-sm text-scale-1100">
+ <ul className="pl-5 text-sm list-disc text-scale-1100">
{(membersExceededLimit || []).map((member, idx: number) => (
<li key={`member-${idx}`}>
{member.username || member.primary_email} (Limit: {member.free_project_limit}{' '}