Skip to content

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

fix: Fix a bug in Edit FDW sheet (#41685)

Ivan Vasilovf9cbf661ca6e
diff --git a/apps/studio/components/interfaces/Integrations/Wrappers/EditWrapperSheet.tsx b/apps/studio/components/interfaces/Integrations/Wrappers/EditWrapperSheet.tsx
index b159095c48a..c659e6d4e39 100644
--- a/apps/studio/components/interfaces/Integrations/Wrappers/EditWrapperSheet.tsx
+++ b/apps/studio/components/interfaces/Integrations/Wrappers/EditWrapperSheet.tsx
@@ -1,15 +1,15 @@
import { useQueryClient } from '@tanstack/react-query'
-import { isEmpty } from 'lodash'
+import { compact, isEmpty, mapValues } from 'lodash'
import { Edit, Trash } from 'lucide-react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { toast } from 'sonner'
+import { UUID_REGEX } from '@/lib/constants'
import { FormSection, FormSectionContent, FormSectionLabel } from 'components/ui/Forms/FormSection'
import { invalidateSchemasQuery } from 'data/database/schemas-query'
import { useFDWUpdateMutation } from 'data/fdw/fdw-update-mutation'
import { FDW } from 'data/fdw/fdws-query'
-import { getDecryptedValue } from 'data/vault/vault-secret-decrypted-value-query'
-import { useVaultSecretsQuery } from 'data/vault/vault-secrets-query'
+import { getDecryptedValues } from 'data/vault/vault-secret-decrypted-value-query'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { useConfirmOnClose, type ConfirmOnCloseModalProps } from 'hooks/ui/useConfirmOnClose'
import { Button, Form, Input, SheetFooter, SheetHeader, SheetTitle } from 'ui'
@@ -44,11 +44,6 @@ export const EditWrapperSheet = ({
const queryClient = useQueryClient()
const { data: project } = useSelectedProjectQuery()
- const { data: secrets, isPending: isSecretsLoading } = useVaultSecretsQuery({
- projectRef: project?.ref,
- connectionString: project?.connectionString,
- })
-
const { mutate: updateFDW, isPending: isSaving } = useFDWUpdateMutation({
onSuccess: () => {
toast.success(`Successfully updated ${wrapperMeta?.label} foreign data wrapper`)
@@ -75,7 +70,7 @@ export const EditWrapperSheet = ({
...convertKVStringArrayToJson(wrapper?.server_options ?? []),
}
- const onUpdateTable = (values: any) => {
+ const onUpdateTable = (values: FormattedWrapperTable) => {
setWrapperTables((prev) => {
// if the new values have tableIndex, we are editing an existing table
if (values.tableIndex !== undefined) {
@@ -91,9 +86,9 @@ export const EditWrapperSheet = ({
setSelectedTableToEdit(undefined)
}
- const onSubmit = async (values: any) => {
+ const onSubmit = async (values: Record<string, string>) => {
const validate = makeValidateRequired(wrapperMeta.server.options)
- const errors: any = validate(values)
+ const errors = validate(values)
const { wrapper_name } = values
if (wrapper_name.length === 0) errors.name = 'Please provide a name for your wrapper'
@@ -137,7 +132,15 @@ export const EditWrapperSheet = ({
onSubmit={onSubmit}
className="h-full flex flex-col"
>
- {({ values, initialValues, resetForm }: any) => {
+ {({
+ values,
+ initialValues,
+ resetForm,
+ }: {
+ values: Record<string, string>
+ initialValues: Record<string, string>
+ resetForm: (value: Record<string, Record<string, string>>) => void
+ }) => {
// [Alaister] although this "technically" is breaking the rules of React hooks
// it won't error because the hooks are always rendered in the same order
// eslint-disable-next-line react-hooks/rules-of-hooks
@@ -152,50 +155,60 @@ export const EditWrapperSheet = ({
const hasChanges = hasFormChanges || hasTableChanges
hasChangesRef.current = hasChanges
- const encryptedOptions = wrapperMeta.server.options.filter((option) => option.encrypted)
-
// [Alaister] although this "technically" is breaking the rules of React hooks
// it won't error because the hooks are always rendered in the same order
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
- const fetchEncryptedValues = async () => {
- setLoadingSecrets(true)
- // If the secrets haven't loaded, escape and run the effect again when they're loaded
- if (isSecretsLoading) {
- return
- }
+ const fetchEncryptedValues = async (ids: string[]) => {
+ try {
+ setLoadingSecrets(true)
+ // If the secrets haven't loaded, escape and run the effect again when they're loaded
+ const decryptedValues = await getDecryptedValues({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ ids: ids,
+ })
- const res = await Promise.all(
- encryptedOptions.map(async (option) => {
- const secret = secrets?.find(
- (secret) => secret.name === `${wrapper.name}_${option.name}`
- )
- if (secret !== undefined) {
- const value = await getDecryptedValue({
- projectRef: project?.ref,
- connectionString: project?.connectionString,
- id: secret.id,
- })
- return { [option.name]: value[0]?.decrypted_secret ?? '' }
- } else {
- return { [option.name]: '' }
- }
+ // replace all values which are in the decryptedValues object with the decrypted value
+ const transformValues = (values: Record<string, string>) => {
+ return mapValues(values, (value) => {
+ return decryptedValues[value] ?? value
+ })
+ }
+
+ resetForm({
+ values: transformValues(values),
+ initialValues: transformValues(initialValues),
})
- )
- const secretValues = res.reduce((a: any, b: any) => {
- const [key] = Object.keys(b)
- return { ...a, [key]: b[key] }
- }, {})
+ } catch (error) {
+ toast.error('Failed to fetch encrypted values')
+ } finally {
+ setLoadingSecrets(false)
+ }
+ }
- resetForm({
- values: { ...values, ...secretValues },
- initialValues: { ...initialValues, ...secretValues },
+ const encryptedOptions = wrapperMeta.server.options.filter(
+ (option) => option.encrypted
+ )
+
+ const encryptedIdsToFetch = compact(
+ encryptedOptions.map((option) => {
+ const value = initialValues[option.name]
+ return value ?? null
})
- setLoadingSecrets(false)
- }
+ ).filter((x) => UUID_REGEX.test(x))
+ // [Joshen] ^ Validate UUID to filter out already decrypted values
- if (encryptedOptions.length > 0) fetchEncryptedValues()
- }, [isSecretsLoading])
+ if (encryptedIdsToFetch.length > 0) {
+ fetchEncryptedValues(encryptedIdsToFetch)
+ }
+ /**
+ * [Joshen] We're deliberately not adding values and initialValues to the dependency array here
+ * as we only want to fetch the encrypted values once on load + values and initialValues will be updated
+ * as a result of that
+ */
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [project?.ref, project?.connectionString])
return (
<>
@@ -279,8 +292,7 @@ export const EditWrapperSheet = ({
Target: {target}
</p>
<p className="text-sm text-foreground-light">
- Columns:{' '}
- {table.columns.map((column: any) => column.name).join(', ')}
+ Columns: {table.columns.map((column) => column.name).join(', ')}
</p>
</div>
<div className="flex items-center space-x-2">
diff --git a/apps/studio/components/interfaces/Integrations/Wrappers/WrapperRow.tsx b/apps/studio/components/interfaces/Integrations/Wrappers/WrapperRow.tsx
index ef5f4772d27..43d6b8d200c 100644
--- a/apps/studio/components/interfaces/Integrations/Wrappers/WrapperRow.tsx
+++ b/apps/studio/components/interfaces/Integrations/Wrappers/WrapperRow.tsx
@@ -25,7 +25,6 @@ import { formatWrapperTables } from './Wrappers.utils'
interface WrapperRowProps {
wrapper: FDW
- wrappers: FDW[]
selectedWrapperToEdit?: FDW
selectedWrapperToDelete?: FDW
setSelectedWrapperToEdit: (value: string | null) => void
@@ -33,9 +32,8 @@ interface WrapperRowProps {
deletingWrapperIdRef: MutableRefObject<string | null>
}
-const WrapperRow = ({
+export const WrapperRow = ({
wrapper,
- wrappers,
selectedWrapperToEdit,
selectedWrapperToDelete,
setSelectedWrapperToEdit,
@@ -88,17 +86,20 @@ const WrapperRow = ({
<TableCell className="space-y-2 !p-4">
{_tables?.map((table) => {
- const target = table.table ?? table.object
+ const target = table.table ?? table.object ?? table.src_key
return (
- <div key={table.id} className="flex items-center -space-x-3">
- <Badge className="bg-surface-300 bg-opacity-100 pr-1 gap-2 font-mono text-[0.75rem] h-6 text-foreground">
+ <div key={table.id} className="flex items-center">
+ <Badge className="bg-surface-300 bg-opacity-100 gap-2 font-mono text-[0.75rem] h-6 text-foreground rounded-r-none">
<div className="relative w-3 h-3 flex items-center justify-center">
{integration.icon({ className: 'p-0' })}
</div>
<Tooltip>
<TooltipTrigger className="truncate max-w-28">{target}</TooltipTrigger>
- <TooltipContent className="max-w-64 whitespace-pre-wrap break-words">
+ <TooltipContent
+ side="bottom"
+ className="max-w-64 whitespace-pre-wrap break-words"
+ >
{target}
</TooltipContent>
</Tooltip>
@@ -110,13 +111,16 @@ const WrapperRow = ({
</Badge>
<Link href={`/project/${ref}/editor/${table.id}`}>
- <Badge className="transition hover:bg-surface-300 pl-5 rounded-l-none gap-2 h-6 font-mono text-[0.75rem] border-l-0">
+ <Badge className="transition hover:bg-surface-300 px-2 rounded-l-none gap-1.5 h-6 font-mono text-[0.75rem] border-l-0">
<Table2 size={12} strokeWidth={1.5} className="text-foreground-lighter/50" />
<Tooltip>
<TooltipTrigger className="truncate max-w-28">
{table.schema}.{table.table_name}
</TooltipTrigger>
- <TooltipContent className="max-w-64 whitespace-pre-wrap break-words">
+ <TooltipContent
+ side="bottom"
+ className="max-w-64 whitespace-pre-wrap break-words"
+ >
{table.schema}.{table.table_name}
</TooltipContent>
</Tooltip>
@@ -129,7 +133,6 @@ const WrapperRow = ({
<TableCell>
{encryptedMetadata.map((metadata) => (
<div key={metadata.name} className="flex items-center space-x-2 text-sm">
- {/* <p className="text-foreground-light">{metadata.label}:</p> */}
<Link
href={`/project/${ref}/settings/vault/secrets?search=${wrapper.name}_${metadata.name}`}
className="transition text-foreground-light hover:text-foreground flex items-center space-x-2 max-w-28"
@@ -212,5 +215,3 @@ const WrapperRow = ({
</>
)
}
-
-export default WrapperRow
diff --git a/apps/studio/components/interfaces/Integrations/Wrappers/WrapperTable.tsx b/apps/studio/components/interfaces/Integrations/Wrappers/WrapperTable.tsx
index 46af1b1529a..431fdf15aa6 100644
--- a/apps/studio/components/interfaces/Integrations/Wrappers/WrapperTable.tsx
+++ b/apps/studio/components/interfaces/Integrations/Wrappers/WrapperTable.tsx
@@ -17,7 +17,7 @@ import {
TableRow,
} from 'ui'
import { INTEGRATIONS } from '../Landing/Integrations.constants'
-import WrapperRow from './WrapperRow'
+import { WrapperRow } from './WrapperRow'
import { wrapperMetaComparator } from './Wrappers.utils'
interface WrapperTableProps {
@@ -91,7 +91,6 @@ export const WrapperTable = ({ isLatest = false }: WrapperTableProps) => {
<WrapperRow
key={x.id}
wrapper={x}
- wrappers={wrappers}
selectedWrapperToEdit={selectedWrapperToEdit}
selectedWrapperToDelete={selectedWrapperToDelete}
setSelectedWrapperToEdit={setSelectedWrapperToEdit}
diff --git a/apps/studio/data/auth/user-query.ts b/apps/studio/data/auth/user-query.ts
index 0a93cd1f7f6..40620107f4b 100644
--- a/apps/studio/data/auth/user-query.ts
+++ b/apps/studio/data/auth/user-query.ts
@@ -1,13 +1,12 @@
import { useQuery } from '@tanstack/react-query'
+import { UUID_REGEX } from '@/lib/constants'
import { executeSql, type ExecuteSqlError } from 'data/sql/execute-sql-query'
import { getUserSQL } from 'data/sql/queries/get-user'
import { UseCustomQueryOptions } from 'types'
import { authKeys } from './keys'
import { User } from './users-infinite-query'
-const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
-
type UserVariables = {
projectRef?: string
connectionString?: string | null
diff --git a/apps/studio/data/vault/vault-secret-decrypted-value-query.ts b/apps/studio/data/vault/vault-secret-decrypted-value-query.ts
index f63336b3414..1b4ab8d3633 100644
--- a/apps/studio/data/vault/vault-secret-decrypted-value-query.ts
+++ b/apps/studio/data/vault/vault-secret-decrypted-value-query.ts
@@ -84,8 +84,14 @@ export const getDecryptedValues = async (
signal?: AbortSignal
) => {
const sql = vaultSecretDecryptedValuesQuery(ids)
- const { result } = await executeSql({ projectRef, connectionString, sql }, signal)
- return result.reduce((a: any, b: any) => {
- return { ...a, [b.id]: b.decrypted_secret }
- }, {})
+ const { result } = await executeSql<{ id: string; decrypted_secret: string }[]>(
+ { projectRef, connectionString, sql },
+ signal
+ )
+ return result.reduce(
+ (a, b) => {
+ return { ...a, [b.id]: b.decrypted_secret }
+ },
+ {} as Record<string, string>
+ )
}
diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.ts
index 4a95864039e..3bf961dec49 100644
--- a/apps/studio/lib/constants/index.ts
+++ b/apps/studio/lib/constants/index.ts
@@ -56,3 +56,5 @@ export const OPT_IN_TAGS = {
export const GB = 1024 * 1024 * 1024
export const MB = 1024 * 1024
export const KB = 1024
+
+export const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i