-
Notifications
You must be signed in to change notification settings - Fork 498
/
connectInfiniteHits.ts
204 lines (177 loc) · 5.73 KB
/
connectInfiniteHits.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
import escapeHits, { TAG_PLACEHOLDER } from '../../lib/escape-highlight';
import {
Renderer,
RenderOptions,
WidgetFactory,
Hits,
Unmounter,
Helper,
SearchParameters,
} from '../../types';
import {
checkRendering,
createDocumentationMessageGenerator,
isEqual,
addAbsolutePosition,
addQueryID,
noop,
} from '../../lib/utils';
import { InfiniteHitsRendererWidgetParams } from '../../widgets/infinite-hits/infinite-hits';
export type InfiniteHitsConnectorParams = Partial<
InfiniteHitsRendererWidgetParams
>;
export interface InfiniteHitsRenderOptions<TInfiniteHitsWidgetParams>
extends RenderOptions<TInfiniteHitsWidgetParams> {
showPrevious: () => void;
showMore: () => void;
isFirstPage: boolean;
isLastPage: boolean;
}
export type InfiniteHitsRenderer<TInfiniteHitsWidgetParams> = Renderer<
InfiniteHitsRenderOptions<
InfiniteHitsConnectorParams & TInfiniteHitsWidgetParams
>
>;
export type InfiniteHitsWidgetFactory<
TInfiniteHitsWidgetParams
> = WidgetFactory<InfiniteHitsConnectorParams & TInfiniteHitsWidgetParams>;
export type InfiniteHitsConnector = <TInfiniteHitsWidgetParams>(
render: InfiniteHitsRenderer<TInfiniteHitsWidgetParams>,
unmount?: Unmounter
) => InfiniteHitsWidgetFactory<TInfiniteHitsWidgetParams>;
const withUsage = createDocumentationMessageGenerator({
name: 'infinite-hits',
connector: true,
});
const connectInfiniteHits: InfiniteHitsConnector = (
renderFn,
unmountFn = noop
) => {
checkRendering(renderFn, withUsage());
return widgetParams => {
const {
escapeHTML = true,
transformItems = (items: any[]) => items,
showPrevious: hasShowPrevious = false,
} = widgetParams || {};
let hitsCache: Hits = [];
let firstReceivedPage = Infinity;
let lastReceivedPage = -1;
let prevState: Partial<SearchParameters>;
let showPrevious: () => void;
let showMore: () => void;
const getShowPrevious = (helper: Helper): (() => void) => () => {
// Using the helper's `overrideStateWithoutTriggeringChangeEvent` method
// avoid updating the browser URL when the user displays the previous page.
helper
.overrideStateWithoutTriggeringChangeEvent({
...helper.state,
page: firstReceivedPage - 1,
})
.search();
};
const getShowMore = (helper: Helper): (() => void) => () => {
helper.setPage(lastReceivedPage + 1).search();
};
return {
getConfiguration() {
return escapeHTML ? TAG_PLACEHOLDER : {};
},
init({ instantSearchInstance, helper }) {
showPrevious = getShowPrevious(helper);
showMore = getShowMore(helper);
firstReceivedPage = helper.state.page!;
lastReceivedPage = helper.state.page!;
renderFn(
{
hits: hitsCache,
results: undefined,
showPrevious,
showMore,
isFirstPage: firstReceivedPage === 0,
isLastPage: true,
instantSearchInstance,
widgetParams,
},
true
);
},
render({ results, state, instantSearchInstance }) {
// Reset cache and received pages if anything changes in the
// search state, except for the page.
//
// We're doing this to "reset" the widget if a refinement or the
// query changes between renders, but we want to keep it as is
// if we only change pages.
const { page, ...currentState } = state;
if (!isEqual(currentState, prevState)) {
hitsCache = [];
firstReceivedPage = page!;
lastReceivedPage = page!;
prevState = currentState;
}
if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}
const initialEscaped = (results.hits as any).__escaped;
results.hits = addAbsolutePosition(
results.hits,
results.page,
results.hitsPerPage
);
results.hits = addQueryID(results.hits, results.queryID);
results.hits = transformItems(results.hits);
// Make sure the escaped tag stays after mapping over the hits.
// This prevents the hits from being double-escaped if there are multiple
// hits widgets mounted on the page.
(results.hits as any).__escaped = initialEscaped;
if (lastReceivedPage < page! || !hitsCache.length) {
hitsCache = [...hitsCache, ...results.hits];
lastReceivedPage = page!;
} else if (firstReceivedPage > page!) {
hitsCache = [...results.hits, ...hitsCache];
firstReceivedPage = page!;
}
const isFirstPage = firstReceivedPage === 0;
const isLastPage = results.nbPages <= results.page + 1;
renderFn(
{
hits: hitsCache,
results,
showPrevious,
showMore,
isFirstPage,
isLastPage,
instantSearchInstance,
widgetParams,
},
false
);
},
dispose() {
unmountFn();
},
getWidgetState(uiState, { searchParameters }) {
const page = searchParameters.page!;
if (!hasShowPrevious || page === 0 || page + 1 === uiState.page) {
return uiState;
}
return {
...uiState,
page: page + 1,
};
},
getWidgetSearchParameters(searchParameters, { uiState }) {
if (!hasShowPrevious) {
return searchParameters;
}
const uiPage = uiState.page;
if (uiPage) {
return searchParameters.setQueryParameter('page', uiPage - 1);
}
return searchParameters.setQueryParameter('page', 0);
},
};
};
};
export default connectInfiniteHits;