@@ -412,30 +412,77 @@ export function selectDefaultSortableFields(
412412 defaultSortable = [ COMMIT_DATE , ...defaultSortable ] ;
413413 }
414414
415- return defaultSortable as string [ ] ;
415+ // Return as objects with field property
416+ return defaultSortable . map ( field => ( { field } ) ) as {
417+ field : string ;
418+ label ?: string ;
419+ default_sort ?: boolean | 'asc' | 'desc' ;
420+ } [ ] ;
416421}
417422
418423export function selectSortableFields ( collection : Collection , t : ( key : string ) => string ) {
419424 const fields = collection
420425 . get ( 'sortable_fields' )
421426 . toArray ( )
422- . map ( key => {
427+ . map ( sortableField => {
428+ // Extract the field name and custom label from the sortable field object
429+ const key = sortableField . get ( 'field' ) ;
430+ const customLabel = sortableField . get ( 'label' ) ;
431+
423432 if ( key === COMMIT_DATE ) {
424- return { key, field : { name : key , label : t ( 'collection.defaultFields.updatedOn.label' ) } } ;
433+ const label = customLabel || t ( 'collection.defaultFields.updatedOn.label' ) ;
434+ return { key, field : { name : key , label } } ;
425435 }
426436 const field = selectField ( collection , key ) ;
427437 if ( key === COMMIT_AUTHOR && ! field ) {
428- return { key, field : { name : key , label : t ( 'collection.defaultFields.author.label' ) } } ;
438+ const label = customLabel || t ( 'collection.defaultFields.author.label' ) ;
439+ return { key, field : { name : key , label } } ;
440+ }
441+
442+ let fieldObj : Record < string , unknown > | undefined = field ?. toJS ( ) ;
443+
444+ // If custom label is provided, override the field's label
445+ if ( fieldObj && customLabel ) {
446+ fieldObj = { ...fieldObj , label : customLabel } ;
429447 }
430448
431- return { key, field : field ?. toJS ( ) } ;
449+ // If no label exists at all, use the field name
450+ if ( fieldObj && ! fieldObj . label ) {
451+ fieldObj = { ...fieldObj , label : ( fieldObj . name as string ) || key } ;
452+ }
453+
454+ return { key, field : fieldObj } ;
432455 } )
433456 . filter ( item => ! ! item . field )
434457 . map ( item => ( { ...item . field , key : item . key } ) ) ;
435458
436459 return fields ;
437460}
438461
462+ export function selectDefaultSortField ( collection : Collection ) {
463+ const sortableFields = collection . get ( 'sortable_fields' ) . toArray ( ) ;
464+ const defaultField = sortableFields . find ( field => field . get ( 'default_sort' ) !== undefined ) ;
465+
466+ if ( ! defaultField ) {
467+ return null ;
468+ }
469+
470+ const fieldName = defaultField . get ( 'field' ) ;
471+ const defaultSortValue = defaultField . get ( 'default_sort' ) ;
472+
473+ // Determine direction based on default_sort value
474+ let direction ;
475+ if ( defaultSortValue === true || defaultSortValue === 'asc' ) {
476+ direction = 'asc' ;
477+ } else if ( defaultSortValue === 'desc' ) {
478+ direction = 'desc' ;
479+ } else {
480+ direction = 'asc' ; // fallback
481+ }
482+
483+ return { field : fieldName , direction } ;
484+ }
485+
439486export function selectSortDataPath ( collection : Collection , key : string ) {
440487 if ( key === COMMIT_DATE ) {
441488 return 'updatedOn' ;
0 commit comments