-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathglobalsearch.tsx
214 lines (201 loc) · 7.53 KB
/
globalsearch.tsx
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import * as O from 'fp-ts/lib/Option';
import { pipe } from 'fp-ts/lib/function';
import { GetServerSideProps } from 'next';
import Head from 'next/head';
import Image from 'next/image.js';
import Link from 'next/link';
import { ParsedUrlQuery } from 'querystring';
import { useMemo } from 'react';
import { Col, Container, Row } from 'react-bootstrap';
import DataTable from '../components/DataTable';
import { extractQueryParam } from '../libs/api/apipage';
import { PlaceNoTreeApi, TaxonomyEntryNoParent } from '../libs/api/apitypes';
import { GlobalSearchResults, TinySource, TinySpecies, globalSearch } from '../libs/db/search.ts';
import { EntryLinked } from '../libs/pages/glossary.ts';
import { formatWithDescription } from '../libs/pages/renderhelpers';
import { TABLE_CUSTOM_STYLES } from '../libs/utils/DataTableConstants';
import { logger } from '../libs/utils/logger.ts';
import { capitalizeFirstLetter, mightFail } from '../libs/utils/util';
type SearchResultItem = {
// we need a unique ID for the data table and to keep React happy. we can not just use the ID of the
// item since we are mixing items of different type in the results and the likelihood of an ID clash
// is quite likely.
uid: string;
id: string;
type: string;
name: string;
aliases: string[];
};
type Props = {
results: SearchResultItem[];
search: string;
};
const imageForType = (i: SearchResultItem) => {
switch (i.type) {
case 'gall':
return <Image src="/images/cynipid_R.svg" alt="gallformer" aria-label="gallformer" width="45" height="45" />;
case 'plant':
return <Image src="/images/host.svg" alt="plant" aria-label="plant" width="25" height="25" />;
case 'entry':
return <Image src="/images/entry.svg" alt="glossary entry" aria-label="glossary entry" width="25" height="25" />;
case 'source':
return <Image src="/images/source.svg" alt="source" aria-label="source" width="25" height="25" />;
case 'genus':
return <Image src="/images/taxon.svg" alt="genus" aria-label="genus" width="25" height="25" />;
case 'section':
return <Image src="/images/taxon.svg" alt="section" aria-label="section" width="25" height="25" />;
case 'family':
return <Image src="/images/taxon.svg" alt="family" aria-label="family" width="25" height="25" />;
case 'place':
return <Image src="/images/place.svg" alt="place" aria-label="place" width="25" height="25" />;
default:
return <></>;
}
};
const linkItem = (i: SearchResultItem) => {
switch (i.type) {
case 'gall':
return (
<Link href={`/gall/${i.id}`}>
<em>{formatWithDescription(i.name, i.aliases, true)}</em>
</Link>
);
case 'plant':
return (
<Link href={`/host/${i.id}`}>
<em>{formatWithDescription(i.name, i.aliases, true)}</em>
</Link>
);
case 'entry':
return <Link href={`/glossary#${i.name.toLocaleLowerCase()}`}>{i.name}</Link>;
case 'source':
return <Link href={`/source/${i.id}`}>{i.name}</Link>;
case 'genus':
return (
<Link href={`/genus/${i.id}`}>
<em>{`Genus ${formatWithDescription(i.name, i.aliases, true)}`}</em>
</Link>
);
case 'section':
return (
<Link href={`/section/${i.id}`}>
<em>{`Section ${formatWithDescription(i.name, i.aliases, true)}`}</em>
</Link>
);
case 'family':
return <Link href={`/family/${i.id}`}>{`Family ${i.name}`}</Link>;
case 'place':
return <Link href={`/place/${i.id}`}>{i.name}</Link>;
default:
return <></>;
}
};
const GlobalSearch = ({ results, search }: Props): JSX.Element => {
const columns = useMemo(
() => [
{
id: 'type',
selector: (row: SearchResultItem) => row.type,
name: 'Type',
sortable: true,
center: true,
compact: true,
maxWidth: '100px',
format: imageForType,
},
{
id: 'name',
selector: (row: SearchResultItem) => row.name,
name: 'Name',
sortable: true,
wrap: true,
format: linkItem,
},
],
[],
);
return (
<Container className="pt-2" fluid>
<Head>
<title>Search Results - {`'${search}'`}</title>
<meta name="description" content="Gallformer Search Results" />
</Head>
{results.length <= 0 && (
<Row className="p-2">
<Col>
<strong>No results for {`'${search}'`}</strong>
</Col>
</Row>
)}
<Row>
<Col xs={12}>
<DataTable
keyField="uid"
data={results}
columns={columns}
striped
responsive={false}
defaultSortFieldId="pubyear"
customStyles={TABLE_CUSTOM_STYLES}
/>
</Col>
</Row>
</Container>
);
};
export const getServerSideProps: GetServerSideProps = async (context: { query: ParsedUrlQuery }) => {
const queryParam = 'searchText';
const search = pipe(
extractQueryParam(context.query, queryParam),
O.getOrElse(() => {
logger.error(`Failed to find the expected query parameter ${queryParam}.`);
return '';
}),
);
const emptySearch = (): GlobalSearchResults => ({
species: new Array<TinySpecies>(),
glossary: new Array<EntryLinked>(),
sources: new Array<TinySource>(),
taxa: new Array<TaxonomyEntryNoParent>(),
places: new Array<PlaceNoTreeApi>(),
});
const { species, glossary, sources, taxa, places } = await mightFail(emptySearch)(globalSearch(search));
// an experiment - lets mash them all together and show them in a single table
const r: SearchResultItem[] = species
.map((s) => ({ uid: `${s.id}-${s.taxoncode}`, id: s.id.toString(), type: s.taxoncode, name: s.name, aliases: s.aliases }))
.concat(
glossary.map((g) => ({
uid: `${g.id}-entry`,
id: g.word,
type: 'entry',
name: capitalizeFirstLetter(g.word),
aliases: [],
})),
)
.concat(sources.map((s) => ({ uid: `${s.id}-source`, id: s.id.toString(), type: 'source', name: s.source, aliases: [] })))
.concat(
places.map((p) => ({
uid: `${p.id}-place`,
id: p.id.toString(),
type: 'place',
name: `${p.name} - ${p.code}`,
aliases: [],
})),
)
.concat(
taxa.map((t) => ({
uid: `${t.id}-${t.type}`,
id: t.id.toString(),
type: t.type,
name: t.name,
aliases: t.description ? [t.description] : [],
})),
);
return {
props: {
results: r,
search: search,
},
};
};
export default GlobalSearch;