@@ -20,12 +20,7 @@ import { useEffectAfterMount } from '@Common/Helper'
20
20
import { useStateFilters } from '@Common/Hooks'
21
21
22
22
import { DynamicDataTable } from '../DynamicDataTable'
23
- import {
24
- KeyValueTableDataType ,
25
- KeyValueTableInternalProps ,
26
- KeyValueTableProps ,
27
- KeyValueTableRowType ,
28
- } from './KeyValueTable.types'
23
+ import { KeyValueTableDataType , KeyValueTableInternalProps , KeyValueTableProps } from './KeyValueTable.types'
29
24
import {
30
25
getEmptyRow ,
31
26
getKeyValueHeaders ,
@@ -100,31 +95,26 @@ export const KeyValueTable = ({
100
95
// Sort rows for display purposes only. \
101
96
// The `sortedRows` state is used internally to render the data, while the original `rows` prop remains unaltered during sorting.
102
97
useEffectAfterMount ( ( ) => {
103
- if ( isSortable ) {
104
- // Create a map of rows using their IDs for quick lookup
105
- const rowMap = rows . reduce < Record < KeyValueTableRowType [ 'id' ] , KeyValueTableInternalProps [ 'rows' ] [ number ] > > (
106
- ( acc , row ) => {
107
- acc [ row . id ] = row
108
- return acc
109
- } ,
110
- { } ,
111
- )
98
+ if ( ! isSortable ) {
99
+ // If sorting is disabled, directly set rows without any processing
100
+ setSortedRows ( rows )
101
+ return
102
+ }
112
103
113
- // Create a set of IDs from the currently sorted rows for efficient lookup
114
- const sortedRowSet = new Set ( sortedRows . map ( ( { id } ) => id ) )
104
+ // Create a mapping of row IDs to row objects for quick lookup
105
+ const rowMap = new Map ( rows . map ( ( row ) => [ row . id , row ] ) )
115
106
116
- // Update the sorted rows by filtering out rows that no longer exist and mapping them to the latest data
117
- const updatedSortedRows = sortedRows . filter ( ( { id } ) => rowMap [ id ] ) . map ( ( { id } ) => rowMap [ id ] )
107
+ // Create a set of IDs from the current sorted rows for efficient membership checking
108
+ const sortedRowIds = new Set ( sortedRows . map ( ( row ) => row . id ) )
118
109
119
- // Identify rows that are not part of the current sorted set (new or unsorted rows)
120
- const unsortedRows = rows . filter ( ( { id } ) => ! sortedRowSet . has ( id ) )
110
+ // Update the sorted rows by mapping them to the latest version from `rows` and filtering out any rows that no longer exist
111
+ const updatedSortedRows = sortedRows . map ( ( row ) => rowMap . get ( row . id ) ) . filter ( Boolean )
121
112
122
- // Combine unsorted rows with updated sorted rows and set them as the new sorted rows
123
- setSortedRows ( [ ...unsortedRows , ...updatedSortedRows ] )
124
- } else {
125
- // If sorting is disabled, directly set the rows as the sorted rows
126
- setSortedRows ( rows )
127
- }
113
+ // Find any new rows that are not already in the sorted list
114
+ const newUnsortedRows = rows . filter ( ( row ) => ! sortedRowIds . has ( row . id ) )
115
+
116
+ // Combine new unsorted rows (at the top) with the updated sorted rows (preserving original order)
117
+ setSortedRows ( [ ...newUnsortedRows , ...updatedSortedRows ] )
128
118
} , [ rows ] )
129
119
130
120
// Update the sorted rows whenever the sorting configuration changes
0 commit comments