Skip to content

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

Implement optimistic rendering for editing rows in the grid itself

Joshen Lim3743e4574b8f
diff --git a/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx b/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx
index edb9b9084c3..e6abb30516f 100644
--- a/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx
+++ b/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx
@@ -17,7 +17,7 @@ export const ForeignKeyFormatter = (
const rowData = deepClone(p.row)
rowData[p.column.key] = _value
- const { error } = state.rowService!.update(rowData, p.column.key)
+ const { error } = state.rowService!.update(rowData, p.row, p.column.key)
if (error) {
if (state.onError) state.onError(error)
} else {
diff --git a/studio/components/grid/components/grid/Grid.tsx b/studio/components/grid/components/grid/Grid.tsx
index 230e40dabf3..07aee22745b 100644
--- a/studio/components/grid/components/grid/Grid.tsx
+++ b/studio/components/grid/components/grid/Grid.tsx
@@ -41,12 +41,17 @@ export const Grid = memo(
)
if (changedColumn) {
- const { error } = state.rowService!.update(rowData, changedColumn, (payload) => {
- dispatch({
- type: 'EDIT_ROW',
- payload,
- })
- })
+ const { error } = state.rowService!.update(
+ rowData,
+ originRowData,
+ changedColumn,
+ (payload) => {
+ dispatch({
+ type: 'EDIT_ROW',
+ payload,
+ })
+ }
+ )
if (error && onErrorFunc) onErrorFunc(error)
}
}
diff --git a/studio/components/grid/services/row/SqlRowService.ts b/studio/components/grid/services/row/SqlRowService.ts
index 4e4e7226396..5014d78df00 100644
--- a/studio/components/grid/services/row/SqlRowService.ts
+++ b/studio/components/grid/services/row/SqlRowService.ts
@@ -163,12 +163,21 @@ export class SqlRowService implements IRowService {
return rows
}
- update(row: SupaRow, changedColumn?: string, onRowUpdate?: (value: any) => void) {
+ update(
+ row: SupaRow,
+ originalRow: SupaRow,
+ changedColumn?: string,
+ onRowUpdate?: (value: any) => void
+ ) {
const { primaryKeys, error } = this.getPrimaryKeys()
if (error) {
return { error }
}
const { idx, ...value } = row
+
+ // Optimistic rendering
+ if (onRowUpdate) onRowUpdate({ row: value, idx })
+
const matchValues: any = {}
primaryKeys!.forEach((key) => {
matchValues[key] = row[key]
@@ -199,6 +208,9 @@ export class SqlRowService implements IRowService {
if (error) throw error
if (onRowUpdate) onRowUpdate({ row: data[0], idx })
}).catch((error) => {
+ const { idx, ...originalRowData } = originalRow
+ // Revert optimistic rendering if any errors
+ if (onRowUpdate) onRowUpdate({ row: originalRowData, idx })
this.onError(error)
})
diff --git a/studio/components/grid/services/row/index.tsx b/studio/components/grid/services/row/index.tsx
index 3c63b0685a1..fd60208cba0 100644
--- a/studio/components/grid/services/row/index.tsx
+++ b/studio/components/grid/services/row/index.tsx
@@ -28,6 +28,7 @@ export interface IRowService {
update: (
row: SupaRow,
+ originalRow: SupaRow,
changedColumn?: string,
onRowUpdate?: (value: any) => void
) => { row?: SupaRow; error?: ServiceError }