Skip to content

Commit e543e7b

Browse files
authored
Merge pull request #267 from performant-software/RB-inverse-facet-labels
Added handling for inverse facet labels
2 parents f15e926 + b0f573f commit e543e7b

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/apps/search/Facet.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
import TranslationContext from '@contexts/TranslationContext';
2-
import { Icon } from '@performant-software/core-data';
2+
import { Icon, useCachedHits } from '@performant-software/core-data';
33
import { getFacetLabel } from '@utils/search';
44
import clsx from 'clsx';
55
import { useContext, useMemo, type ReactNode } from 'react';
6+
import _ from 'underscore';
67

78
interface Props {
89
attribute: string;
910
children: ReactNode,
1011
className?: string;
1112
icon?: string;
13+
inverse?: boolean;
1214
}
1315

1416
const Facet = ({ attribute, children, className, icon }: Props) => {
1517
const { t } = useContext(TranslationContext);
1618

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+
1736
/**
1837
* Memo-izes the label for the current facet.
1938
*/
20-
const label = useMemo(() => getFacetLabel(attribute, t), [attribute, t]);
39+
const label = useMemo(() => getFacetLabel(attribute, t, inverse), [attribute, t, inverse]);
2140

2241
return (
2342
<div

src/utils/search.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,21 @@ export const getAttributes = (config) => config.result_card.attributes?.slice(0,
4646
*
4747
* @param attribute
4848
* @param t
49+
* @param inverse
4950
*/
50-
export const getFacetLabel = (attribute, t) => {
51+
export const getFacetLabel = (attribute, t, inverse = false, inverseSuffix = '_inverse') => {
5152
let value;
5253

5354
// exclude these from facet labels, e.g. 'Organizations' rather than 'Organizations: Name'
5455
const DEFAULT_FIELDIDS = ["name", "names"]
5556

56-
const relationshipId = TypesenseUtils.getRelationshipId(attribute);
57+
let relationshipId = TypesenseUtils.getRelationshipId(attribute);
5758
const fieldId = TypesenseUtils.getFieldId(attribute);
5859

60+
if (inverse) {
61+
relationshipId = relationshipId + inverseSuffix;
62+
}
63+
5964
if (relationshipId && fieldId && !DEFAULT_FIELDIDS.includes(fieldId)) {
6065
value = t('facetLabel', { relationship: t(relationshipId), field: t(fieldId) })
6166
} else if (relationshipId) {

0 commit comments

Comments
 (0)