33 useEffect ,
44 FC ,
55} from 'react' ;
6+ import { useIntl } from '@edx/frontend-platform/i18n' ;
67import {
78 DataTableContext ,
89 Dropdown ,
@@ -28,38 +29,33 @@ const SORT_BY_OPTIONS: SortByOptions = {
2829 oldest : { id : 'createdAt' , desc : false } ,
2930} ;
3031
31- const SORT_LABELS : Record < string , string > = {
32- 'name-a-z' : 'Name A-Z' ,
33- 'name-z-a' : 'Name Z-A' ,
34- newest : 'Newest' ,
35- oldest : 'Oldest' ,
36- } ;
37-
3832const SortDropdown : FC = ( ) => {
33+ const intl = useIntl ( ) ;
3934 const { toggleSortBy, state } = useContext < DataTableContext > ( DataTableContext ) ;
4035 const [ sortOrder , setSortOrder ] = useState < string | undefined > ( undefined ) ;
4136
42- // Get current sort state from DataTable context
37+ const SORT_LABELS : Record < string , string > = useMemo ( ( ) => ( {
38+ 'name-a-z' : intl . formatMessage ( { id : 'authz.libraries.team.table.sort.name-a-z' , defaultMessage : 'Name A-Z' } ) ,
39+ 'name-z-a' : intl . formatMessage ( { id : 'authz.libraries.team.table.sort.name-z-a' , defaultMessage : 'Name Z-A' } ) ,
40+ newest : intl . formatMessage ( { id : 'authz.libraries.team.table.sort.newest' , defaultMessage : 'Newest' } ) ,
41+ oldest : intl . formatMessage ( { id : 'authz.libraries.team.table.sort.oldest' , defaultMessage : 'Oldest' } ) ,
42+ // eslint-disable-next-line react-hooks/exhaustive-deps
43+ } ) , [ ] ) ;
44+
4345 const currentSort = useMemo ( ( ) => {
4446 if ( ! state ?. sortBy ?. length ) { return undefined ; }
4547
4648 const activeSortBy = state . sortBy [ 0 ] ;
4749 return Object . entries ( SORT_BY_OPTIONS ) . find (
4850 ( [ , option ] ) => option . id === activeSortBy . id && option . desc === activeSortBy . desc ,
49- ) ?. [ 0 ] ;
51+ ) ?. [ 0 ] ; // return the key
5052 } , [ state ?. sortBy ] ) ;
5153
52- // Update local state when external sort changes
5354 useEffect ( ( ) => {
5455 setSortOrder ( currentSort ) ;
5556 } , [ currentSort ] ) ;
5657
5758 const handleChangeSortBy = useCallback ( ( newSortOrder : string ) => {
58- if ( ! SORT_BY_OPTIONS [ newSortOrder ] ) {
59- console . warn ( `Invalid sort option: ${ newSortOrder } ` ) ;
60- return ;
61- }
62-
6359 setSortOrder ( newSortOrder ) ;
6460 const { id, desc } = SORT_BY_OPTIONS [ newSortOrder ] ;
6561 toggleSortBy ( id , desc ) ;
@@ -71,17 +67,15 @@ const SortDropdown: FC = () => {
7167 ...option ,
7268 label : SORT_LABELS [ key ] ,
7369 } ) ) ,
70+ // eslint-disable-next-line react-hooks/exhaustive-deps
7471 [ ] ,
7572 ) ;
7673
7774 const currentSortLabel = sortOrder ? SORT_LABELS [ sortOrder ] : 'Sort' ;
7875
7976 return (
8077 < Dropdown onSelect = { handleChangeSortBy } >
81- < Dropdown . Toggle
82- variant = "outline-primary"
83- aria-label = { `Sort options. Currently sorted by: ${ currentSortLabel } ` }
84- >
78+ < Dropdown . Toggle variant = "outline-primary" >
8579 < Stack direction = "horizontal" gap = { 2 } >
8680 < Icon color = "primary" src = { SwapVert } />
8781 { currentSortLabel }
@@ -94,7 +88,6 @@ const SortDropdown: FC = () => {
9488 key = { key }
9589 active = { sortOrder === key }
9690 eventKey = { key }
97- aria-label = { `Sort by ${ label } ` }
9891 >
9992 { label }
10093 </ Dropdown . Item >
0 commit comments