Skip to content

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

Remove MultiSelectEditor, try to improve array editing in table editor

Joshen Limd972cb8c57a4
diff --git a/studio/components/grid/components/editor/MultiSelectEditor.tsx b/studio/components/grid/components/editor/MultiSelectEditor.tsx
deleted file mode 100644
index fb05cb12260..00000000000
--- a/studio/components/grid/components/editor/MultiSelectEditor.tsx
+++ /dev/null
@@ -1,104 +0,0 @@
-import { useState } from 'react'
-import { Popover, IconX, IconPlus } from '@supabase/ui'
-import { EditorProps } from '@supabase/react-data-grid'
-
-import { useTrackedState } from 'components/grid/store'
-import { convertPgArrayToJsArray, convertJsArraytoPgArray } from 'lib/helpers'
-
-interface Props<TRow, TSummaryRow = unknown> extends EditorProps<TRow, TSummaryRow> {
- options?: { label: string; value: string }[]
-}
-
-// Mainly for arrays (specifically enums)
-// Array elements are not unique, so this supports selecting an option more than once
-export function MultiSelectEditor<TRow, TSummaryRow = unknown>({
- row,
- column,
- onRowChange,
- onClose,
- options = [],
-}: Props<TRow, TSummaryRow>) {
- const state = useTrackedState()
- const gridColumn = state.gridColumns.find((x) => x.name == column.key)
-
- const originalValue = row[column.key as keyof TRow] as unknown as string
- const [selectedValues, setSelectedValues] = useState(convertPgArrayToJsArray(originalValue))
-
- const onOpenChange = (open: boolean) => {
- if (!open) {
- const updatedValue = convertJsArraytoPgArray(selectedValues)
- if (updatedValue === originalValue) {
- onClose()
- } else {
- onRowChange({ ...row, [column.key]: updatedValue }, true)
- }
- }
- }
-
- const handleAdd = (option: { label: string; value: string }) => {
- const updatedValues = selectedValues.concat([option.value])
- setSelectedValues(updatedValues)
- }
-
- const handleRemove = (idx: number) => {
- const updatedValues = selectedValues.filter((x, index) => index !== idx)
- setSelectedValues(updatedValues)
- }
-
- return (
- <Popover
- open
- side="bottom"
- align="start"
- sideOffset={-35}
- className="rounded-none"
- onOpenChange={onOpenChange}
- overlay={
- <div style={{ width: `calc(${gridColumn?.width || column.width}px - 2px)` }}>
- <div className="flex gap-2 p-1 flex-wrap">
- {selectedValues.map((value, idx) => (
- <div
- className={[
- 'text-typography-body-light dark:text-typography-body-dark',
- 'flex items-center space-x-2 rounded bg-gray-500',
- 'py-0.5 px-2 text-xs',
- ].join(' ')}
- >
- <span>{value}</span>
- <IconX
- size={12}
- className="cursor-pointer opacity-50 transition hover:opacity-100"
- onClick={(e: any) => {
- e.preventDefault()
- handleRemove(idx)
- }}
- />
- </div>
- ))}
- </div>
- <div
- className="bg-scale-200 p-1 border-t border-scale-500 space-y-1 overflow-y-auto"
- style={{ maxHeight: '160px' }}
- >
- {options.map((option) => (
- <div
- key={option.value}
- className={[
- 'py-1 px-2 rounded group flex items-center justify-between',
- 'hover:bg-scale-400 cursor-pointer transition',
- ].join(' ')}
- onClick={() => handleAdd(option)}
- >
- <p className="text-xs">{option.label}</p>
- <div className="flex items-center space-x-1 opacity-0 group-hover:opacity-100">
- <IconPlus size={12} />
- <p className="text-xs text-scale-1000">Add value</p>
- </div>
- </div>
- ))}
- </div>
- </div>
- }
- />
- )
-}
diff --git a/studio/components/grid/components/editor/index.ts b/studio/components/grid/components/editor/index.ts
index d11ccfedb78..af37e035773 100644
--- a/studio/components/grid/components/editor/index.ts
+++ b/studio/components/grid/components/editor/index.ts
@@ -5,6 +5,5 @@ export * from './JsonEditor'
export * from './NullableBooleanEditor'
export * from './NumberEditor'
export * from './SelectEditor'
-export * from './MultiSelectEditor'
export * from './TextEditor'
export * from './TimeEditor'
diff --git a/studio/components/grid/utils/gridColumns.tsx b/studio/components/grid/utils/gridColumns.tsx
index 7789f664774..69a0724cc7e 100644
--- a/studio/components/grid/utils/gridColumns.tsx
+++ b/studio/components/grid/utils/gridColumns.tsx
@@ -19,17 +19,20 @@ import {
DateTimeEditor,
DateTimeWithTimezoneEditor,
JsonEditor,
- MultiSelectEditor,
NullableBooleanEditor,
NumberEditor,
SelectEditor,
TextEditor,
TimeEditor,
TimeWithTimezoneEditor,
-} from '../components/editor'
-import { AddColumn, ColumnHeader, SelectColumn } from '../components/grid'
-import { COLUMN_MIN_WIDTH } from '../constants'
-import { BooleanFormatter, DefaultFormatter, ForeignKeyFormatter } from '../components/formatter'
+} from 'components/grid/components/editor'
+import { AddColumn, ColumnHeader, SelectColumn } from 'components/grid/components/grid'
+import { COLUMN_MIN_WIDTH } from 'components/grid/constants'
+import {
+ BooleanFormatter,
+ DefaultFormatter,
+ ForeignKeyFormatter,
+} from 'components/grid/components/formatter'
const ESTIMATED_CHARACTER_PIXEL_WIDTH = 9
@@ -111,16 +114,7 @@ function getColumnEditor(columnDefinition: SupaColumn, columnType: ColumnType) {
})
return (p: any) => <SelectEditor {...p} options={options} />
}
- case 'array': {
- if ((columnDefinition?.enum ?? []).length > 0) {
- const options = columnDefinition.enum!.map((x) => {
- return { label: x, value: x }
- })
- return (p: any) => <MultiSelectEditor {...p} options={options} />
- } else {
- return JsonEditor
- }
- }
+ case 'array':
case 'json': {
return JsonEditor
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
index 961d6f98198..d4fe3ec2c99 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
@@ -5,8 +5,6 @@ import { Button, Select, Input, IconLink, IconArrowRight, IconEdit2 } from 'ui'
import { RowField } from './RowEditor.types'
import DateTimeInput from './DateTimeInput'
import { TEXT_TYPES, JSON_TYPES, DATETIME_TYPES } from '../SidePanelEditor.constants'
-import MultiSelect from 'components/ui/MultiSelect'
-import { convertPgArrayToJsArray, convertJsArraytoPgArray, uuidv4 } from 'lib/helpers'
interface Props {
field: RowField
@@ -27,34 +25,28 @@ const InputField: FC<Props> = ({
}) => {
if (field.enums.length > 0) {
const isArray = field.format[0] === '_'
-
if (isArray) {
- const formattedValue = convertPgArrayToJsArray(field?.value ?? '')
- const options = field.enums.map((value: string) => {
- return { id: uuidv4(), value, name: value, disabled: false }
- })
return (
- <div className="grid gap-x-2 md:grid-cols-12 md:gap-x-4">
- <div className="col-span-4">
- <label className="block text-scale-1100 text-sm break-all">{field.name}</label>
- <span className="text-scale-900 text-sm" id="-optional">
- {field.format}
- </span>
- </div>
- <div className="col-span-8">
- <MultiSelect
- allowDuplicateSelection
- options={options}
- value={formattedValue}
- searchPlaceholder="Search for a value"
- placeholder="NULL"
- error={errors[field.name]}
- onChange={(values) => {
- const formattedValues = convertJsArraytoPgArray(values)
- onUpdateField({ [field.name]: formattedValues })
- }}
- />
- </div>
+ <div className="text-area-text-sm">
+ <Input.TextArea
+ layout="horizontal"
+ label={field.name}
+ className="text-sm"
+ descriptionText={field.comment}
+ labelOptional={field.format}
+ disabled={!isEditable}
+ error={errors[field.name]}
+ rows={5}
+ value={field.value ?? ''}
+ placeholder={
+ field.defaultValue === null
+ ? ''
+ : typeof field.defaultValue === 'string' && field.defaultValue.length === 0
+ ? 'Default: Empty string'
+ : `Default: ${field.defaultValue}`
+ }
+ onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}
+ />
</div>
)
} else {
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
index b343b3e02af..bd283c2dd56 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
@@ -139,6 +139,7 @@ const RowEditor: FC<Props> = ({
configuration.rowIdx = row!.idx
}
+ console.log('onSaveChanges', payload)
saveChanges(payload, isNewRecord, configuration, () => setLoading(false))
} else {
setLoading(false)
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
index e3510f70be3..9bcc98427c6 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
@@ -53,6 +53,15 @@ export const generateRowFields = (
export const validateFields = (fields: RowField[]) => {
const errors = {} as any
fields.forEach((field) => {
+ const isArray = field.format.startsWith('_')
+
+ if (isArray && field.value && field.enums.length === 0) {
+ try {
+ JSON.parse(field.value)
+ } catch {
+ errors[field.name] = 'Value is an invalid array'
+ }
+ }
if (field.format.includes('json') && (field.value?.length ?? 0) > 0) {
try {
minifyJSON(field.value ?? '')
@@ -174,8 +183,12 @@ export const generateRowObjectFromFields = (
): object => {
const rowObject = {} as any
fields.forEach((field) => {
+ const isArray = field.format.startsWith('_')
const value = (field?.value ?? '').length === 0 ? null : field.value
- if (field.format.includes('json')) {
+
+ if (isArray && value !== null && field.enums.length === 0) {
+ rowObject[field.name] = tryParseJson(value)
+ } else if (field.format.includes('json')) {
if (typeof field.value === 'object') {
rowObject[field.name] = value
} else if (isString(value)) {
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 1a4ad535f67..7ee7667b8b8 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -1,6 +1,7 @@
import { v4 as _uuidV4 } from 'uuid'
import { post } from 'lib/common/fetch'
import { PASSWORD_STRENGTH, DEFAULT_MINIMUM_PASSWORD_STRENGTH, API_URL } from 'lib/constants'
+import { TEXT_TYPES } from 'components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.constants'
export const tryParseJson = (jsonString: any) => {
try {
@@ -214,19 +215,3 @@ export const detectBrowser = () => {
return 'Safari'
}
}
-
-export const convertPgArrayToJsArray = (value: string): string[] => {
- if (!value) return []
-
- const valueArray = value
- .slice(1, value.length - 1)
- .split(',')
- .map((x) => `"${x}"`)
- const formattedValue = `[${valueArray.join(',')}]`
- return tryParseJson(formattedValue) ?? []
-}
-
-export const convertJsArraytoPgArray = (value: string[]) => {
- if (value.length === 0) return null
- else return `{${value.join(',')}}`
-}