-
Notifications
You must be signed in to change notification settings - Fork 14
/
FeatureSearchBox.tsx
192 lines (169 loc) · 5.34 KB
/
FeatureSearchBox.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
import React, { Component, ComponentClass } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
Segment,
Search,
SearchProps,
SearchResultsProps,
StrictSearchCategoryProps,
Select,
SemanticCOLORS,
} from 'semantic-ui-react';
import * as R from 'ramda';
import { RootState } from '../../redux/reducers';
import { Features, LegacyAPI } from '../../api';
import {
FeatureSearch,
FeatureFilter,
FeatureSearchSelection,
featuresToResults,
findResult,
init,
orderCategories,
} from './model';
import * as Action from './actions';
import * as Selector from './selectors';
import { NAME } from './constants';
import './FeatureSearchBox.css';
// TODO: Remove this when removing the LegacyAPI code
declare const __TEST_ONLY__: boolean;
type FeatureSearchBoxProps = {
/** A unique identifier for search data provided by this search box. */
field: string;
/** The feature type to search for. May be "all". */
filter: FeatureFilter;
/** Background colour */
colour: string;
};
type FeatureSearchBoxState = {
/** A search result has been selected */
selected: FeatureSearchSelection | undefined;
/** Show a loading indication */
loading: boolean;
/** Text to display in the input */
value: string;
/** Search results */
results: readonly Features[];
};
/**
* A text search input for items in a dataset.
*/
export const FeatureSearchBox = (props: FeatureSearchBoxProps) => {
const legacyFeatureIndex = props.field.slice(-1); //TODO: This is a horrible hack
const dispatch = useDispatch();
const state: FeatureSearchBoxState = useSelector<
RootState,
FeatureSearchBoxState
>((root: RootState) => {
return {
selected: Selector.selected(props.field, root),
loading: Selector.isLoading(props.field, root),
value: Selector.value(props.field, root),
results: Selector.searchResults(props.field, root),
};
});
const onSearchChange = (query: string | undefined) => {
if (query === undefined || query === '') {
LegacyAPI.setActiveFeature(
legacyFeatureIndex,
props.filter,
'',
'',
0,
null
);
dispatch(Action.clear(props.field));
} else {
dispatch(
Action.search(
props.field,
LegacyAPI.getActiveLoom(),
props.filter,
query
)
);
}
};
const onResultSelect = (_, { result }) => {
const selection = findResult(result, props.colour, state.results);
if (selection) {
if (!__TEST_ONLY__) {
LegacyAPI.updateFeature(
legacyFeatureIndex,
props.filter,
selection.title,
selection.category,
selection.description
);
}
dispatch(Action.select(props.field, selection));
}
};
/**
* Input element for the Search box has an "action" button
* which either has a 'search' icon, or,
* when a search result is selected, a clickable cross icon.
* Clicking the cross should clear the search field.
*/
const input = () => {
return {
action: {
...(state.selected && {
onClick: () => onSearchChange(''),
}),
icon: state.selected ? 'cancel' : 'search',
},
actionPosition: 'left',
icon: false,
};
};
//TODO: A hack to ensure the old state managment knows something is selected
// when the page changes.
if (state.selected && !__TEST_ONLY__) {
let legacyFeature = LegacyAPI.getActiveFeatures()[legacyFeatureIndex];
if (legacyFeature !== undefined) {
legacyFeature = R.pick(
['type', 'featureType', 'feature', 'threshold'],
legacyFeature
);
}
const currentFeature = {
type: props.filter,
featureType: state.selected.category,
feature: state.selected.title,
threshold: 0,
};
if (!R.equals(currentFeature, legacyFeature)) {
LegacyAPI.updateFeature(
legacyFeatureIndex,
props.filter,
state.selected.title,
state.selected.category,
state.selected.description
);
}
}
const displayResults = R.fromPairs(
R.sortWith([R.comparator(orderCategories)], state.results).map(
featuresToResults
)
);
return (
<Segment
color={props.colour as SemanticCOLORS}
inverted
className='noPadding'>
<Search
category
className='feature-search-input'
input={input()}
loading={state.loading}
placeholder='Search...'
onSearchChange={(_, data) => onSearchChange(data.value)}
onResultSelect={onResultSelect}
results={displayResults}
value={state.value}
/>
</Segment>
);
};