-
Notifications
You must be signed in to change notification settings - Fork 551
Expand file tree
/
Copy pathconnectTrendingItems.ts
More file actions
253 lines (229 loc) · 5.99 KB
/
Copy pathconnectTrendingItems.ts
File metadata and controls
253 lines (229 loc) · 5.99 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
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
251
252
253
import {
createDocumentationMessageGenerator,
checkRendering,
noop,
escapeHits,
TAG_PLACEHOLDER,
getObjectType,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';
import type { SendEventForHits } from '../../lib/utils';
import type {
Connector,
TransformItems,
BaseHit,
Renderer,
Unmounter,
UnknownWidgetParams,
RecommendResponse,
Hit,
AlgoliaHit,
} from '../../types';
import type { PlainSearchParameters } from 'algoliasearch-helper';
const withUsage = createDocumentationMessageGenerator({
name: 'trending-items',
connector: true,
});
export type TrendingItemsRenderState<
THit extends NonNullable<object> = BaseHit
> = {
/**
* The matched recommendations from the Algolia API.
*/
items: Array<Hit<THit>>;
/**
* Sends an event to the Insights middleware.
*/
sendEvent: SendEventForHits;
};
export type TrendingItemsConnectorParams<
THit extends NonNullable<object> = BaseHit
> = (
| {
/**
* The facet attribute to get recommendations for.
*/
facetName: string;
/**
* The facet value to get recommendations for.
*/
facetValue: string;
}
| {
facetName?: string;
facetValue?: string;
}
) & {
/**
* The number of recommendations to retrieve.
*/
limit?: number;
/**
* The threshold for the recommendations confidence score (between 0 and 100).
*/
threshold?: number;
/**
* List of search parameters to send.
*/
fallbackParameters?: Omit<
PlainSearchParameters,
'page' | 'hitsPerPage' | 'offset' | 'length'
>;
/**
* List of search parameters to send.
*/
queryParameters?: Omit<
PlainSearchParameters,
'page' | 'hitsPerPage' | 'offset' | 'length'
>;
/**
* Whether to escape HTML tags from items string values.
*
* @default true
*/
escapeHTML?: boolean;
/**
* Function to transform the items passed to the templates.
*/
transformItems?: TransformItems<
Hit<THit>,
{ results: RecommendResponse<AlgoliaHit<THit>> }
>;
};
export type TrendingItemsWidgetDescription<
THit extends NonNullable<object> = BaseHit
> = {
$$type: 'ais.trendingItems';
renderState: TrendingItemsRenderState<THit>;
};
export type TrendingItemsConnector<THit extends NonNullable<object> = BaseHit> =
Connector<
TrendingItemsWidgetDescription<THit>,
TrendingItemsConnectorParams<THit>
>;
export default (function connectTrendingItems<
TWidgetParams extends UnknownWidgetParams
>(
renderFn: Renderer<
TrendingItemsRenderState,
TWidgetParams & TrendingItemsConnectorParams
>,
unmountFn: Unmounter = noop
) {
checkRendering(renderFn, withUsage());
return <THit extends NonNullable<object> = BaseHit>(
widgetParams: TWidgetParams & TrendingItemsConnectorParams<THit>
) => {
const {
facetName,
facetValue,
limit,
threshold,
fallbackParameters,
queryParameters,
// @MAJOR: this can default to false
escapeHTML = true,
transformItems = ((items) => items) as NonNullable<
TrendingItemsConnectorParams<THit>['transformItems']
>,
} = widgetParams || {};
if ((facetName && !facetValue) || (!facetName && facetValue)) {
throw new Error(
withUsage(
`When you provide facetName (received type ${getObjectType(
facetName
)}), you must also provide facetValue (received type ${getObjectType(
facetValue
)}).`
)
);
}
let sendEvent: SendEventForHits;
return {
dependsOn: 'recommend',
$$type: 'ais.trendingItems',
init(initOptions) {
renderFn(
{
...this.getWidgetRenderState(initOptions),
instantSearchInstance: initOptions.instantSearchInstance,
},
true
);
},
render(renderOptions) {
const renderState = this.getWidgetRenderState(renderOptions);
renderFn(
{
...renderState,
instantSearchInstance: renderOptions.instantSearchInstance,
},
false
);
},
getRenderState(renderState) {
return renderState;
},
getWidgetRenderState({ results, helper, instantSearchInstance }) {
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
helper,
widgetType: this.$$type,
});
}
if (results === null || results === undefined) {
return { items: [], widgetParams, sendEvent };
}
if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}
const itemsWithAbsolutePosition = addAbsolutePosition(
results.hits,
0,
1
);
const itemsWithAbsolutePositionAndQueryID = addQueryID(
itemsWithAbsolutePosition,
results.queryID
);
const transformedItems = transformItems(
itemsWithAbsolutePositionAndQueryID,
{
results: results as RecommendResponse<AlgoliaHit<THit>>,
}
);
return {
items: transformedItems,
widgetParams,
sendEvent,
};
},
dispose({ recommendState }) {
unmountFn();
return recommendState.removeParams(this.$$id!);
},
getWidgetParameters(state) {
return state.removeParams(this.$$id!).addTrendingItems({
facetName: facetName as string,
facetValue: facetValue as string,
maxRecommendations: limit,
threshold,
fallbackParameters: fallbackParameters
? {
...fallbackParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
}
: undefined,
queryParameters: {
...queryParameters,
...(escapeHTML ? TAG_PLACEHOLDER : {}),
},
$$id: this.$$id!,
});
},
};
};
} satisfies TrendingItemsConnector);