Skip to content

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

Prevent ForeignRowSelector from firing multiple saves when clicked (#41381)

Joshen Lim4c62839e2842
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
index 3e50ed6ec18..11c008d3839 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
@@ -66,10 +66,12 @@ export const ForeignKeySelector = ({
const hasTypeErrors = (errors.types ?? []).length > 0
const hasTypeNotices = (errors.typeNotice ?? []).length > 0
- const { data: schemas } = useSchemasQuery({
+ const { data: schemas = [] } = useSchemasQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
})
+ const sortedSchemas = [...schemas].sort((a, b) => a.name.localeCompare(b.name))
+
const { data: tables } = useTablesQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
@@ -145,7 +147,7 @@ export const ForeignKeySelector = ({
setFk({ ...fk, [action]: value })
}
- const validateSelection = (resolve: any) => {
+ const validateSelection = (resolve: () => void) => {
const errors: SelectorErrors = {}
const incompleteColumns = fk.columns.filter(
(column) => column.source === '' || column.target === ''
@@ -204,11 +206,13 @@ export const ForeignKeySelector = ({
if (foreignKey !== undefined) setFk(foreignKey)
else setFk({ ...EMPTY_STATE, id: uuidv4() })
}
+ // eslint-disable-next-line react-hooks/exhaustive-deps
}, [visible])
useEffect(() => {
if (visible) validateType()
- }, [fk])
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [fk, visible])
return (
<SidePanel
@@ -222,7 +226,7 @@ export const ForeignKeySelector = ({
disableApply={disableApply}
applyButtonLabel="Save"
closePanel={onClose}
- applyFunction={(resolve: any) => validateSelection(resolve)}
+ applyFunction={(resolve) => validateSelection(resolve)}
/>
}
>
@@ -244,7 +248,7 @@ export const ForeignKeySelector = ({
value={fk.schema}
onChange={(value: string) => updateSelectedSchema(value)}
>
- {schemas?.map((schema) => {
+ {sortedSchemas.map((schema) => {
return (
<Listbox.Option
key={schema.id}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
index efd1891c9ac..02ac21c5f5f 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
@@ -1,4 +1,4 @@
-import { Loader2 } from 'lucide-react'
+import { Loader2, X } from 'lucide-react'
import { useEffect, useState } from 'react'
import { DndProvider } from 'react-dnd'
import { HTML5Backend } from 'react-dnd-html5-backend'
@@ -24,7 +24,6 @@ import {
} from 'state/role-impersonation-state'
import { TableEditorTableStateContextProvider } from 'state/table-editor-table'
import { Button, SidePanel } from 'ui'
-import { ActionBar } from '../../ActionBar'
import { ForeignKey } from '../../ForeignKeySelector/ForeignKeySelector.types'
import { convertByteaToHex } from '../RowEditor.utils'
import Pagination from './Pagination'
@@ -35,6 +34,7 @@ const FOREIGN_ROW_SELECTOR_TABLE_NAME_SUFFIX = '__frselector'
export interface ForeignRowSelectorProps {
visible: boolean
foreignKey?: ForeignKey
+ isSaving?: boolean
onSelect: (value?: { [key: string]: any }) => void
closePanel: () => void
}
@@ -42,6 +42,7 @@ export interface ForeignRowSelectorProps {
export const ForeignRowSelector = ({
visible,
foreignKey,
+ isSaving,
onSelect,
closePanel,
}: ForeignRowSelectorProps) => {
@@ -167,18 +168,29 @@ export const ForeignRowSelector = ({
return (
<SidePanel
+ hideFooter
visible={visible}
size="large"
header={
- <div>
- Select a record to reference from{' '}
- <code className="text-code-inline !text-sm">
- {schemaName}.{tableName}
- </code>
+ <div className="flex items-center justify-between">
+ <p>
+ Select a record to reference from{' '}
+ <code className="text-code-inline !text-sm">
+ {schemaName}.{tableName}
+ </code>
+ </p>
+ <div className="flex items-center gap-x-4">
+ {isSaving && (
+ <div className="flex items-center gap-x-2">
+ <Loader2 className="animate-spin" size={12} />
+ <p className="text-xs text-foreground-light">Saving</p>
+ </div>
+ )}
+ <Button type="text" icon={<X />} className="w-7" onClick={closePanel} />
+ </div>
</div>
}
onCancel={closePanel}
- customFooter={<ActionBar hideApply backButtonLabel="Cancel" closePanel={closePanel} />}
>
<SidePanel.Content className="h-full !px-0">
<div className="h-full">
@@ -252,15 +264,19 @@ export const ForeignRowSelector = ({
<SelectorGrid
rows={data.rows}
onRowSelect={(row) => {
- const value = columns?.reduce((a, b) => {
- const targetColumn = selectedTable?.columns.find((x) => x.name === b.target)
- const value =
- targetColumn?.format === 'bytea'
- ? convertByteaToHex(row[b.target])
- : row[b.target]
- return { ...a, [b.source]: value }
- }, {})
- onSelect(value)
+ if (!isSaving) {
+ const value = columns?.reduce((a, b) => {
+ const targetColumn = selectedTable?.columns.find(
+ (x) => x.name === b.target
+ )
+ const value =
+ targetColumn?.format === 'bytea'
+ ? convertByteaToHex(row[b.target])
+ : row[b.target]
+ return { ...a, [b.source]: value }
+ }, {})
+ onSelect(value)
+ }
}}
/>
) : (
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 80a1ba852d5..4485a06f332 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -6,7 +6,6 @@ import { toast } from 'sonner'
import { useParams } from 'common'
import { type GeneratedPolicy } from 'components/interfaces/Auth/Policies/Policies.utils'
-import { useDatabasePolicyCreateMutation } from 'data/database-policies/database-policy-create-mutation'
import { databasePoliciesKeys } from 'data/database-policies/keys'
import { useDatabasePublicationCreateMutation } from 'data/database-publications/database-publications-create-mutation'
import { useDatabasePublicationsQuery } from 'data/database-publications/database-publications-query'
@@ -168,7 +167,7 @@ export const SidePanelEditor = ({
toast.success('Successfully created row')
},
})
- const { mutateAsync: updateTableRow } = useTableRowUpdateMutation({
+ const { mutateAsync: updateTableRow, isPending: isUpdatingRow } = useTableRowUpdateMutation({
onSuccess() {
toast.success('Successfully updated row')
},
@@ -177,9 +176,6 @@ export const SidePanelEditor = ({
const { mutateAsync: updatePublication } = useDatabasePublicationUpdateMutation({
onError: () => {},
})
- const { mutateAsync: createPolicy } = useDatabasePolicyCreateMutation({
- onError: () => {}, // Errors handled inline
- })
const isDuplicating = snap.sidePanel?.type === 'table' && snap.sidePanel.mode === 'duplicate'
@@ -785,6 +781,7 @@ export const SidePanelEditor = ({
? snap.sidePanel.foreignKey.foreignKey
: undefined
}
+ isSaving={isUpdatingRow}
closePanel={onClosePanel}
onSelect={onSaveForeignRow}
/>