Skip to content

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

Update FDW UI

Joshen Lim0b7064530358
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx b/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx
index 214df2baa34..f1756648276 100644
--- a/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx
+++ b/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx
@@ -11,6 +11,8 @@ import { useFDWDeleteMutation } from 'data/fdw/fdw-delete-mutation'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import Image from 'next/image'
+// [Joshen TODO] No longer used, can remove after WrapperRow is working
+
export type WrapperCardProps = {
wrapper: Wrapper
enabled?: boolean
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx b/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx
index 096624e4674..65cde64fa25 100644
--- a/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx
+++ b/studio/components/interfaces/Database/Wrappers/WrapperEditor.tsx
@@ -10,6 +10,8 @@ import { Wrapper } from './types'
import { makeValidateRequired } from './utils'
import WrapperTableEditor from './WrapperTableEditor'
+// [Joshen TODO] No longer used, can remove after WrapperRow is working
+
export type WrapperEditorProps = {
visible: boolean
wrapper: Wrapper
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx b/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx
index 32b7d4ccd8b..6e5bb32d556 100644
--- a/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx
+++ b/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx
@@ -1,85 +1,275 @@
import Image from 'next/image'
-import { FC } from 'react'
-import { Collapsible, Form, IconCheck, IconChevronUp } from 'ui'
+import { isEmpty } from 'lodash'
+import { FC, useState } from 'react'
+import {
+ Collapsible,
+ Input,
+ IconCheck,
+ IconChevronUp,
+ Button,
+ IconEdit,
+ IconTrash,
+ IconEye,
+ IconEyeOff,
+} from 'ui'
+
+import { useStore } from 'hooks'
import { Wrapper } from './types'
+import { makeValidateRequired } from './utils'
+import { useFDWCreateMutation } from 'data/fdw/fdw-create-mutation'
+import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
+import WrapperTableEditor from './WrapperTableEditor'
interface Props {
wrapper: Wrapper
+ isEnabled: boolean
isOpen: boolean
onOpen: (wrapper: string) => void
}
-const WrapperRow: FC<Props> = ({ wrapper, isOpen, onOpen }) => {
+const WrapperRow: FC<Props> = ({ wrapper, isEnabled, isOpen, onOpen }) => {
+ const getInitialFormState = () =>
+ Object.fromEntries(
+ wrapper.server.options.map((option) => [option.name, option.defaultValue ?? ''])
+ )
+
+ const { ui } = useStore()
+ const { project } = useProjectContext()
+ const { mutateAsync: createFDW } = useFDWCreateMutation()
+
+ const [isSubmitting, setIsSubmitting] = useState(false)
+ const [isEditingTable, setIsEditingTable] = useState(false)
+ const [selectedTableToEdit, setSelectedTableToEdit] = useState()
+
+ const [formState, setFormState] = useState(getInitialFormState)
+ const [newTables, setNewTables] = useState<any[]>([])
+ const [formErrors, setFormErrors] = useState<{ [k: string]: string }>({})
+
+ // [Joshen TODO] Fix this logic
+ const noChanges = false
+ // const noChanges = JSON.stringify(getInitialFormState()) === JSON.stringify(formState)
+
+ const onUpdateTable = (values: any) => {
+ setNewTables((prev) => {
+ // if the new values have tableIndex, we are editing an existing table
+ if (values.tableIndex !== undefined) {
+ const tableIndex = values.tableIndex
+ const newTables = [...prev]
+ delete values.tableIndex
+ newTables[tableIndex] = values
+ return newTables
+ }
+ return [...prev, values]
+ })
+ setIsEditingTable(false)
+ setSelectedTableToEdit(undefined)
+ }
+
+ const onSubmit = async () => {
+ const validate = makeValidateRequired(wrapper.server.options)
+ const errors = validate(formState)
+ if (!isEmpty(errors)) {
+ setFormErrors(errors)
+ return
+ }
+
+ setIsSubmitting(true)
+ try {
+ await createFDW({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ wrapper,
+ formState,
+ newTables,
+ })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully created ${wrapper.label} foreign data wrapper`,
+ })
+ onOpen('')
+ } catch (error: any) {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Failed to create ${wrapper.label} foreign data wrapper: ${error.message}`,
+ })
+ } finally {
+ setIsSubmitting(false)
+ }
+ }
+
return (
- <Collapsible
- open={isOpen}
- onOpenChange={() => onOpen(wrapper.name)}
- className={[
- 'bg-scale-100 dark:bg-scale-300 ',
- 'hover:bg-scale-200 dark:hover:bg-scale-500',
- 'data-open:bg-scale-200 dark:data-open:bg-scale-500',
- 'border-scale-300',
- 'dark:border-scale-500 hover:border-scale-500',
- 'dark:hover:border-scale-700 data-open:border-scale-700',
- 'data-open:pb-px col-span-12 mx-auto',
- '-space-y-px overflow-hidden',
- 'transition border shadow hover:z-50',
- 'first:rounded-tl first:rounded-tr',
- 'last:rounded-bl last:rounded-br',
- ].join(' ')}
- >
- <Collapsible.Trigger asChild>
- <button
- type="button"
- className="group flex w-full items-center justify-between rounded py-3 px-6 text-scale-1200"
- >
- <div className="flex items-center gap-3">
- <IconChevronUp
- className="text-scale-800 transition data-open-parent:rotate-0 data-closed-parent:rotate-180"
- strokeWidth={2}
- width={14}
- />
- <Image src={wrapper.icon} width={20} height={20} alt={`${wrapper.name} wrapper icon`} />
- <span className="text-sm capitalize">{wrapper.label}</span>
- </div>
- <div className="flex items-center gap-3">
- {false ? (
- <div className="flex items-center gap-1 rounded-full border border-brand-700 bg-brand-200 py-1 px-1 text-xs text-brand-900">
- <span className="rounded-full bg-brand-900 p-0.5 text-xs text-brand-200">
- <IconCheck strokeWidth={2} size={12} />
- </span>
- <span className="px-1">Enabled</span>
- </div>
- ) : (
- <div className="rounded-md border border-scale-500 bg-scale-100 py-1 px-3 text-xs text-scale-900 dark:border-scale-700 dark:bg-scale-300">
- Disabled
- </div>
- )}
- </div>
- </button>
- </Collapsible.Trigger>
- <Form
- name={`provider-${wrapper.name}-form`}
- initialValues={{}}
- // validationSchema={provider.validationSchema}
- onSubmit={() => {}}
+ <>
+ <Collapsible
+ open={isOpen}
+ onOpenChange={() => onOpen(wrapper.name)}
+ className={[
+ 'bg-scale-100 dark:bg-scale-300 ',
+ 'hover:bg-scale-200 dark:hover:bg-scale-500',
+ 'data-open:bg-scale-200 dark:data-open:bg-scale-500',
+ 'border-scale-300',
+ 'dark:border-scale-500 hover:border-scale-500',
+ 'dark:hover:border-scale-700 data-open:border-scale-700',
+ 'data-open:pb-px col-span-12 mx-auto',
+ '-space-y-px overflow-hidden',
+ 'transition border shadow hover:z-50',
+ 'first:rounded-tl first:rounded-tr',
+ 'last:rounded-bl last:rounded-br',
+ ].join(' ')}
>
- {({ isSubmitting, handleReset, initialValues, values }: any) => {
- return (
- <Collapsible.Content>
- <div
- className="
- group border-t
- border-scale-500 bg-scale-100 py-6 px-6 text-scale-1200 dark:bg-scale-300
- "
- >
- Hello
+ <Collapsible.Trigger asChild>
+ <button
+ type="button"
+ className="group flex w-full items-center justify-between rounded py-3 px-6 text-scale-1200"
+ >
+ <div className="flex items-center gap-3">
+ <IconChevronUp
+ className="text-scale-800 transition data-open-parent:rotate-0 data-closed-parent:rotate-180"
+ strokeWidth={2}
+ width={14}
+ />
+ <Image
+ src={wrapper.icon}
+ width={20}
+ height={20}
+ alt={`${wrapper.name} wrapper icon`}
+ />
+ <span className="text-sm capitalize">{wrapper.label}</span>
+ </div>
+ <div className="flex items-center gap-3">
+ {isEnabled ? (
+ <div className="flex items-center gap-1 rounded-full border border-brand-700 bg-brand-200 py-1 px-1 text-xs text-brand-900">
+ <span className="rounded-full bg-brand-900 p-0.5 text-xs text-brand-200">
+ <IconCheck strokeWidth={2} size={12} />
+ </span>
+ <span className="px-1">Enabled</span>
+ </div>
+ ) : (
+ <div className="rounded-md border border-scale-500 bg-scale-100 py-1 px-3 text-xs text-scale-900 dark:border-scale-700 dark:bg-scale-300">
+ Disabled
+ </div>
+ )}
+ </div>
+ </button>
+ </Collapsible.Trigger>
+ <Collapsible.Content>
+ <div className="group border-t border-scale-500 bg-scale-100 py-6 px-6 text-scale-1200 dark:bg-scale-300">
+ <div className="max-w-lg mx-auto space-y-6 my-6">
+ {wrapper.server.options.map((option) => {
+ const [showHidden, setShowHidden] = useState(option.hidden)
+ return (
+ <Input
+ key={option.name}
+ id={option.name}
+ name={option.name}
+ label={option.label}
+ defaultValue={option.defaultValue ?? ''}
+ required={option.required ?? false}
+ value={formState[option.name]}
+ onChange={(e) => {
+ setFormState((prev) => ({ ...prev, [option.name]: e.target.value }))
+ setFormErrors((prev) => ({ ...prev, [option.name]: '' }))
+ }}
+ error={formErrors[option.name]}
+ className="input-mono"
+ type={!option.hidden ? 'text' : showHidden ? 'text' : 'password'}
+ actions={
+ option.hidden ? (
+ <div className="mr-1 flex items-center justify-center">
+ <Button
+ type="default"
+ icon={showHidden ? <IconEye /> : <IconEyeOff />}
+ onClick={() => setShowHidden(!showHidden)}
+ />
+ </div>
+ ) : null
+ }
+ />
+ )
+ })}
+ <div className="space-y-4">
+ <div className="space-y-1">
+ <div className="flex items-center justify-between">
+ <p>Foreign Tables</p>
+ <Button type="default" onClick={() => setIsEditingTable(true)}>
+ Add table
+ </Button>
+ </div>
+ </div>
+ <div className="space-y-2">
+ {newTables.length === 0 && (
+ <div className="border border-scale-600 px-4 py-4 rounded-md flex items-center justify-center">
+ <p className="text-sm text-scale-1000">
+ Add foreign tables to query from after the wrapper is enabled
+ </p>
+ </div>
+ )}
+ {newTables.map((table, i) => (
+ <div className="border border-scale-600 px-4 py-2 rounded-md space-y-1 flex items-center justify-between">
+ <div>
+ <p>{table.table_name}</p>
+ <p className="text-sm text-scale-1100">
+ {wrapper.tables[table.index].label}: {table.columns.join(', ')}
+ </p>
+ </div>
+ <div className="flex items-center space-x-2">
+ <Button
+ type="default"
+ className="px-1"
+ icon={<IconEdit />}
+ onClick={() => {
+ setIsEditingTable(true)
+ setSelectedTableToEdit({ ...table, tableIndex: i })
+ }}
+ />
+ <Button
+ type="default"
+ className="px-1"
+ icon={<IconTrash />}
+ onClick={() => {
+ setNewTables((prev) => prev.filter((_, j) => j !== i))
+ }}
+ />
+ </div>
+ </div>
+ ))}
+ </div>
+ <div className="flex items-center justify-end !mt-8">
+ {/* [Joshen] Thinking if we need to add a disclaimer here that users cannot edit wrappers */}
+ <div className="flex items-center space-x-3">
+ <Button
+ type="default"
+ htmlType="reset"
+ onClick={() => {
+ onOpen('')
+ setNewTables([])
+ setFormState(getInitialFormState)
+ }}
+ disabled={isSubmitting}
+ >
+ Cancel
+ </Button>
+ <Button loading={isSubmitting} disabled={noChanges} onClick={() => onSubmit()}>
+ Save
+ </Button>
+ </div>
+ </div>
</div>
- </Collapsible.Content>
- )
+ </div>
+ </div>
+ </Collapsible.Content>
+ </Collapsible>
+ <WrapperTableEditor
+ visible={isEditingTable}
+ tables={wrapper.tables}
+ onCancel={() => {
+ setSelectedTableToEdit(undefined)
+ setIsEditingTable(false)
}}
- </Form>
- </Collapsible>
+ onSave={onUpdateTable}
+ initialData={selectedTableToEdit}
+ />
+ </>
)
}
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx b/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx
index e0c0b501738..0f8a4db53ee 100644
--- a/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx
+++ b/studio/components/interfaces/Database/Wrappers/WrapperTableEditor.tsx
@@ -99,7 +99,6 @@ const Option = ({ option }: { option: TableOption }) => {
label={option.label}
placeholder={option.placeholder ?? ''}
defaultValue={option.defaultValue ?? ''}
- className="input-mono"
/>
)
}
@@ -148,7 +147,6 @@ const TableForm = ({
name="table_name"
label="Table name"
descriptionText="The name of the local table table you will query after the wrapper is enabled."
- className="input-mono"
/>
{requiredOptions.map((option) => (
diff --git a/studio/components/interfaces/Database/Wrappers/Wrappers.tsx b/studio/components/interfaces/Database/Wrappers/Wrappers.tsx
index 8c0bf08155d..bbdcf5f8938 100644
--- a/studio/components/interfaces/Database/Wrappers/Wrappers.tsx
+++ b/studio/components/interfaces/Database/Wrappers/Wrappers.tsx
@@ -1,12 +1,12 @@
+import { partition } from 'lodash'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { FormHeader } from 'components/ui/Forms'
import { useFDWsQuery } from 'data/fdw/fdws-query'
-import { Collapsible, Form, IconCheck, IconLoader, Toggle } from 'ui'
import { wrappers } from './data'
import WrapperCard from './WrapperCard'
-import Image from 'next/image'
import WrapperRow from './WrapperRow'
import { useState } from 'react'
+import { Wrapper } from './types'
const Wrappers = () => {
const { project } = useProjectContext()
@@ -17,25 +17,24 @@ const Wrappers = () => {
const [open, setOpen] = useState<string>('')
const enabledWrapperNamesSet = new Set(data?.result.map((fdw) => fdw.name))
-
- const enabledWrappers = wrappers.filter((wrapper) =>
+ // const enabledWrapperNamesSet = new Set(['stripe_server'])
+ const [enabledWrappers, disabledWrappers] = partition(wrappers, (wrapper: Wrapper) =>
enabledWrapperNamesSet.has(wrapper.server.name)
)
- const disabledWrappers = wrappers.filter(
- (wrapper) => !enabledWrapperNamesSet.has(wrapper.server.name)
- )
return (
- <>
+ <div>
<FormHeader
title="Foreign Data Wrappers"
- description="Connect your database to external systems. Query your data warehouse directly from your database, or query third-party APIs using SQL."
+ description="Connect your database to external systems. Query your data warehouse directly from your database, or third-party APIs using SQL."
/>
+
<div>
{wrappers.map((wrapper) => {
return (
<WrapperRow
wrapper={wrapper}
+ isEnabled={enabledWrapperNamesSet.has(wrapper.server.name)}
isOpen={open === wrapper.name}
onOpen={(wrapperName) => {
if (open !== wrapperName) setOpen(wrapperName)
@@ -46,7 +45,8 @@ const Wrappers = () => {
})}
</div>
- <div className="space-y-4">
+ {/* [Joshen TODO] Once above is working, can remove below */}
+ <div className="space-y-4 mt-20">
<div className="w-full space-y-12">
{enabledWrappers.length > 0 && (
<div className="space-y-4">
@@ -71,7 +71,7 @@ const Wrappers = () => {
)}
</div>
</div>
- </>
+ </div>
)
}
diff --git a/studio/components/interfaces/Database/Wrappers/data.ts b/studio/components/interfaces/Database/Wrappers/data.ts
index 816cac4626d..05f7a29374f 100644
--- a/studio/components/interfaces/Database/Wrappers/data.ts
+++ b/studio/components/interfaces/Database/Wrappers/data.ts
@@ -14,6 +14,7 @@ export const wrappers: Wrapper[] = [
label: 'Stripe Secret Key',
required: true,
encrypted: true,
+ hidden: true,
},
{
name: 'api_url',
@@ -21,12 +22,14 @@ export const wrappers: Wrapper[] = [
defaultValue: 'https://api.stripe.com/v1',
required: false,
encrypted: false,
+ hidden: false,
},
],
},
tables: [
{
label: 'Balance',
+ description: 'Shows the balance currently on your Stripe account',
availableColumns: [
{
name: 'amount',
@@ -61,12 +64,14 @@ export const wrappers: Wrapper[] = [
label: 'Project ID',
required: true,
encrypted: false,
+ hidden: false,
},
{
name: 'sa_key_id',
label: 'Service Account Key',
required: true,
encrypted: true,
+ hidden: true,
},
],
},
diff --git a/studio/components/interfaces/Database/Wrappers/types.ts b/studio/components/interfaces/Database/Wrappers/types.ts
index 42544c56382..41d9bd1f690 100644
--- a/studio/components/interfaces/Database/Wrappers/types.ts
+++ b/studio/components/interfaces/Database/Wrappers/types.ts
@@ -12,6 +12,7 @@ export type ServerOption = {
label: string
required: boolean
encrypted: boolean
+ hidden: boolean
defaultValue?: string
}
@@ -31,6 +32,7 @@ export type TableOption = {
export type Table = {
label: string
+ description?: string
availableColumns: AvailableColumn[]
options: TableOption[]
}
diff --git a/studio/components/ui/MultiSelect/index.tsx b/studio/components/ui/MultiSelect/index.tsx
index e43ed33aee1..18a49705e77 100644
--- a/studio/components/ui/MultiSelect/index.tsx
+++ b/studio/components/ui/MultiSelect/index.tsx
@@ -112,7 +112,7 @@ export default function MultiSelect({
return (
<div className={`form-group ${disabled ? 'pointer-events-none opacity-50' : ''}`}>
- {label && <label>{label}</label>}
+ {label && <label className="!w-full">{label}</label>}
<div
className={[
'form-control form-control--multi-select',
diff --git a/studio/pages/project/[ref]/database/wrappers.tsx b/studio/pages/project/[ref]/database/wrappers.tsx
index 90f45dd7209..ead5fba1e25 100644
--- a/studio/pages/project/[ref]/database/wrappers.tsx
+++ b/studio/pages/project/[ref]/database/wrappers.tsx
@@ -6,6 +6,7 @@ import { checkPermissions } from 'hooks'
import { DatabaseLayout } from 'components/layouts'
import { Wrappers } from 'components/interfaces/Database'
import NoPermission from 'components/ui/NoPermission'
+import { FormsContainer } from 'components/ui/Forms'
const DatabaseWrappers: NextPageWithLayout = () => {
const canReadWrappers = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'wrappers')
@@ -18,9 +19,7 @@ const DatabaseWrappers: NextPageWithLayout = () => {
DatabaseWrappers.getLayout = (page) => (
<DatabaseLayout title="Wrappers">
- <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 ">
- {page}
- </div>
+ <FormsContainer>{page}</FormsContainer>
</DatabaseLayout>
)