Skip to content

Commit 036dad2

Browse files
committed
fix/refactor useRefinements
1 parent 9cf212e commit 036dad2

File tree

3 files changed

+63
-80
lines changed

3 files changed

+63
-80
lines changed
Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { generalCourts } from "functions/src/shared"
22
import { RefinementListItem } from "instantsearch.js/es/connectors/refinement-list/connectRefinementList"
3-
import { useCallback, useMemo } from "react"
3+
import { useMemo } from "react"
44
import { useRefinements } from "../useRefinements"
55
import { useTranslation } from "next-i18next"
66

@@ -18,49 +18,35 @@ import { useTranslation } from "next-i18next"
1818
// searchablePlaceholder: "Legislative Session",
1919

2020
export const useBillRefinements = () => {
21-
const baseProps = { limit: 500, searchable: true }
2221
const { t } = useTranslation("search")
23-
const propsList = useMemo(
24-
() =>
25-
[
26-
{
27-
transformItems: useCallback(
28-
(items: RefinementListItem[]) =>
22+
23+
return useRefinements({
24+
hierarchicalMenuProps: { attributes: ["topics.lvl0", "topics.lvl1"] },
25+
refinementProps: useMemo(
26+
() =>
27+
[
28+
{
29+
transformItems: (items: RefinementListItem[]) =>
2930
items
3031
.map(i => ({
3132
...i,
3233
label: generalCourts[parseInt(i.value, 10)]?.Name ?? i.label
3334
}))
3435
.sort((a, b) => Number(b.value) - Number(a.value)),
35-
[]
36-
),
37-
attribute: "court"
38-
},
39-
{ attribute: "currentCommittee" },
40-
{ attribute: "city" },
41-
{ attribute: "primarySponsor" },
42-
{ attribute: "cosponsors" }
43-
].map(props => ({
44-
searchablePlaceholder: t(`refinements.bill.${props.attribute}`),
45-
...baseProps,
46-
...props
47-
})),
48-
[t]
49-
)
5036

51-
const hierarchicalPropsList = [
52-
{
53-
attribute: "topics.lvl0",
54-
...baseProps
55-
},
56-
{
57-
attribute: "topics.lvl1",
58-
...baseProps
59-
}
60-
]
61-
62-
return useRefinements({
63-
hierarchicalMenuProps: hierarchicalPropsList,
64-
refinementProps: propsList
37+
attribute: "court"
38+
},
39+
{ attribute: "currentCommittee" },
40+
{ attribute: "city" },
41+
{ attribute: "primarySponsor" },
42+
{ attribute: "cosponsors" }
43+
].map(props => ({
44+
limit: 500,
45+
searchable: true,
46+
searchablePlaceholder: t(`refinements.bill.${props.attribute}`),
47+
...props
48+
})),
49+
[t]
50+
)
6551
})
6652
}
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
import { RefinementListItem } from "instantsearch.js/es/connectors/refinement-list/connectRefinementList"
22
import { useRefinements } from "../useRefinements"
3-
import { useCallback, useMemo } from "react"
3+
import { useMemo } from "react"
44
import { useTranslation } from "next-i18next"
55

66
export const useTestimonyRefinements = () => {
77
const { t } = useTranslation("search")
8-
const refinementProps = useMemo(
9-
() =>
10-
[
11-
{
12-
transformItems: useCallback(
13-
(i: RefinementListItem[]) => i.filter(i => i.label !== "private"),
14-
[]
15-
),
16-
attribute: "authorDisplayName"
17-
},
18-
{ attribute: "court" },
19-
{ attribute: "position" },
20-
{ attribute: "billId" },
21-
{ attribute: "authorRole", searchable: false, hidden: true }
22-
].map(props => ({
23-
limit: 500,
24-
searchable: props.searchable ?? true,
25-
searchablePlaceholder: t(`refinements.testimony.${props.attribute}`),
26-
...props
27-
})),
28-
[t]
29-
)
308

31-
return useRefinements({ refinementProps })
9+
return useRefinements({
10+
refinementProps: useMemo(
11+
() =>
12+
[
13+
{
14+
transformItems: (i: RefinementListItem[]) =>
15+
i.filter(i => i.label !== "private"),
16+
attribute: "authorDisplayName"
17+
},
18+
{ attribute: "court" },
19+
{ attribute: "position" },
20+
{ attribute: "billId" },
21+
{ attribute: "authorRole", searchable: false, hidden: true }
22+
].map(props => ({
23+
limit: 500,
24+
searchable: true,
25+
searchablePlaceholder: t(`refinements.testimony.${props.attribute}`),
26+
...props
27+
})),
28+
[t]
29+
)
30+
})
3231
}

components/search/useRefinements.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { useTranslation } from "next-i18next"
2-
import { RefinementList, useInstantSearch } from "react-instantsearch"
2+
import {
3+
RefinementList,
4+
RefinementListProps,
5+
useInstantSearch
6+
} from "react-instantsearch"
37
import { faFilter } from "@fortawesome/free-solid-svg-icons"
48
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
59
import { useCallback, useState } from "react"
610
import styled from "styled-components"
711
import { useMediaQuery } from "usehooks-ts"
812
import { Button, Offcanvas } from "../bootstrap"
9-
import { MultiselectHierarchicalMenu } from "./HierarchicalMenuWidget"
13+
import {
14+
MultiselectHierarchicalMenu,
15+
MultiselectHierarchicalMenuParams
16+
} from "./HierarchicalMenuWidget"
1017
import { SearchContainer } from "./SearchContainer"
1118

1219
export const FilterButton = styled(Button)`
@@ -27,8 +34,8 @@ export const useRefinements = ({
2734
hierarchicalMenuProps,
2835
refinementProps
2936
}: {
30-
hierarchicalMenuProps?: any[]
31-
refinementProps: any[]
37+
hierarchicalMenuProps?: MultiselectHierarchicalMenuParams
38+
refinementProps: RefinementListProps[]
3239
}) => {
3340
const inline = useMediaQuery("(min-width: 768px)")
3441
const [show, setShow] = useState(false)
@@ -38,25 +45,16 @@ export const useRefinements = ({
3845
const refinements = (
3946
<>
4047
{refinementProps.map((p, i) => (
41-
<RefinementList className="mb-4" key={i} {...(p as any)} />
48+
<RefinementList className="mb-4" key={i} {...p} />
4249
))}
4350
</>
4451
)
4552

46-
let hierarchicalMenu = <></>
47-
48-
if (hierarchicalMenuProps) {
49-
hierarchicalMenu = (
50-
<>
51-
<MultiselectHierarchicalMenu
52-
attributes={[
53-
hierarchicalMenuProps[0].attribute,
54-
hierarchicalMenuProps[1].attribute
55-
]}
56-
/>
57-
</>
58-
)
59-
}
53+
const hierarchicalMenu = hierarchicalMenuProps ? (
54+
<MultiselectHierarchicalMenu {...hierarchicalMenuProps} />
55+
) : (
56+
<></>
57+
)
6058

6159
const hasRefinements = useHasRefinements()
6260

0 commit comments

Comments
 (0)