Files
supabase/apps/studio/data/fdw/fdw-update-mutation.ts
Joshen Lim 838fcaaa89 Joshenlim/fe 4509 fdw general UI consolidation and refactor (#51079)
## Context

Stacks on top of https://github.com/supabase/supabase/pull/51074

PR's just mainly refactoring, no visual differences:
- `CreateWrapperSheet` + `EditWrapperSheet` use the same UI components
for the foreign tables section
  - Can be consolidated into one reusable component
- `WrapperTableEditor` is still using `SidePanel` component
  - Can be swapped to use new `Sheet` component
- Refactor `WrapperTableEditor`'s layout a little - added separators for
clarity between sections
<img width="400" alt="image"
src="https://github.com/user-attachments/assets/b1983bf2-cff5-43eb-8b31-40a7abb65038"
/>
- Update `getCreateFDWSql` to just use the Foreign Data Wrapper's name
from `wrapperMeta` since its now standardized

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added a shared foreign-table selector for wrapper setup and editing,
with options to view columns, add or edit table definitions, and remove
tables.
  * Updated the table editor to use a sheet layout with a fixed footer.
* **Bug Fixes**
* Wrapper creation now uses the wrapper’s configured name when creating
the server.
* Foreign-table targets display the table name when other target details
are unavailable.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-10-05 16:20:50 +08:00

85 lines
2.6 KiB
TypeScript

import { getUpdateFDWSql } from '@supabase/pg-meta'
import { wrapWithTransaction } from '@supabase/pg-meta/src/query'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { FDW } from './fdws-query'
import { fdwKeys } from './keys'
import type { WrapperMeta } from '@/components/interfaces/Integrations/Wrappers/Wrappers.types'
import { entityTypeKeys } from '@/data/entity-types/keys'
import { foreignTableKeys } from '@/data/foreign-tables/keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { vaultSecretsKeys } from '@/data/vault/keys'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type FDWUpdateVariables = {
projectRef?: string
connectionString?: string | null
wrapper: FDW
wrapperMeta: WrapperMeta
formState: {
[k: string]: string
}
tables: any[]
skipInvalidation?: boolean
}
export async function updateFDW({
projectRef,
connectionString,
wrapper,
wrapperMeta,
formState,
tables,
}: FDWUpdateVariables) {
const sql = wrapWithTransaction(getUpdateFDWSql({ wrapper, wrapperMeta, formState, tables }))
const { result } = await executeSql({
projectRef,
connectionString,
sql,
queryKey: ['update-fdw'],
})
return result
}
type FDWUpdateData = Awaited<ReturnType<typeof updateFDW>>
export const useFDWUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<FDWUpdateData, ResponseError, FDWUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<FDWUpdateData, ResponseError, FDWUpdateVariables>({
mutationFn: (vars) => updateFDW(vars),
async onSuccess(data, variables, context) {
const { projectRef, skipInvalidation = false } = variables
if (!skipInvalidation) {
await Promise.all([
queryClient.invalidateQueries({ queryKey: fdwKeys.list(projectRef), refetchType: 'all' }),
queryClient.invalidateQueries({ queryKey: entityTypeKeys.list(projectRef) }),
queryClient.invalidateQueries({ queryKey: foreignTableKeys.list(projectRef) }),
queryClient.invalidateQueries({ queryKey: vaultSecretsKeys.list(projectRef) }),
])
}
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(
`Failed to update ${variables.wrapperMeta.label} foreign data wrapper: ${data.message}`
)
} else {
onError(data, variables, context)
}
},
...options,
})
}