Skip to content

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

Add react queries for edge functions secrets

Joshen Lim469ce7d1ad69
diff --git a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
index 640d1a18aa6..b5321e00cf3 100644
--- a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
+++ b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
@@ -99,7 +99,7 @@ const StorageConfig = ({ config, projectRef }: any) => {
// [Joshen] To be refactored using FormContainer, FormPanel, FormContent etc once
// Jonny's auth config refactor PR goes in
return (
- <div className="mx-auto w-[56rem] max-w-4xl px-5 pt-12 pb-20">
+ <div>
<Form validateOnBlur initialValues={initialValues} validate={onValidate} onSubmit={onSubmit}>
{({ values, handleReset }: { values: any; handleReset: () => void }) => {
const hasChanges =
diff --git a/studio/data/secrets/keys.ts b/studio/data/secrets/keys.ts
new file mode 100644
index 00000000000..c73592b38c3
--- /dev/null
+++ b/studio/data/secrets/keys.ts
@@ -0,0 +1,3 @@
+export const secretsKeys = {
+ list: (projectRef: string | undefined) => ['projects', projectRef, 'secrets'] as const,
+}
diff --git a/studio/data/secrets/secrets-create-mutation.ts b/studio/data/secrets/secrets-create-mutation.ts
new file mode 100644
index 00000000000..8500df98377
--- /dev/null
+++ b/studio/data/secrets/secrets-create-mutation.ts
@@ -0,0 +1,52 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { secretsKeys } from './keys'
+
+export type SecretsCreateVariables = {
+ projectRef: string
+ secrets: { name: string; value: string }[]
+}
+
+export async function createSecrets({ projectRef, secrets }: SecretsCreateVariables) {
+ const { data, error } = await post('/v1/projects/{ref}/secrets', {
+ params: { path: { ref: projectRef } },
+ body: secrets,
+ })
+
+ if (error) throw error
+ return data
+}
+
+type SecretsCreateData = Awaited<ReturnType<typeof createSecrets>>
+
+export const useSecretsCreateMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<SecretsCreateData, ResponseError, SecretsCreateVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<SecretsCreateData, ResponseError, SecretsCreateVariables>(
+ (vars) => createSecrets(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(secretsKeys.list(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to create secrets: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/secrets/secrets-delete-mutation.ts b/studio/data/secrets/secrets-delete-mutation.ts
new file mode 100644
index 00000000000..c3a57580da1
--- /dev/null
+++ b/studio/data/secrets/secrets-delete-mutation.ts
@@ -0,0 +1,52 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { del } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { secretsKeys } from './keys'
+
+export type SecretsDeleteVariables = {
+ projectRef: string
+ ids: string[]
+}
+
+export async function deleteSecrets({ projectRef, ids }: SecretsDeleteVariables) {
+ const { data, error } = await del('/v1/projects/{ref}/secrets', {
+ params: { path: { ref: projectRef } },
+ body: ids,
+ })
+
+ if (error) throw error
+ return data
+}
+
+type SecretsDeleteData = Awaited<ReturnType<typeof deleteSecrets>>
+
+export const useSecretsDeleteMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<SecretsDeleteData, ResponseError, SecretsDeleteVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<SecretsDeleteData, ResponseError, SecretsDeleteVariables>(
+ (vars) => deleteSecrets(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(secretsKeys.list(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to delete secrets: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/secrets/secrets-query.ts b/studio/data/secrets/secrets-query.ts
new file mode 100644
index 00000000000..d8aaa5d31b5
--- /dev/null
+++ b/studio/data/secrets/secrets-query.ts
@@ -0,0 +1,31 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'data/fetchers'
+import { secretsKeys } from './keys'
+import { ResponseError } from 'types'
+
+export type SecretsVariables = {
+ projectRef: string
+}
+
+export async function getSecrets({ projectRef }: SecretsVariables, signal?: AbortSignal) {
+ const { data, error } = await get(`/v1/projects/{ref}/secrets`, {
+ params: { path: { ref: projectRef } },
+ signal,
+ })
+
+ if (error) throw error
+ return data
+}
+
+export type SecretsData = Awaited<ReturnType<typeof getSecrets>>
+export type SecretsError = ResponseError
+
+export const useSecretsQuery = <TData = SecretsData>(
+ { projectRef }: SecretsVariables,
+ { enabled = true, ...options }: UseQueryOptions<SecretsData, SecretsError, TData> = {}
+) =>
+ useQuery<SecretsData, SecretsError, TData>(
+ secretsKeys.list(projectRef),
+ ({ signal }) => getSecrets({ projectRef }, signal),
+ { enabled: enabled && typeof projectRef !== 'undefined', ...options }
+ )
diff --git a/studio/pages/project/[ref]/settings/storage.tsx b/studio/pages/project/[ref]/settings/storage.tsx
index 728a44bd4b7..0622e4f543c 100644
--- a/studio/pages/project/[ref]/settings/storage.tsx
+++ b/studio/pages/project/[ref]/settings/storage.tsx
@@ -1,16 +1,14 @@
import { useParams } from 'common'
import { SettingsLayout } from 'components/layouts'
-import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { StorageSettings } from 'components/to-be-cleaned/Storage'
import { NextPageWithLayout } from 'types'
const PageLayout: NextPageWithLayout = () => {
const { ref: projectRef } = useParams()
- const { project } = useProjectContext()
return (
- <div className="flex flex-grow p-4 storage-container">
+ <div className="1xl:px-28 mx-auto flex flex-col gap-8 px-5 py-6 lg:px-16 xl:px-24 2xl:px-32">
<StorageSettings projectRef={projectRef} />
</div>
)