Skip to content

Commit eb17564

Browse files
authored
Merge pull request #227 from gabrielvincent/edit-cell-deadlock
Fix: UI freeze when editing cells in tables without primary keys
2 parents 3e5c518 + fa4333b commit eb17564

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

components/results_table.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ func (table *ResultsTable) subscribeToSidebarChanges() {
257257
}
258258

259259
logger.Info("eventSidebarCommitEditing", map[string]any{"cellValue": cellValue, "params": params, "rowIndex": row, "changedColumnIndex": changedColumnIndex})
260-
table.AppendNewChange(models.DMLUpdateType, row, changedColumnIndex, cellValue)
260+
err := table.AppendNewChange(models.DMLUpdateType, row, changedColumnIndex, cellValue)
261+
if err != nil {
262+
table.SetError(err.Error(), nil)
263+
}
261264

262265
App.ForceDraw()
263266
case eventSidebarError:
@@ -466,7 +469,10 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
466469
}
467470
}
468471
} else {
469-
table.AppendNewChange(models.DMLDeleteType, selectedRowIndex, -1, models.CellValue{TableColumnIndex: -1, TableRowIndex: selectedRowIndex, Column: table.GetColumnNameByIndex(selectedColumnIndex)})
472+
err := table.AppendNewChange(models.DMLDeleteType, selectedRowIndex, -1, models.CellValue{TableColumnIndex: -1, TableRowIndex: selectedRowIndex, Column: table.GetColumnNameByIndex(selectedColumnIndex)})
473+
if err != nil {
474+
table.SetError(err.Error(), nil)
475+
}
470476
}
471477

472478
}
@@ -484,7 +490,10 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
484490
table.FinishSettingValue()
485491

486492
if selection >= 0 {
487-
table.AppendNewChange(models.DMLUpdateType, selectedRowIndex, selectedColumnIndex, models.CellValue{Type: selection, Value: value, Column: table.GetColumnNameByIndex(selectedColumnIndex)})
493+
err := table.AppendNewChange(models.DMLUpdateType, selectedRowIndex, selectedColumnIndex, models.CellValue{Type: selection, Value: value, Column: table.GetColumnNameByIndex(selectedColumnIndex)})
494+
if err != nil {
495+
table.SetError(err.Error(), nil)
496+
}
488497
}
489498
})
490499

@@ -1016,6 +1025,8 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV
10161025
inputField.SetFieldTextColor(app.Styles.PrimitiveBackgroundColor)
10171026
inputField.SetBorder(true)
10181027

1028+
initialText := cell.Text
1029+
10191030
inputField.SetDoneFunc(func(key tcell.Key) {
10201031
table.SetIsEditing(false)
10211032
currentValue := cell.Text
@@ -1026,11 +1037,13 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV
10261037
columnName = strings.ReplaceAll(columnName, " ▼", "")
10271038
columnName = strings.ReplaceAll(columnName, " ▲", "")
10281039

1040+
var appendErr error
1041+
10291042
if key != tcell.KeyEscape {
10301043
cell.SetText(newValue)
10311044

10321045
if currentValue != newValue {
1033-
table.AppendNewChange(models.DMLUpdateType, row, col, models.CellValue{Type: models.String, Value: newValue, Column: columnName, TableColumnIndex: col, TableRowIndex: row})
1046+
appendErr = table.AppendNewChange(models.DMLUpdateType, row, col, models.CellValue{Type: models.String, Value: newValue, Column: columnName, TableColumnIndex: col, TableRowIndex: row})
10341047
}
10351048

10361049
switch key {
@@ -1057,9 +1070,18 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV
10571070
}
10581071

10591072
if key == tcell.KeyEnter || key == tcell.KeyEscape {
1073+
if appendErr != nil {
1074+
cell.Text = initialText
1075+
}
1076+
10601077
table.SetInputCapture(table.tableInputCapture)
10611078
table.Page.RemovePage(pageNameTableEditCell)
1062-
App.SetFocus(table)
1079+
1080+
if appendErr != nil {
1081+
table.SetError(appendErr.Error(), nil)
1082+
} else {
1083+
App.SetFocus(table)
1084+
}
10631085
}
10641086

10651087
if callback != nil {
@@ -1125,7 +1147,7 @@ func (table *ResultsTable) MutateInsertedRowCell(rowID string, newValue models.C
11251147
}
11261148
}
11271149

1128-
func (table *ResultsTable) AppendNewChange(changeType models.DMLType, rowIndex int, colIndex int, value models.CellValue) {
1150+
func (table *ResultsTable) AppendNewChange(changeType models.DMLType, rowIndex int, colIndex int, value models.CellValue) error {
11291151
// case models.Empty:
11301152
// placeholders = append(placeholders, "")
11311153
databaseName := table.GetDatabaseName()
@@ -1142,14 +1164,13 @@ func (table *ResultsTable) AppendNewChange(changeType models.DMLType, rowIndex i
11421164

11431165
if isAnInsertedRow {
11441166
table.MutateInsertedRowCell(tableCellReference.(string), value)
1145-
return
1167+
return nil
11461168
}
11471169

11481170
rowPrimaryKeyInfo := table.GetPrimaryKeyValue(rowIndex)
11491171

11501172
if len(rowPrimaryKeyInfo) == 0 {
1151-
table.SetError(fmt.Sprintf("Primary key not found for row %d", rowIndex), nil)
1152-
return
1173+
return fmt.Errorf("Primary key not found for row %d", rowIndex)
11531174
}
11541175

11551176
if changeType == models.DMLUpdateType {
@@ -1231,6 +1252,8 @@ func (table *ResultsTable) AppendNewChange(changeType models.DMLType, rowIndex i
12311252

12321253
*table.state.listOfDBChanges = append(*table.state.listOfDBChanges, newDMLChange)
12331254
}
1255+
1256+
return nil
12341257
}
12351258

12361259
func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) []models.PrimaryKeyInfo {

0 commit comments

Comments
 (0)