@@ -29,6 +29,9 @@ export const OrderListTable = () => {
2929 // Track which relationship fields are currently visible/needed
3030 const [ visibleColumns , setVisibleColumns ] = useState < Record < string , boolean > > ( { } )
3131
32+ // Track column order
33+ const [ columnOrder , setColumnOrder ] = useState < string [ ] > ( [ ] )
34+
3235 // Calculate required fields based on visible columns
3336 const requiredFields = useMemo ( ( ) => {
3437 if ( ! apiColumns ?. length ) return DEFAULT_FIELDS
@@ -119,14 +122,7 @@ export const OrderListTable = () => {
119122 return [ ]
120123 }
121124
122- // Sort columns by default_order before mapping
123- const sortedColumns = [ ...apiColumns ] . sort ( ( a , b ) => {
124- const orderA = a . default_order ?? 500
125- const orderB = b . default_order ?? 500
126- return orderA - orderB
127- } )
128-
129- return sortedColumns . map ( apiColumn => {
125+ return apiColumns . map ( apiColumn => {
130126 // Get the display strategy for this column
131127 const displayStrategy = getDisplayStrategy ( apiColumn )
132128
@@ -186,16 +182,28 @@ export const OrderListTable = () => {
186182 setVisibleColumns ( visibility )
187183 } , [ ] )
188184
189- // Initialize visible columns when API columns are loaded
185+ // Initialize visible columns and column order when API columns are loaded
190186 useEffect ( ( ) => {
191- if ( apiColumns ?. length && Object . keys ( visibleColumns ) . length === 0 ) {
192- const initialVisibility : Record < string , boolean > = { }
193- apiColumns . forEach ( column => {
194- initialVisibility [ column . field ] = column . default_visible
195- } )
196- setVisibleColumns ( initialVisibility )
187+ if ( apiColumns ?. length ) {
188+ if ( Object . keys ( visibleColumns ) . length === 0 ) {
189+ const initialVisibility : Record < string , boolean > = { }
190+ apiColumns . forEach ( column => {
191+ initialVisibility [ column . field ] = column . default_visible
192+ } )
193+ setVisibleColumns ( initialVisibility )
194+ }
195+
196+ if ( columnOrder . length === 0 ) {
197+ // Sort columns by default_order before creating initial order
198+ const sortedColumns = [ ...apiColumns ] . sort ( ( a , b ) => {
199+ const orderA = a . default_order ?? 500
200+ const orderB = b . default_order ?? 500
201+ return orderA - orderB
202+ } )
203+ setColumnOrder ( sortedColumns . map ( col => col . field ) )
204+ }
197205 }
198- } , [ apiColumns , visibleColumns ] )
206+ } , [ apiColumns , visibleColumns , columnOrder ] )
199207
200208 if ( isError ) {
201209 throw error
@@ -236,6 +244,8 @@ export const OrderListTable = () => {
236244 enableColumnVisibility
237245 initialColumnVisibility = { initialColumnVisibility }
238246 onColumnVisibilityChange = { handleColumnVisibilityChange }
247+ columnOrder = { columnOrder }
248+ onColumnOrderChange = { setColumnOrder }
239249 isLoading = { isLoading }
240250 pageSize = { PAGE_SIZE }
241251 emptyState = { {
0 commit comments