@@ -20,12 +20,7 @@ import { useEffectAfterMount } from '@Common/Helper'
2020import { useStateFilters } from '@Common/Hooks'
2121
2222import { 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'
2924import {
3025 getEmptyRow ,
3126 getKeyValueHeaders ,
@@ -100,31 +95,26 @@ export const KeyValueTable = ({
10095 // Sort rows for display purposes only. \
10196 // The `sortedRows` state is used internally to render the data, while the original `rows` prop remains unaltered during sorting.
10297 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+ }
112103
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 ] ) )
115106
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 ) )
118109
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 )
121112
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 ] )
128118 } , [ rows ] )
129119
130120 // Update the sorted rows whenever the sorting configuration changes
0 commit comments