Skip to content

Commit 7d1bbe3

Browse files
authored
Merge pull request #296 from performant-software/RB-facet-ordering
Ordering facets based on `include` list
2 parents 888e6e1 + 6ee260e commit 7d1bbe3

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/apps/search/Facets.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import SelectFacet from '@apps/search/SelectFacet';
88
import TranslationContext from '@contexts/TranslationContext';
99
import { FacetStateContext } from '@performant-software/core-data';
1010
import clsx from 'clsx';
11-
import { useCallback, useContext } from 'react';
11+
import { useCallback, useContext, useMemo } from 'react';
1212
import _ from 'underscore';
1313

1414
const 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

Comments
 (0)