diff --git a/packages/frontend-core/src/components/sheet/stores/rows.js b/packages/frontend-core/src/components/sheet/stores/rows.js index 6c6fc7e439..91a3ad5f4f 100644 --- a/packages/frontend-core/src/components/sheet/stores/rows.js +++ b/packages/frontend-core/src/components/sheet/stores/rows.js @@ -32,11 +32,11 @@ export const createRowsStore = context => { // Generate a lookup map to quick find a row by ID const rowLookupMap = derived( - enrichedRows, + rows, $rows => { let map = {} - for (let row of $rows) { - map[row._id] = row.__idx + for (let i = 0; i < $rows.length; i++) { + map[$rows[i]._id] = i } return map }, @@ -259,11 +259,6 @@ export const createRowsStore = context => { rows.update(state => { return state.filter(row => !deletedIds.includes(row._id)) }) - - // If we ended up with no rows, try getting the next page - if (!get(rows).length) { - loadNextPage() - } } // Loads the next page of data if available @@ -276,6 +271,11 @@ export const createRowsStore = context => { return await get(fetch)?.refreshDefinition() } + // Checks if we have a row with a certain ID + const hasRow = id => { + return get(rowLookupMap)[id] != null + } + return { rows: { ...rows, @@ -284,6 +284,7 @@ export const createRowsStore = context => { addRow, updateRow, deleteRows, + hasRow, loadNextPage, refreshRow, refreshData, diff --git a/packages/frontend-core/src/components/sheet/stores/ui.js b/packages/frontend-core/src/components/sheet/stores/ui.js index 895fe60d12..fb1bf213ec 100644 --- a/packages/frontend-core/src/components/sheet/stores/ui.js +++ b/packages/frontend-core/src/components/sheet/stores/ui.js @@ -19,19 +19,20 @@ export const createUIStores = context => { ) // Ensure we clear invalid rows from state if they disappear - rows.subscribe($rows => { + rows.subscribe(() => { const $selectedCellId = get(selectedCellId) const $selectedRows = get(selectedRows) const $hoveredRowId = get(hoveredRowId) + const hasRow = rows.actions.hasRow // Check selected cell const selectedRowId = $selectedCellId?.split("-")[0] - if (selectedRowId && !$rows.find(row => row._id === selectedRowId)) { + if (selectedRowId && !hasRow(selectedRowId)) { selectedCellId.set(null) } // Check hovered row - if ($hoveredRowId && !$rows.find(row => row._id === $hoveredRowId)) { + if ($hoveredRowId && !hasRow($hoveredRowId)) { hoveredRowId.set(null) } @@ -40,7 +41,7 @@ export const createUIStores = context => { let selectedRowsNeedsUpdate = false const selectedIds = Object.keys($selectedRows) for (let i = 0; i < selectedIds.length; i++) { - if (!$rows.find(row => row._id === selectedIds[i])) { + if (!hasRow(selectedIds[i])) { delete newSelectedRows[selectedIds[i]] selectedRowsNeedsUpdate = true }