@@ -8,7 +8,7 @@ import SelectFacet from '@apps/search/SelectFacet';
88import TranslationContext from '@contexts/TranslationContext' ;
99import { FacetStateContext } from '@performant-software/core-data' ;
1010import clsx from 'clsx' ;
11- import { useCallback , useContext } from 'react' ;
11+ import { useCallback , useContext , useMemo } from 'react' ;
1212import _ from 'underscore' ;
1313
1414const TYPE_LIST = 'list' ;
@@ -25,6 +25,28 @@ const Facets = (props: Props) => {
2525 const { attributes, rangeAttributes } = useContext ( FacetStateContext ) ;
2626 const { t } = useContext ( TranslationContext ) ;
2727
28+ const { typesense } = config ;
29+
30+ const sortAttributes = useCallback ( ( atts : string [ ] ) => {
31+ const copy = [ ...atts ] ;
32+ if ( ! copy ) {
33+ return [ ] ;
34+ }
35+ if ( typesense ?. facets ?. include ) {
36+ return copy . sort ( ( a , b ) => {
37+ const index_a = typesense . facets . include . findIndex ( ( facet ) => facet === a ) ;
38+ const index_b = typesense . facets . include . findIndex ( ( facet ) => facet === b ) ;
39+ return index_a - index_b ;
40+ } )
41+ } else {
42+ return copy ;
43+ }
44+ } , [ typesense ] )
45+
46+ const sortedAttributes = useMemo ( ( ) => sortAttributes ( attributes ) , [ attributes , sortAttributes ] ) ;
47+
48+ const sortedRangeAttributes = useMemo ( ( ) => sortAttributes ( rangeAttributes ) , [ rangeAttributes , sortAttributes ] ) ;
49+
2850 /**
2951 * Returns the `search.facets` value for the passed attribute key populated with the passed default values.
3052 */
@@ -96,8 +118,8 @@ const Facets = (props: Props) => {
96118 </ div >
97119 < CurrentRefinementsList />
98120 < GeosearchFilter />
99- { _ . map ( rangeAttributes , ( attribute ) => renderFacet ( attribute , TYPE_RANGE ) ) }
100- { _ . map ( attributes , ( attribute ) => renderFacet ( attribute , TYPE_LIST ) ) }
121+ { _ . map ( sortedRangeAttributes , ( attribute ) => renderFacet ( attribute , TYPE_RANGE ) ) }
122+ { _ . map ( sortedAttributes , ( attribute ) => renderFacet ( attribute , TYPE_LIST ) ) }
101123 </ aside >
102124 ) ;
103125} ;
0 commit comments