|
1 | 1 | import TranslationContext from '@contexts/TranslationContext'; |
2 | | -import { Icon } from '@performant-software/core-data'; |
| 2 | +import { Icon, useCachedHits } from '@performant-software/core-data'; |
3 | 3 | import { getFacetLabel } from '@utils/search'; |
4 | 4 | import clsx from 'clsx'; |
5 | 5 | import { useContext, useMemo, type ReactNode } from 'react'; |
| 6 | +import _ from 'underscore'; |
6 | 7 |
|
7 | 8 | interface Props { |
8 | 9 | attribute: string; |
9 | 10 | children: ReactNode, |
10 | 11 | className?: string; |
11 | 12 | icon?: string; |
| 13 | + inverse?: boolean; |
12 | 14 | } |
13 | 15 |
|
14 | 16 | const Facet = ({ attribute, children, className, icon }: Props) => { |
15 | 17 | const { t } = useContext(TranslationContext); |
16 | 18 |
|
| 19 | + //this is very janky but we need some way to determine whether to use the inverse label or not, |
| 20 | + //and the easiest place to access that information seems to be by looking at the value of |
| 21 | + //the attribute in a hit where it's defined |
| 22 | + |
| 23 | + const hits = useCachedHits(); |
| 24 | + |
| 25 | + const attribute_base = useMemo(() => attribute.split('.')[0], [attribute]); |
| 26 | + |
| 27 | + const sample_hit = useMemo(() => _.find(hits, (hit) => |
| 28 | + (hit && |
| 29 | + hit[attribute_base] && |
| 30 | + Array.isArray(hit[attribute_base]) && |
| 31 | + typeof hit[attribute_base][0] === 'object') |
| 32 | + ), [hits, attribute_base]); |
| 33 | + |
| 34 | + const inverse = useMemo(() => sample_hit && sample_hit[attribute_base][0]["inverse"], [sample_hit]); |
| 35 | + |
17 | 36 | /** |
18 | 37 | * Memo-izes the label for the current facet. |
19 | 38 | */ |
20 | | - const label = useMemo(() => getFacetLabel(attribute, t), [attribute, t]); |
| 39 | + const label = useMemo(() => getFacetLabel(attribute, t, inverse), [attribute, t, inverse]); |
21 | 40 |
|
22 | 41 | return ( |
23 | 42 | <div |
|
0 commit comments