Skip to content

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

fix: Fix various bugs in Text Editor and JSON editor (#28766)

Ivan Vasilovf0505b2454b5
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
index 7d42d26dd09..2ed80b405d2 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
@@ -1,4 +1,6 @@
import { includes, noop } from 'lodash'
+import { Edit, Edit2, Link } from 'lucide-react'
+
import {
Button,
DropdownMenu,
@@ -9,11 +11,9 @@ import {
Listbox,
Select,
} from 'ui'
-
-import { Edit, Edit2, Link } from 'lucide-react'
import { DATETIME_TYPES, JSON_TYPES, TEXT_TYPES } from '../SidePanelEditor.constants'
import { DateTimeInput } from './DateTimeInput'
-import type { RowField } from './RowEditor.types'
+import type { EditValue, RowField } from './RowEditor.types'
import { isValueTruncated } from './RowEditor.utils'
export interface InputFieldProps {
@@ -22,7 +22,7 @@ export interface InputFieldProps {
isEditable?: boolean
onUpdateField?: (changes: object) => void
onEditJson?: (data: any) => void
- onEditText?: (data: any) => void
+ onEditText?: (data: EditValue) => void
onSelectForeignKey?: () => void
}
@@ -173,14 +173,14 @@ const InputField = ({
Set to NULL
</DropdownMenuItem>
<DropdownMenuItem
- onClick={() => onEditText({ column: field.name, value: field.value })}
+ onClick={() => onEditText({ column: field.name, value: field.value || '' })}
>
Expand editor
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
}
- onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}
+ onChange={(event) => onUpdateField({ [field.name]: event.target.value })}
/>
</div>
)
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/JsonEditor/JsonEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/JsonEditor/JsonEditor.tsx
index 572934489b4..27ebfeaba62 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/JsonEditor/JsonEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/JsonEditor/JsonEditor.tsx
@@ -1,7 +1,7 @@
import * as Tooltip from '@radix-ui/react-tooltip'
import { PostgresTable } from '@supabase/postgres-meta'
import { AlignLeft } from 'lucide-react'
-import { useEffect, useState } from 'react'
+import { useCallback, useEffect, useState } from 'react'
import { toast } from 'sonner'
import { useParams } from 'common'
@@ -13,13 +13,13 @@ import useTable from 'hooks/misc/useTable'
import { minifyJSON, prettifyJSON, removeJSONTrailingComma, tryParseJson } from 'lib/helpers'
import { Button, SidePanel, cn } from 'ui'
import ActionBar from '../../ActionBar'
+import { isValueTruncated } from '../RowEditor.utils'
import { DrilldownViewer } from './DrilldownViewer'
import JsonCodeEditor from './JsonCodeEditor'
interface JsonEditProps {
row?: { [key: string]: any }
column: string
- jsonString: string
visible: boolean
backButtonLabel?: string
applyButtonLabel?: string
@@ -31,7 +31,6 @@ interface JsonEditProps {
const JsonEdit = ({
row,
column,
- jsonString,
visible,
backButtonLabel,
applyButtonLabel,
@@ -46,9 +45,12 @@ const JsonEdit = ({
const [view, setView] = useState<'edit' | 'view'>('edit')
const [jsonStr, setJsonStr] = useState('')
- const isTruncated = jsonString.endsWith('...') && jsonString.length > MAX_CHARACTERS
+ // sometimes the value is a JSON object if it was truncated, then fully loaded from the grid.
+ const value = row?.[column as keyof typeof row] as unknown
+ const jsonString = typeof value === 'object' ? JSON.stringify(value) : (value as string)
+ const isTruncated = isValueTruncated(jsonString)
- const { mutate: getCellValue, isLoading, isSuccess } = useGetCellValueMutation()
+ const { mutate: getCellValue, isLoading, isSuccess, reset } = useGetCellValueMutation()
const validateJSON = async (resolve: () => void) => {
try {
@@ -99,6 +101,13 @@ const JsonEdit = ({
}
}, [visible])
+ // reset the mutation when the panel closes. Fixes an issue where the value is truncated if you close and reopen the
+ // panel again
+ const onClose = useCallback(() => {
+ reset()
+ closePanel()
+ }, [reset])
+
return (
<SidePanel
size="large"
@@ -151,11 +160,11 @@ const JsonEdit = ({
</div>
}
visible={visible}
- onCancel={closePanel}
+ onCancel={onClose}
customFooter={
<ActionBar
hideApply={readOnly}
- closePanel={closePanel}
+ closePanel={onClose}
backButtonLabel={backButtonLabel}
applyButtonLabel={applyButtonLabel}
applyFunction={readOnly ? undefined : validateJSON}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
index f6cf07991d0..517cb76fc77 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
@@ -1,12 +1,13 @@
import type { PostgresTable } from '@supabase/postgres-meta'
import { isEmpty, noop, partition } from 'lodash'
import { useEffect, useMemo, useState } from 'react'
-import type { Dictionary } from 'types'
-import { SidePanel } from 'ui'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'
+import type { Dictionary } from 'types'
+import { SidePanel } from 'ui'
import ActionBar from '../ActionBar'
+import { formatForeignKeys } from '../ForeignKeySelector/ForeignKeySelector.utils'
import ForeignRowSelector from './ForeignRowSelector/ForeignRowSelector'
import HeaderTitle from './HeaderTitle'
import InputField from './InputField'
@@ -19,7 +20,6 @@ import {
validateFields,
} from './RowEditor.utils'
import { TextEditor } from './TextEditor'
-import { formatForeignKeys } from '../ForeignKeySelector/ForeignKeySelector.utils'
export interface RowEditorProps {
row?: Dictionary<any>
@@ -39,7 +39,7 @@ const RowEditor = ({
updateEditorDirty = noop,
}: RowEditorProps) => {
const [errors, setErrors] = useState<Dictionary<any>>({})
- const [rowFields, setRowFields] = useState<any[]>([])
+ const [rowFields, setRowFields] = useState<RowField[]>([])
const [selectedValueForTextEdit, setSelectedValueForTextEdit] = useState<EditValue>()
const [selectedValueForJsonEdit, setSelectedValueForJsonEdit] = useState<EditValue>()
@@ -141,6 +141,14 @@ const RowEditor = ({
}
}
+ // Transform the rowFields to a dictionary of column names to values. Used to pass to the TextEditor
+ const editedRow = useMemo(() => {
+ return rowFields.reduce((acc, field) => {
+ acc[field.name] = field.value
+ return acc
+ }, {} as Dictionary<any>)
+ }, [rowFields])
+
return (
<SidePanel
hideFooter
@@ -166,6 +174,7 @@ const RowEditor = ({
errors={errors}
onUpdateField={onUpdateField}
onEditJson={setSelectedValueForJsonEdit}
+ onEditText={setSelectedValueForTextEdit}
onSelectForeignKey={() => onOpenForeignRowSelector(field)}
/>
)
@@ -203,7 +212,7 @@ const RowEditor = ({
<TextEditor
visible={isEditingText}
- row={row}
+ row={editedRow}
column={selectedValueForTextEdit?.column ?? ''}
closePanel={() => setSelectedValueForTextEdit(undefined)}
onSaveField={(value) => {
@@ -213,9 +222,8 @@ const RowEditor = ({
/>
<JsonEditor
visible={isEditingJson}
- row={row}
+ row={editedRow}
column={selectedValueForJsonEdit?.column ?? ''}
- jsonString={selectedValueForJsonEdit?.value ?? ''}
closePanel={() => setSelectedValueForJsonEdit(undefined)}
onSaveJSON={(value) => {
onUpdateField({ [selectedValueForJsonEdit?.column ?? '']: value })
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/TextEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/TextEditor.tsx
index 9670b580c30..6f3c614aee3 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/TextEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/TextEditor.tsx
@@ -1,7 +1,7 @@
import { Editor } from '@monaco-editor/react'
import { PostgresTable } from '@supabase/postgres-meta'
import { Loader } from 'lucide-react'
-import { useEffect, useState } from 'react'
+import { useCallback, useEffect, useState } from 'react'
import remarkGfm from 'remark-gfm'
import { toast } from 'sonner'
@@ -43,7 +43,7 @@ export const TextEditor = ({
const value = row?.[column as keyof typeof row] as unknown as string
const isTruncated = isValueTruncated(value)
- const { mutate: getCellValue, isLoading, isSuccess } = useGetCellValueMutation()
+ const { mutate: getCellValue, isLoading, isSuccess, reset } = useGetCellValueMutation()
const loadFullValue = () => {
if (selectedTable === undefined || project === undefined || row === undefined) return
@@ -78,11 +78,18 @@ export const TextEditor = ({
}
}, [visible])
+ // reset the mutation when the panel closes. Fixes an issue where the value is truncated if you close and reopen the
+ // panel again
+ const onClose = useCallback(() => {
+ reset()
+ closePanel()
+ }, [reset])
+
return (
<SidePanel
size="large"
visible={visible}
- onCancel={() => closePanel()}
+ onCancel={onClose}
header={
<div className="flex items-center justify-between">
<p>
@@ -101,7 +108,7 @@ export const TextEditor = ({
customFooter={
<ActionBar
hideApply={readOnly}
- closePanel={closePanel}
+ closePanel={onClose}
backButtonLabel="Cancel"
applyButtonLabel="Save value"
applyFunction={readOnly ? undefined : saveValue}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 338941dcd64..93b92eacd6d 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -613,8 +613,8 @@ const SidePanelEditor = ({
<SchemaEditor visible={snap.sidePanel?.type === 'schema'} closePanel={onClosePanel} />
<JsonEditor
visible={snap.sidePanel?.type === 'json'}
+ row={(snap.sidePanel?.type === 'json' && snap.sidePanel.jsonValue.row) || {}}
column={(snap.sidePanel?.type === 'json' && snap.sidePanel.jsonValue.column) || ''}
- jsonString={(snap.sidePanel?.type === 'json' && snap.sidePanel.jsonValue.value) || ''}
backButtonLabel="Cancel"
applyButtonLabel="Save changes"
readOnly={!editable}