-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathTestimonySearch.tsx
More file actions
183 lines (171 loc) · 5.21 KB
/
TestimonySearch.tsx
File metadata and controls
183 lines (171 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
CurrentRefinements,
Hits,
InstantSearch,
Pagination,
SearchBox,
useInstantSearch
} from "react-instantsearch"
import {
StyledTabContent,
StyledTabNav
} from "components/EditProfilePage/StyledEditProfileComponents"
import { currentGeneralCourt } from "functions/src/shared"
import { SortByItem } from "instantsearch.js/es/connectors/sort-by/connectSortBy"
import { useState } from "react"
import { TabContainer, TabPane } from "react-bootstrap"
import styled from "styled-components"
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter"
import { Col, Nav, Row } from "../../bootstrap"
import { NoResults } from "../NoResults"
import { ResultCount } from "../ResultCount"
import { SearchContainer } from "../SearchContainer"
import { SearchErrorBoundary } from "../SearchErrorBoundary"
import { SortBy } from "../SortBy"
import { getServerConfig, VirtualFilters } from "../common"
import { useRouting } from "../useRouting"
import { TestimonyHit } from "./TestimonyHit"
import { useTestimonyRefinements } from "./useTestimonyRefinements"
import { FollowContext, OrgFollowStatus } from "components/shared/FollowContext"
const searchClient = new TypesenseInstantSearchAdapter({
server: getServerConfig(),
additionalSearchParameters: {
query_by: "billId,content,authorDisplayName,authorRole",
exclude_fields: ""
}
}).searchClient
const items: SortByItem[] = [
{
label: "Sort by New -> Old",
value: "publishedTestimony/sort/publishedAt:desc"
},
{
label: "Sort by Old -> New",
value: "publishedTestimony/sort/publishedAt:asc"
},
{
label: "Sort by Relevance",
value: "publishedTestimony/sort/_text_match:desc,publishedAt:desc"
}
]
export const initialSortByValue = items[0].value
export const TestimonySearch = () => (
<SearchErrorBoundary>
<InstantSearch
indexName={initialSortByValue}
initialUiState={{
[initialSortByValue]: {
refinementList: { court: [String(currentGeneralCourt)] }
}
}}
searchClient={searchClient}
routing={useRouting()}
future={{ preserveSharedStateOnUnmount: true }}
>
<VirtualFilters type="testimony" />
<Layout />
</InstantSearch>
</SearchErrorBoundary>
)
const RefinementRow = styled.div`
display: inline-flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 0.5rem;
`
const useSearchStatus = () => {
const { results } = useInstantSearch()
if (!results.query) {
return "loading"
} else if (results.nbHits === 0) {
return "empty"
} else {
return "results"
}
}
const tabs = ["All", "Individuals", "Organizations"]
type Tab = (typeof tabs)[number]
const Layout = () => {
const [key, setKey] = useState<string>("All")
const refinements = useTestimonyRefinements()
const status = useSearchStatus()
const { indexUiState, setIndexUiState } = useInstantSearch()
const onTabClick = (t: Tab) => {
setKey(t)
setIndexUiState(prevState => {
const validRoles = ["user", "organization", "admin"]
const role =
t === "Individuals"
? ["user"]
: t === "Organizations"
? ["organization"]
: validRoles
return {
...prevState,
refinementList: {
...prevState.refinementList,
authorRole: role
}
}
})
}
const [followStatus, setFollowStatus] = useState<OrgFollowStatus>({})
return (
<>
<FollowContext.Provider value={{ followStatus, setFollowStatus }}>
<TabContainer activeKey={key} onSelect={(k: any) => setKey(k)}>
<StyledTabNav>
{tabs.map((t, i) => (
<Nav.Item key={t}>
<Nav.Link
eventKey={t}
className={`rounded-top m-0 p-0`}
onClick={e => onTabClick(t)}
>
<p className={`my-0 ${i == 0 ? "" : "mx-4"}`}>{t}</p>
<hr className={`my-0`} />
</Nav.Link>
</Nav.Item>
))}
</StyledTabNav>
<StyledTabContent></StyledTabContent>
</TabContainer>
<SearchContainer>
<Row>
<SearchBox
placeholder="Search For Testimony"
className="mt-2 mb-3"
/>
</Row>
<Row>
<Col xs={0} lg={3}>
{refinements.options}
</Col>
<Col className="d-flex flex-column">
<RefinementRow>
<ResultCount className="flex-grow-1 m-1" />
<SortBy items={items} />
{refinements.show}
</RefinementRow>
<CurrentRefinements
className="mt-2 mb-2"
excludedAttributes={["authorRole"]}
/>
{status === "empty" ? (
<NoResults>
Your search has yielded zero results!
<br />
<b>Try another search term</b>
</NoResults>
) : (
<Hits hitComponent={TestimonyHit} />
)}
<Pagination className="mx-auto mt-2 mb-3" />
</Col>
</Row>
</SearchContainer>
</FollowContext.Provider>
</>
)
}