@@ -22,6 +22,32 @@ import { enqueueSnackbar } from "notistack";
22
22
23
23
import "@glideapps/glide-data-grid/dist/index.css" ;
24
24
25
+ const columnIndexToLetter = ( index ) => {
26
+ let temp = index + 1 ;
27
+ let letter = "" ;
28
+ while ( temp > 0 ) {
29
+ let remainder = ( temp - 1 ) % 26 ;
30
+ letter = String . fromCharCode ( 65 + remainder ) + letter ;
31
+ temp = Math . floor ( ( temp - 1 ) / 26 ) ;
32
+ }
33
+ return letter ;
34
+ } ;
35
+
36
+ const cellIdToGridCell = ( cellId , columns ) => {
37
+ const match = cellId . match ( / ( [ A - Z ] + ) ( \d + ) / ) ;
38
+ if ( ! match ) return null ;
39
+ const [ , colLetter , rowString ] = match ;
40
+ const row = parseInt ( rowString , 10 ) - 1 ;
41
+ const col = columns . findIndex ( ( c ) => c . col === colLetter ) ;
42
+ return [ col , row ] ;
43
+ } ;
44
+
45
+ const gridCellToCellId = ( gridCell , columns ) => {
46
+ const [ colIndex , rowIndex ] = gridCell ;
47
+ const colLetter = columns [ colIndex ] . col ;
48
+ return `${ colLetter } ${ rowIndex + 1 } ` ;
49
+ } ;
50
+
25
51
const SheetHeader = ( { sheet, setRunId, hasChanges, onSave } ) => {
26
52
const navigate = useNavigate ( ) ;
27
53
@@ -129,16 +155,15 @@ function Sheet(props) {
129
155
130
156
const getCellContent = useCallback (
131
157
( [ column , row ] ) => {
132
- // Find the column in cells
133
- const col = columns [ column ] . col ;
134
- const cell = cells [ row ] ?. [ col ] ;
158
+ const colLetter = columns [ column ] . col ;
159
+ const cell = cells [ `${ colLetter } ${ row + 1 } ` ] ;
135
160
136
161
return {
137
162
kind :
138
163
( cell ?. kind === "app_run" ? GridCellKind . Text : cell ?. kind ) ||
139
164
columns [ column ] . kind ,
140
- displayData : cell ?. displayData || cell ?. data || "" ,
141
- data : cell ?. data || "" ,
165
+ displayData : cell ?. display_data || "" ,
166
+ data : cell ?. display_data || "" ,
142
167
allowOverlay : true ,
143
168
allowWrapping : true ,
144
169
skeletonWidth : 80 ,
@@ -153,9 +178,9 @@ function Sheet(props) {
153
178
return [ ] ;
154
179
}
155
180
156
- return columns . map ( ( column ) => {
181
+ return columns . map ( ( column , index ) => {
157
182
return {
158
- col : column . col ,
183
+ col : columnIndexToLetter ( index ) ,
159
184
title : column . title ,
160
185
kind : column . kind ,
161
186
data : column . data ,
@@ -231,41 +256,44 @@ function Sheet(props) {
231
256
232
257
const onCellEdited = useCallback (
233
258
( cell , value ) => {
259
+ const [ col , row ] = cell ;
260
+ const cellId = gridCellToCellId ( cell , columns ) ;
261
+
234
262
setCells ( ( prevCells ) => {
235
263
const newCells = {
236
264
...prevCells ,
237
- [ cell [ 1 ] ] : {
238
- ...prevCells [ cell [ 1 ] ] ,
239
- [ columns [ cell [ 0 ] ] . col ] : {
240
- ...( prevCells [ cell [ 1 ] ] ?. [ columns [ cell [ 0 ] ] . col ] || { } ) ,
241
- ...value ,
242
- kind : columns [ cell [ 0 ] ] . kind ,
243
- data : value . data ,
244
- displayData : value . displayData || value . data ,
245
- } ,
265
+ [ cellId ] : {
266
+ ...( prevCells [ cellId ] || { } ) ,
267
+ row : row + 1 ,
268
+ col : columns [ col ] ?. col ,
269
+ kind : GridCellKind . Text ,
270
+ display_data : value . displayData || value . data ,
246
271
} ,
247
272
} ;
248
- updateUserChanges ( "cells" , ` ${ cell [ 1 ] } - ${ columns [ cell [ 0 ] ] . col } ` , value ) ;
273
+ updateUserChanges ( "cells" , cellId , value ) ;
249
274
return newCells ;
250
275
} ) ;
251
276
sheetRef . current ?. updateCells ( [ { cell : cell } ] ) ;
252
277
} ,
253
- [ columns , updateUserChanges ] ,
278
+ [ columns , updateUserChanges , sheetRef ] ,
254
279
) ;
255
280
256
281
const onRowAppended = useCallback ( ( ) => {
282
+ const newRowIndex = numRows + 1 ;
283
+ const newCells = columns . reduce ( ( acc , column ) => {
284
+ const cellId = `${ column . col } ${ newRowIndex } ` ;
285
+ acc [ cellId ] = {
286
+ kind : GridCellKind . Text ,
287
+ display_data : "" ,
288
+ row : newRowIndex ,
289
+ col : column . col ,
290
+ } ;
291
+ return acc ;
292
+ } , { } ) ;
293
+
257
294
setCells ( ( prevCells ) => ( {
258
295
...prevCells ,
259
- [ numRows ] : {
260
- ...columns . reduce ( ( acc , column ) => {
261
- acc [ column . col ] = {
262
- kind : GridCellKind . Text ,
263
- displayData : "" ,
264
- data : "" ,
265
- } ;
266
- return acc ;
267
- } , { } ) ,
268
- } ,
296
+ ...newCells ,
269
297
} ) ) ;
270
298
setNumRows ( ( prevNumRows ) => {
271
299
const newNumRows = prevNumRows + 1 ;
@@ -278,12 +306,9 @@ function Sheet(props) {
278
306
return new Promise ( ( resolve , reject ) => {
279
307
const updatedSheet = {
280
308
...sheet ,
281
- data : {
282
- ...sheet . data ,
283
- columns : columns ,
284
- cells : cells ,
285
- total_rows : numRows ,
286
- } ,
309
+ columns : columns ,
310
+ cells : cells ,
311
+ total_rows : numRows ,
287
312
} ;
288
313
axios ( )
289
314
. patch ( `/api/sheets/${ sheet . uuid } ` , updatedSheet )
@@ -320,47 +345,37 @@ function Sheet(props) {
320
345
321
346
if ( event . type === "cell.update" ) {
322
347
const cell = event . cell ;
323
- const [ row , col ] = cell . id . split ( "-" ) ;
324
- const column = columns . find ( ( c ) => c . col === col ) ;
325
-
326
- setCells ( ( cells ) => ( {
327
- ...cells ,
328
- [ row ] : {
329
- ...cells [ row ] ,
330
- [ col ] : {
331
- ...cells [ row ] ?. [ col ] ,
332
- ...{
333
- kind : column ?. kind || GridCellKind . Text ,
334
- data : cell . data ,
335
- displayData : cell . data ,
336
- } ,
348
+ const gridCell = cellIdToGridCell ( cell . id , columns ) ;
349
+ if ( gridCell ) {
350
+ const [ colIndex ] = gridCell ;
351
+ const column = columns [ colIndex ] ;
352
+
353
+ setCells ( ( cells ) => ( {
354
+ ...cells ,
355
+ [ cell . id ] : {
356
+ ...cells [ cell . id ] ,
357
+ kind : column ?. kind || GridCellKind . Text ,
358
+ data : cell . data ,
359
+ display_data : cell . data ,
337
360
} ,
338
- } ,
339
- } ) ) ;
361
+ } ) ) ;
340
362
341
- sheetRef . current ?. updateCells ( [
342
- { cell : [ columns . findIndex ( ( c ) => c . col === col ) , row ] } ,
343
- ] ) ;
363
+ sheetRef . current ?. updateCells ( [ { cell : gridCell } ] ) ;
364
+ }
344
365
} else if ( event . type === "cell.updating" ) {
345
366
const cell = event . cell ;
346
- const [ row , col ] = cell . id . split ( "-" ) ;
347
-
348
- setCells ( ( cells ) => ( {
349
- ...cells ,
350
- [ row ] : {
351
- ...cells [ row ] ,
352
- [ col ] : {
353
- ...cells [ row ] ?. [ col ] ,
354
- ...{
355
- kind : GridCellKind . Loading ,
356
- } ,
367
+ const gridCell = cellIdToGridCell ( cell . id , columns ) ;
368
+ if ( gridCell ) {
369
+ setCells ( ( cells ) => ( {
370
+ ...cells ,
371
+ [ cell . id ] : {
372
+ ...cells [ cell . id ] ,
373
+ kind : GridCellKind . Loading ,
357
374
} ,
358
- } ,
359
- } ) ) ;
375
+ } ) ) ;
360
376
361
- sheetRef . current ?. updateCells ( [
362
- { cell : [ columns . findIndex ( ( c ) => c . col === col ) , row ] } ,
363
- ] ) ;
377
+ sheetRef . current ?. updateCells ( [ { cell : gridCell } ] ) ;
378
+ }
364
379
}
365
380
} ) ;
366
381
@@ -399,7 +414,9 @@ function Sheet(props) {
399
414
}
400
415
onCellEdited = { onCellEdited }
401
416
onHeaderMenuClick = { ( column , bounds ) => {
402
- setSelectedColumnId ( column ) ;
417
+ setSelectedColumnId (
418
+ columns . findIndex ( ( c ) => c . col === columnIndexToLetter ( column ) ) ,
419
+ ) ;
403
420
editColumnAnchorEl . current = {
404
421
getBoundingClientRect : ( ) => DOMRect . fromRect ( bounds ) ,
405
422
} ;
@@ -426,7 +443,8 @@ function Sheet(props) {
426
443
updateColumn = { ( column ) => {
427
444
setColumns ( ( columns ) => {
428
445
const newColumns = [ ...columns ] ;
429
- newColumns [ selectedColumnId ] = column ;
446
+ const index = columns . findIndex ( ( c ) => c . col === column . col ) ;
447
+ newColumns [ index ] = column ;
430
448
updateUserChanges ( "columns" , selectedColumnId , column ) ;
431
449
return newColumns ;
432
450
} ) ;
0 commit comments