-
Notifications
You must be signed in to change notification settings - Fork 498
/
connectAutocomplete.ts
250 lines (208 loc) · 5.68 KB
/
connectAutocomplete.ts
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import type { SearchResults } from 'algoliasearch-helper';
import type { SendEventForHits } from '../../lib/utils';
import {
escapeHits,
TAG_PLACEHOLDER,
checkRendering,
createDocumentationMessageGenerator,
createSendEventForHits,
noop,
warning,
} from '../../lib/utils';
import type { Hits, Connector, WidgetRenderState } from '../../types';
const withUsage = createDocumentationMessageGenerator({
name: 'autocomplete',
connector: true,
});
export type AutocompleteConnectorParams = {
/**
* Escapes HTML entities from hits string values.
*
* @default `true`
*/
escapeHTML?: boolean;
};
export type AutocompleteRenderState = {
/**
* The current value of the query.
*/
currentRefinement: string;
/**
* The indices this widget has access to.
*/
indices: Array<{
/**
* The name of the index
*/
indexName: string;
/**
* The resolved hits from the index matching the query.
*/
hits: Hits;
/**
* The full results object from the Algolia API.
*/
results: SearchResults;
/**
* Send event to insights middleware
*/
sendEvent: SendEventForHits;
}>;
/**
* Searches into the indices with the provided query.
*/
refine: (query: string) => void;
};
export type AutocompleteWidgetDescription = {
$$type: 'ais.autocomplete';
renderState: AutocompleteRenderState;
indexRenderState: {
autocomplete: WidgetRenderState<
AutocompleteRenderState,
AutocompleteConnectorParams
>;
};
indexUiState: { query: string };
};
export type AutocompleteConnector = Connector<
AutocompleteWidgetDescription,
AutocompleteConnectorParams
>;
const connectAutocomplete: AutocompleteConnector = function connectAutocomplete(
renderFn,
unmountFn = noop
) {
checkRendering(renderFn, withUsage());
return (widgetParams) => {
const { escapeHTML = true } = widgetParams || {};
warning(
!(widgetParams as any).indices,
`
The option \`indices\` has been removed from the Autocomplete connector.
The indices to target are now inferred from the widgets tree.
${
Array.isArray((widgetParams as any).indices)
? `
An alternative would be:
const autocomplete = connectAutocomplete(renderer);
search.addWidgets([
${(widgetParams as any).indices
.map(({ value }: { value: string }) => `index({ indexName: '${value}' }),`)
.join('\n ')}
autocomplete()
]);
`
: ''
}
`
);
type ConnectorState = {
refine?(query: string): void;
};
const connectorState: ConnectorState = {};
return {
$$type: 'ais.autocomplete',
init(initOptions) {
const { instantSearchInstance } = initOptions;
renderFn(
{
...this.getWidgetRenderState(initOptions),
instantSearchInstance,
},
true
);
},
render(renderOptions) {
const { instantSearchInstance } = renderOptions;
const renderState = this.getWidgetRenderState(renderOptions);
renderState.indices.forEach(({ sendEvent, hits }) => {
sendEvent('view', hits);
});
renderFn(
{
...renderState,
instantSearchInstance,
},
false
);
},
getRenderState(renderState, renderOptions) {
return {
...renderState,
autocomplete: this.getWidgetRenderState(renderOptions),
};
},
getWidgetRenderState({ helper, scopedResults, instantSearchInstance }) {
if (!connectorState.refine) {
connectorState.refine = (query: string) => {
helper.setQuery(query).search();
};
}
const indices = scopedResults.map((scopedResult) => {
// We need to escape the hits because highlighting
// exposes HTML tags to the end-user.
scopedResult.results.hits = escapeHTML
? escapeHits(scopedResult.results.hits)
: scopedResult.results.hits;
const sendEvent = createSendEventForHits({
instantSearchInstance,
index: scopedResult.results.index,
widgetType: this.$$type,
});
return {
indexId: scopedResult.indexId,
indexName: scopedResult.results.index,
hits: scopedResult.results.hits,
results: scopedResult.results,
sendEvent,
};
});
return {
currentRefinement: helper.state.query || '',
indices,
refine: connectorState.refine,
widgetParams,
};
},
getWidgetUiState(uiState, { searchParameters }) {
const query = searchParameters.query || '';
if (query === '' || (uiState && uiState.query === query)) {
return uiState;
}
return {
...uiState,
query,
};
},
getWidgetSearchParameters(searchParameters, { uiState }) {
const parameters = {
query: uiState.query || '',
};
if (!escapeHTML) {
return searchParameters.setQueryParameters(parameters);
}
return searchParameters.setQueryParameters({
...parameters,
...TAG_PLACEHOLDER,
});
},
dispose({ state }) {
unmountFn();
const stateWithoutQuery = state.setQueryParameter('query', undefined);
if (!escapeHTML) {
return stateWithoutQuery;
}
return stateWithoutQuery.setQueryParameters(
Object.keys(TAG_PLACEHOLDER).reduce(
(acc, key) => ({
...acc,
[key]: undefined,
}),
{}
)
);
},
};
};
};
export default connectAutocomplete;