The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
chore(studio): Deprecate usage of useQueryStateWithSelect in more files (#42500)
Joshen Lim5e2e6690adb2
diff --git a/apps/studio/components/interfaces/APIKeys/CreatePublishableAPIKeyDialog.tsx b/apps/studio/components/interfaces/APIKeys/CreatePublishableAPIKeyDialog.tsxindex 7c430bd6cf8..34a4162004f 100644--- a/apps/studio/components/interfaces/APIKeys/CreatePublishableAPIKeyDialog.tsx+++ b/apps/studio/components/interfaces/APIKeys/CreatePublishableAPIKeyDialog.tsx@@ -1,11 +1,9 @@import { zodResolver } from '@hookform/resolvers/zod'+import { useAPIKeyCreateMutation } from 'data/api-keys/api-key-create-mutation'import { Plus } from 'lucide-react'import { useParams } from 'next/navigation'import { parseAsString, useQueryState } from 'nuqs'import { SubmitHandler, useForm } from 'react-hook-form'-import * as z from 'zod'--import { useAPIKeyCreateMutation } from 'data/api-keys/api-key-create-mutation'import {Button,Dialog,@@ -17,12 +15,13 @@ import {DialogSectionSeparator,DialogTitle,DialogTrigger,+ Form_Shadcn_,FormControl_Shadcn_,FormField_Shadcn_,- Form_Shadcn_,Input_Shadcn_,} from 'ui'import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'+import * as z from 'zod'const FORM_ID = 'create-publishable-api-key'const SCHEMA = z.object({@@ -38,10 +37,7 @@ export const CreatePublishableAPIKeyDialog = () => {const params = useParams()const projectRef = params?.ref as string- const [visible, setVisible] = useQueryState(- 'new',- parseAsString.withDefault('').withOptions({ history: 'push', clearOnDefault: true })- )+ const [visible, setVisible] = useQueryState('new', parseAsString.withDefault(''))const onOpenChange = (value: boolean) => {if (value) setVisible('publishable')diff --git a/apps/studio/components/interfaces/APIKeys/CreateSecretAPIKeyDialog.tsx b/apps/studio/components/interfaces/APIKeys/CreateSecretAPIKeyDialog.tsxindex b44779eaf56..6f6c31ae275 100644--- a/apps/studio/components/interfaces/APIKeys/CreateSecretAPIKeyDialog.tsx+++ b/apps/studio/components/interfaces/APIKeys/CreateSecretAPIKeyDialog.tsx@@ -1,12 +1,10 @@import { zodResolver } from '@hookform/resolvers/zod'+import { useParams } from 'common'+import { useAPIKeyCreateMutation } from 'data/api-keys/api-key-create-mutation'import { Plus, ShieldCheck } from 'lucide-react'import { parseAsString, useQueryState } from 'nuqs'-import { type SubmitHandler, useForm } from 'react-hook-form'+import { useForm, type SubmitHandler } from 'react-hook-form'import { toast } from 'sonner'-import * as z from 'zod'--import { useParams } from 'common'-import { useAPIKeyCreateMutation } from 'data/api-keys/api-key-create-mutation'import {Alert_Shadcn_,AlertDescription_Shadcn_,@@ -27,6 +25,7 @@ import {Input_Shadcn_,} from 'ui'import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'+import * as z from 'zod'const NAME_SCHEMA = z.string()@@ -47,10 +46,7 @@ const SCHEMA = z.object({export const CreateSecretAPIKeyDialog = () => {const { ref: projectRef } = useParams()- const [visible, setVisible] = useQueryState(- 'new',- parseAsString.withDefault('').withOptions({ history: 'push', clearOnDefault: true })- )+ const [visible, setVisible] = useQueryState('new', parseAsString)const onOpenChange = (value: boolean) => {if (value) setVisible('secret')diff --git a/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsx b/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsxindex 3870dea6bab..2473ec03868 100644--- a/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsx+++ b/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsx@@ -1,7 +1,4 @@import { PermissionAction } from '@supabase/shared-types/out/constants'-import { useMemo, useRef } from 'react'-import { toast } from 'sonner'-import { useParams } from 'common'import { AlertError } from 'components/ui/AlertError'import { FormHeader } from 'components/ui/Forms/FormHeader'@@ -9,7 +6,9 @@ import { NoPermission } from 'components/ui/NoPermission'import { useAPIKeyDeleteMutation } from 'data/api-keys/api-key-delete-mutation'import { APIKeysData, useAPIKeysQuery } from 'data/api-keys/api-keys-query'import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'-import { handleErrorOnDelete, useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'+import { parseAsString, useQueryState } from 'nuqs'+import { useEffect, useMemo } from 'react'+import { toast } from 'sonner'import {Card,Table,@@ -21,6 +20,7 @@ import {TableRow,} from 'ui'import { Admonition, GenericSkeletonLoader } from 'ui-patterns'+import { APIKeyRow } from './APIKeyRow'import { CreatePublishableAPIKeyDialog } from './CreatePublishableAPIKeyDialog'@@ -34,6 +34,7 @@ export const PublishableAPIKeys = () => {const {data: apiKeysData = [],error,+ isSuccess: isSuccessApiKeys,isPending: isLoadingApiKeys,isError: isErrorApiKeys,} = useAPIKeysQuery({ projectRef, reveal: false }, { enabled: canReadAPIKeys })@@ -53,35 +54,33 @@ export const PublishableAPIKeys = () => {[apiKeysData])- // Track the ID being deleted to exclude it from error checking- const deletingAPIKeyIdRef = useRef<string | null>(null)-- const { value: apiKeyToDelete, setValue: setAPIKeyToDelete } = useQueryStateWithSelect({- urlKey: 'deletePublishableKey',- select: (id: string) => (id ? publishableApiKeys?.find((key) => key.id === id) : undefined),- enabled: !!publishableApiKeys?.length,- onError: (_error, selectedId) => {- handleErrorOnDelete(deletingAPIKeyIdRef, selectedId, `API Key not found`)- },- })+ const [deleteId, setDeleteId] = useQueryState('deletePublishableKey', parseAsString)+ const apiKeyToDelete = publishableApiKeys?.find((key) => key.id === deleteId)- const { mutate: deleteAPIKey, isPending: isDeletingAPIKey } = useAPIKeyDeleteMutation({+ const {+ mutate: deleteAPIKey,+ isPending: isDeletingAPIKey,+ isSuccess: isDeleteSuccess,+ } = useAPIKeyDeleteMutation({onSuccess: () => {toast.success('Successfully deleted publishable key')- setAPIKeyToDelete(null)- },- onError: () => {- deletingAPIKeyIdRef.current = null+ setDeleteId(null)},})const onDeleteAPIKey = (apiKey: Extract<APIKeysData[number], { type: 'publishable' }>) => {if (!projectRef) return console.error('Project ref is required')if (!apiKey.id) return console.error('API key ID is required')- deletingAPIKeyIdRef.current = apiKey.iddeleteAPIKey({ projectRef, id: apiKey.id })}+ useEffect(() => {+ if (isSuccessApiKeys && !!deleteId && !apiKeyToDelete && !isDeleteSuccess) {+ toast('Unable to find publishable key')+ setDeleteId(null)+ }+ }, [apiKeyToDelete, deleteId, isDeleteSuccess, isSuccessApiKeys, setDeleteId])+return (<div><FormHeader@@ -125,7 +124,7 @@ export const PublishableAPIKeys = () => {isDeleting={apiKeyToDelete?.id === apiKey.id && isDeletingAPIKey}isDeleteModalOpen={apiKeyToDelete?.id === apiKey.id}onDelete={() => onDeleteAPIKey(apiKey)}- setKeyToDelete={setAPIKeyToDelete}+ setKeyToDelete={setDeleteId}/>))}</TableBody>diff --git a/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx b/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsxindex eaaba239b28..5d8967f754e 100644--- a/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx+++ b/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx@@ -1,8 +1,4 @@import { PermissionAction } from '@supabase/shared-types/out/constants'-import dayjs from 'dayjs'-import { useMemo, useRef } from 'react'-import { toast } from 'sonner'-import { useFlag, useParams } from 'common'import { AlertError } from 'components/ui/AlertError'import { FormHeader } from 'components/ui/Forms/FormHeader'@@ -10,9 +6,12 @@ import { NoPermission } from 'components/ui/NoPermission'import { useAPIKeyDeleteMutation } from 'data/api-keys/api-key-delete-mutation'import type { APIKeysData } from 'data/api-keys/api-keys-query'import { useAPIKeysQuery } from 'data/api-keys/api-keys-query'+import dayjs from 'dayjs'import { useLogsQuery } from 'hooks/analytics/useLogsQuery'import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'-import { handleErrorOnDelete, useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'+import { parseAsString, useQueryState } from 'nuqs'+import { useEffect, useMemo, useRef } from 'react'+import { toast } from 'sonner'import { Card } from 'ui'import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'import {@@ -22,6 +21,7 @@ import {TableHeader,TableRow,} from 'ui/src/components/shadcn/ui/table'+import { APIKeyRow } from './APIKeyRow'import { CreateSecretAPIKeyDialog } from './CreateSecretAPIKeyDialog'@@ -77,6 +77,7 @@ export const SecretAPIKeys = () => {const {data: apiKeysData,error,+ isSuccess: isSuccessApiKeys,isPending: isLoadingApiKeys,isError: isErrorApiKeys,} = useAPIKeysQuery({ projectRef, reveal: false }, { enabled: canReadAPIKeys })@@ -97,34 +98,33 @@ export const SecretAPIKeys = () => {const empty = secretApiKeys?.length === 0 && !isLoadingApiKeys && !isLoadingPermissions- // Track the ID being deleted to exclude it from error checking- const deletingAPIKeyIdRef = useRef<string | null>(null)+ const [deleteId, setDeleteId] = useQueryState('deleteSecretKey', parseAsString)+ const apiKeyToDelete = secretApiKeys?.find((key) => key.id === deleteId)- const { setValue: setAPIKeyToDelete, value: apiKeyToDelete } = useQueryStateWithSelect({- urlKey: 'deleteSecretKey',- select: (id: string) => (id ? secretApiKeys?.find((key) => key.id === id) : undefined),- enabled: !!secretApiKeys?.length,- onError: (_error, selectedId) =>- handleErrorOnDelete(deletingAPIKeyIdRef, selectedId, `API Key not found`),- })-- const { mutate: deleteAPIKey, isPending: isDeletingAPIKey } = useAPIKeyDeleteMutation({+ const {+ mutate: deleteAPIKey,+ isPending: isDeletingAPIKey,+ isSuccess: isDeleteSuccess,+ } = useAPIKeyDeleteMutation({onSuccess: () => {toast.success('Successfully deleted secret key')- setAPIKeyToDelete(null)- },- onError: () => {- deletingAPIKeyIdRef.current = null+ setDeleteId(null)},})const onDeleteAPIKey = (apiKey: Extract<APIKeysData[number], { type: 'secret' }>) => {if (!projectRef) return console.error('Project ref is required')if (!apiKey.id) return console.error('API key ID is required')- deletingAPIKeyIdRef.current = apiKey.iddeleteAPIKey({ projectRef, id: apiKey.id })}+ useEffect(() => {+ if (isSuccessApiKeys && !!deleteId && !apiKeyToDelete && !isDeleteSuccess) {+ toast('Unable to find secret key')+ setDeleteId(null)+ }+ }, [apiKeyToDelete, deleteId, isDeleteSuccess, isSuccessApiKeys, setDeleteId])+return (<div className="pb-30"><FormHeader@@ -171,7 +171,7 @@ export const SecretAPIKeys = () => {isLoadingLastSeen={isLoadingLastSeen}isDeleting={apiKeyToDelete?.id === apiKey.id && isDeletingAPIKey}onDelete={() => onDeleteAPIKey(apiKey)}- setKeyToDelete={setAPIKeyToDelete}+ setKeyToDelete={setDeleteId}isDeleteModalOpen={apiKeyToDelete?.id === apiKey.id}/>))}diff --git a/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx b/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsxindex cbd1ad914a1..61d28ee3c5f 100644--- a/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx+++ b/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx@@ -1,14 +1,13 @@-import { useState } from 'react'-import { toast } from 'sonner'-import { useParams } from 'common'import AlertError from 'components/ui/AlertError'import CodeEditor from 'components/ui/CodeEditor/CodeEditor'import { useAuthConfigQuery } from 'data/auth/auth-config-query'import { useAuthHooksUpdateMutation } from 'data/auth/auth-hooks-update-mutation'import { executeSql } from 'data/sql/execute-sql-query'-import { useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'+import { parseAsString, useQueryState } from 'nuqs'+import { useEffect, useState } from 'react'+import { toast } from 'sonner'import { cn } from 'ui'import { EmptyStatePresentational, GenericSkeletonLoader } from 'ui-patterns'import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'@@ -20,10 +19,11 @@ import {PageSectionSummary,PageSectionTitle,} from 'ui-patterns/PageSection'+import { AddHookDropdown } from './AddHookDropdown'import { CreateHookSheet } from './CreateHookSheet'import { HookCard } from './HookCard'-import { HOOKS_DEFINITIONS, Hook } from './hooks.constants'+import { Hook, HOOKS_DEFINITIONS } from './hooks.constants'import { extractMethod, getRevokePermissionStatements, isValidHook } from './hooks.utils'export const HooksListing = () => {@@ -36,6 +36,8 @@ export const HooksListing = () => {isPending: isLoading,} = useAuthConfigQuery({ projectRef })+ const [hook, setHook] = useQueryState('hook', parseAsString)+const [selectedHookForDeletion, setSelectedHookForDeletion] = useState<Hook | null>(null)const { mutate: updateAuthHooks, isPending: isDeletingAuthHook } = useAuthHooksUpdateMutation({@@ -53,7 +55,7 @@ export const HooksListing = () => {}toast.success(`${selectedHookForDeletion.title} has been deleted.`)setSelectedHookForDeletion(null)- setSelectedHook(null)+ setHook(null)},onError: (error) => {toast.error(`Failed to delete hook: ${error.message}`)@@ -71,16 +73,14 @@ export const HooksListing = () => {}})- const { setValue: setSelectedHook, value: selectedHook } = useQueryStateWithSelect({- urlKey: 'hook',- select: (id: string) => {- if (!id) return null- const hook = hooks.find((h) => h.id === id)- return hook ? hook.title : undefined- },- enabled: !!hooks && hooks.length > 0,- onError: () => toast.error(`Hook not found`),- })+ const selectedHook = hooks.find((h) => h.id === hook)++ useEffect(() => {+ if (!!hook && !selectedHook) {+ toast('Hook not found')+ setHook(null)+ }+ }, [hook, selectedHook, setHook])if (isError) {return (@@ -115,7 +115,7 @@ export const HooksListing = () => {<AddHookDropdownonSelectHook={(title) => {const hook = hooks.find((h) => h.title === title)- if (hook) setSelectedHook(hook.id)+ if (hook) setHook(hook.id)}}/></PageSectionAside>@@ -132,7 +132,7 @@ export const HooksListing = () => {buttonText="Add a new hook"onSelectHook={(title) => {const hook = hooks.find((h) => h.title === title)- if (hook) setSelectedHook(hook.id)+ if (hook) setHook(hook.id)}}/></EmptyStatePresentational>@@ -143,23 +143,19 @@ export const HooksListing = () => {.filter((h) => isValidHook(h)).map((hook) => {return (- <HookCard- key={hook.enabledKey}- hook={hook}- onSelect={() => setSelectedHook(hook.id)}- />+ <HookCard key={hook.enabledKey} hook={hook} onSelect={() => setHook(hook.id)} />)})}</div><CreateHookSheet- title={selectedHook ?? null}+ title={selectedHook?.title ?? null}visible={!!selectedHook}onDelete={() => {- const hook = hooks.find((h) => h.title === selectedHook)+ const hook = hooks.find((h) => h.title === selectedHook?.title)if (hook) setSelectedHookForDeletion(hook)}}- onClose={() => setSelectedHook(null)}+ onClose={() => setHook(null)}authConfig={authConfig!}/>diff --git a/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx b/apps/studio/components/interfaces/Database/Indexes/Indexes.tsxindex 8188e9c224b..f5c36ab3896 100644--- a/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx+++ b/apps/studio/components/interfaces/Database/Indexes/Indexes.tsx@@ -1,9 +1,3 @@-import { sortBy } from 'lodash'-import { AlertCircle, Search, Trash } from 'lucide-react'-import { parseAsBoolean, parseAsString, useQueryState } from 'nuqs'-import { useEffect, useRef } from 'react'-import { toast } from 'sonner'-import { useParams } from 'common'import AlertError from 'components/ui/AlertError'import CodeEditor from 'components/ui/CodeEditor/CodeEditor'@@ -11,10 +5,14 @@ import SchemaSelector from 'components/ui/SchemaSelector'import { useDatabaseIndexDeleteMutation } from 'data/database-indexes/index-delete-mutation'import { useIndexesQuery, type DatabaseIndex } from 'data/database-indexes/indexes-query'import { useSchemasQuery } from 'data/database/schemas-query'-import { handleErrorOnDelete, useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'import { useQuerySchemaState } from 'hooks/misc/useSchemaQueryState'import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'import { useIsProtectedSchema } from 'hooks/useProtectedSchemas'+import { sortBy } from 'lodash'+import { AlertCircle, Search, Trash } from 'lucide-react'+import { parseAsBoolean, parseAsString, useQueryState } from 'nuqs'+import { useEffect } from 'react'+import { toast } from 'sonner'import {Button,Card,@@ -29,6 +27,7 @@ import {import { Input } from 'ui-patterns/DataInputs/Input'import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal'import { GenericSkeletonLoader, ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'+import { ProtectedSchemaWarning } from '../ProtectedSchemaWarning'import { CreateIndexSidePanel } from './CreateIndexSidePanel'@@ -38,7 +37,6 @@ const Indexes = () => {const [search, setSearch] = useQueryState('search', parseAsString.withDefault(''))const { selectedSchema, setSelectedSchema } = useQuerySchemaState()- const deletingIndexNameRef = useRef<string | null>(null)const {data: allIndexes,@@ -54,24 +52,14 @@ const Indexes = () => {const [showCreateIndex, setShowCreateIndex] = useQueryState('new',- parseAsBoolean.withDefault(false).withOptions({ history: 'push', clearOnDefault: true })+ parseAsBoolean.withDefault(false))- const { setValue: setSelectedIndexName, value: selectedIndex } = useQueryStateWithSelect({- urlKey: 'edit',- select: (id) => (id ? allIndexes?.find((idx) => idx.name === id) : undefined),- enabled: !!allIndexes,- onError: () => toast.error(`Index not found`),- })+ const [editIndexId, setEditIndexId] = useQueryState('edit', parseAsString)+ const selectedIndex = allIndexes?.find((idx) => idx.name === editIndexId)- const { setValue: setSelectedIndexNameToDelete, value: selectedIndexToDelete } =- useQueryStateWithSelect({- urlKey: 'delete',- select: (id) => (id ? allIndexes?.find((idx) => idx.name === id) : undefined),- enabled: !!allIndexes,- onError: (_error, selectedId) =>- handleErrorOnDelete(deletingIndexNameRef, selectedId, `Index not found`),- })+ const [deleteIndexId, setDeleteIndexId] = useQueryState('delete', parseAsString)+ const selectedIndexToDelete = allIndexes?.find((idx) => idx.name === deleteIndexId)const {data: schemas,@@ -83,9 +71,13 @@ const Indexes = () => {connectionString: project?.connectionString,})- const { mutate: deleteIndex, isPending: isExecuting } = useDatabaseIndexDeleteMutation({+ const {+ mutate: deleteIndex,+ isPending: isExecuting,+ isSuccess: isSuccessDelete,+ } = useDatabaseIndexDeleteMutation({onSuccess: async () => {- setSelectedIndexNameToDelete(null)+ setDeleteIndexId(null)toast.success('Successfully deleted index')},})@@ -100,8 +92,6 @@ const Indexes = () => {const onConfirmDeleteIndex = (index: DatabaseIndex) => {if (!project) return console.error('Project is required')-- deletingIndexNameRef.current = index.namedeleteIndex({projectRef: project.ref,connectionString: project.connectionString,@@ -121,6 +111,20 @@ const Indexes = () => {if (table !== undefined) setSearch(table)}, [table])+ useEffect(() => {+ if (isSuccessIndexes && !!editIndexId && !selectedIndex) {+ toast('Index not found')+ setEditIndexId(null)+ }+ }, [isSuccessIndexes, editIndexId, selectedIndex, setEditIndexId])++ useEffect(() => {+ if (isSuccessIndexes && !!deleteIndexId && !selectedIndexToDelete && !isSuccessDelete) {+ toast('Index not found')+ setDeleteIndexId(null)+ }+ }, [isSuccessIndexes, deleteIndexId, selectedIndexToDelete, isSuccessDelete, setDeleteIndexId])+return (<><div className="pb-8">@@ -218,10 +222,7 @@ const Indexes = () => {</TableCell><TableCell><div className="flex justify-end items-center space-x-2">- <Button- type="default"- onClick={() => setSelectedIndexName(index.name)}- >+ <Button type="default" onClick={() => setEditIndexId(index.name)}>View definition</Button>{!isSchemaLocked && (@@ -230,7 +231,7 @@ const Indexes = () => {type="text"className="px-1"icon={<Trash />}- onClick={() => setSelectedIndexNameToDelete(index.name)}+ onClick={() => setDeleteIndexId(index.name)}/>)}</div>@@ -254,7 +255,7 @@ const Indexes = () => {<code className="text-sm ml-2">{selectedIndex?.name}</code></>}- onCancel={() => setSelectedIndexName(null)}+ onCancel={() => setEditIndexId(null)}><div className="h-full"><div className="relative h-full">@@ -285,7 +286,7 @@ const Indexes = () => {onConfirm={() =>selectedIndexToDelete !== undefined ? onConfirmDeleteIndex(selectedIndexToDelete) : {}}- onCancel={() => setSelectedIndexNameToDelete(null)}+ onCancel={() => setDeleteIndexId(null)}alert={{title: 'This action cannot be undone',description: