@@ -10,6 +10,7 @@ import type { Column } from '../Table/index.js'
10
10
import { useConfig } from '../../providers/Config/index.js'
11
11
import { usePreferences } from '../../providers/Preferences/index.js'
12
12
import { useServerFunctions } from '../../providers/ServerFunctions/index.js'
13
+ import { abortAndIgnore } from '../../utilities/abortAndIgnore.js'
13
14
14
15
export interface ITableColumns {
15
16
columns : Column [ ]
@@ -77,25 +78,36 @@ export const TableColumnsProvider: React.FC<Props> = ({
77
78
const { getPreference, setPreference } = usePreferences ( )
78
79
79
80
const [ tableColumns , setTableColumns ] = React . useState ( columnState )
81
+ const tableStateControllerRef = React . useRef < AbortController > ( null )
80
82
81
83
const moveColumn = useCallback (
82
84
async ( args : { fromIndex : number ; toIndex : number } ) => {
85
+ abortAndIgnore ( tableStateControllerRef . current )
86
+
83
87
const { fromIndex, toIndex } = args
84
88
const withMovedColumn = [ ...tableColumns ]
85
89
const [ columnToMove ] = withMovedColumn . splice ( fromIndex , 1 )
86
90
withMovedColumn . splice ( toIndex , 0 , columnToMove )
87
91
88
- const { state : columnState , Table } = await getTableState ( {
92
+ setTableColumns ( withMovedColumn )
93
+
94
+ const controller = new AbortController ( )
95
+ tableStateControllerRef . current = controller
96
+
97
+ const result = await getTableState ( {
89
98
collectionSlug,
90
99
columns : sanitizeColumns ( withMovedColumn ) ,
91
100
docs,
92
101
enableRowSelections,
93
102
renderRowTypes,
103
+ signal : controller . signal ,
94
104
tableAppearance,
95
105
} )
96
106
97
- setTableColumns ( columnState )
98
- setTable ( Table )
107
+ if ( result ) {
108
+ setTableColumns ( result . state )
109
+ setTable ( result . Table )
110
+ }
99
111
} ,
100
112
[
101
113
tableColumns ,
@@ -111,24 +123,55 @@ export const TableColumnsProvider: React.FC<Props> = ({
111
123
112
124
const toggleColumn = useCallback (
113
125
async ( column : string ) => {
114
- const toggledColumns : Pick < Column , 'accessor' | 'active' > [ ] = tableColumns . map ( ( col ) => {
115
- return {
116
- accessor : col . accessor ,
117
- active : col . accessor === column ? ! col . active : col . active ,
118
- }
119
- } )
126
+ abortAndIgnore ( tableStateControllerRef . current )
127
+
128
+ const { newColumnState, toggledColumns } = tableColumns . reduce < {
129
+ newColumnState : Column [ ]
130
+ toggledColumns : Pick < Column , 'accessor' | 'active' > [ ]
131
+ } > (
132
+ ( acc , col ) => {
133
+ if ( col . accessor === column ) {
134
+ acc . newColumnState . push ( {
135
+ ...col ,
136
+ accessor : col . accessor ,
137
+ active : ! col . active ,
138
+ } )
139
+ acc . toggledColumns . push ( {
140
+ accessor : col . accessor ,
141
+ active : ! col . active ,
142
+ } )
143
+ } else {
144
+ acc . newColumnState . push ( col )
145
+ acc . toggledColumns . push ( {
146
+ accessor : col . accessor ,
147
+ active : col . active ,
148
+ } )
149
+ }
120
150
121
- const { state : columnState , Table } = await getTableState ( {
151
+ return acc
152
+ } ,
153
+ { newColumnState : [ ] , toggledColumns : [ ] } ,
154
+ )
155
+
156
+ setTableColumns ( newColumnState )
157
+
158
+ const controller = new AbortController ( )
159
+ tableStateControllerRef . current = controller
160
+
161
+ const result = await getTableState ( {
122
162
collectionSlug,
123
163
columns : toggledColumns ,
124
164
docs,
125
165
enableRowSelections,
126
166
renderRowTypes,
167
+ signal : controller . signal ,
127
168
tableAppearance,
128
169
} )
129
170
130
- setTableColumns ( columnState )
131
- setTable ( Table )
171
+ if ( result ) {
172
+ setTableColumns ( result . state )
173
+ setTable ( result . Table )
174
+ }
132
175
} ,
133
176
[
134
177
tableColumns ,
@@ -233,6 +276,12 @@ export const TableColumnsProvider: React.FC<Props> = ({
233
276
sortColumnProps ,
234
277
] )
235
278
279
+ React . useEffect ( ( ) => {
280
+ return ( ) => {
281
+ abortAndIgnore ( tableStateControllerRef . current )
282
+ }
283
+ } , [ ] )
284
+
236
285
return (
237
286
< TableColumnContext . Provider
238
287
value = { {
0 commit comments