|
| 1 | +import { |
| 2 | + checkRendering, |
| 3 | + createDocumentationMessageGenerator, |
| 4 | + createConcurrentSafePromise, |
| 5 | + addQueryID, |
| 6 | + debounce, |
| 7 | + addAbsolutePosition, |
| 8 | + noop, |
| 9 | + escapeHits, |
| 10 | +} from '../../lib/utils'; |
| 11 | +import { |
| 12 | + Connector, |
| 13 | + Hits, |
| 14 | + Hit, |
| 15 | + FindAnswersOptions, |
| 16 | + FindAnswersResponse, |
| 17 | +} from '../../types'; |
| 18 | + |
| 19 | +type IndexWithAnswers = { |
| 20 | + readonly findAnswers: any; |
| 21 | +}; |
| 22 | + |
| 23 | +function hasFindAnswersMethod( |
| 24 | + answersIndex: IndexWithAnswers | any |
| 25 | +): answersIndex is IndexWithAnswers { |
| 26 | + return typeof (answersIndex as IndexWithAnswers).findAnswers === 'function'; |
| 27 | +} |
| 28 | + |
| 29 | +const withUsage = createDocumentationMessageGenerator({ |
| 30 | + name: 'answers', |
| 31 | + connector: true, |
| 32 | +}); |
| 33 | + |
| 34 | +export type AnswersRendererOptions = { |
| 35 | + /** |
| 36 | + * The matched hits from Algolia API. |
| 37 | + */ |
| 38 | + hits: Hits; |
| 39 | + |
| 40 | + /** |
| 41 | + * Whether it's still loading the results from the Answers API. |
| 42 | + */ |
| 43 | + isLoading: boolean; |
| 44 | +}; |
| 45 | + |
| 46 | +export type AnswersConnectorParams = { |
| 47 | + /** |
| 48 | + * Attributes to use for predictions. |
| 49 | + * If empty, we use all `searchableAttributes` to find answers. |
| 50 | + * All your `attributesForPrediction` must be part of your `searchableAttributes`. |
| 51 | + */ |
| 52 | + attributesForPrediction?: string[]; |
| 53 | + |
| 54 | + /** |
| 55 | + * The languages in the query. Currently only supports `en`. |
| 56 | + */ |
| 57 | + queryLanguages: ['en']; |
| 58 | + |
| 59 | + /** |
| 60 | + * Maximum number of answers to retrieve from the Answers Engine. |
| 61 | + * Cannot be greater than 1000. |
| 62 | + * @default 1 |
| 63 | + */ |
| 64 | + nbHits?: number; |
| 65 | + |
| 66 | + /** |
| 67 | + * Debounce time in milliseconds to debounce render |
| 68 | + * @default 100 |
| 69 | + */ |
| 70 | + renderDebounceTime?: number; |
| 71 | + |
| 72 | + /** |
| 73 | + * Debounce time in milliseconds to debounce search |
| 74 | + * @default 100 |
| 75 | + */ |
| 76 | + searchDebounceTime?: number; |
| 77 | + |
| 78 | + /** |
| 79 | + * Whether to escape HTML tags from hits string values. |
| 80 | + * |
| 81 | + * @default true |
| 82 | + */ |
| 83 | + escapeHTML?: boolean; |
| 84 | + |
| 85 | + /** |
| 86 | + * Extra parameters to pass to findAnswers method. |
| 87 | + * @default {} |
| 88 | + */ |
| 89 | + extraParameters?: FindAnswersOptions; |
| 90 | +}; |
| 91 | + |
| 92 | +export type AnswersConnector = Connector< |
| 93 | + AnswersRendererOptions, |
| 94 | + AnswersConnectorParams |
| 95 | +>; |
| 96 | + |
| 97 | +const connectAnswers: AnswersConnector = function connectAnswers( |
| 98 | + renderFn, |
| 99 | + unmountFn = noop |
| 100 | +) { |
| 101 | + checkRendering(renderFn, withUsage()); |
| 102 | + |
| 103 | + return widgetParams => { |
| 104 | + const { |
| 105 | + queryLanguages, |
| 106 | + attributesForPrediction, |
| 107 | + nbHits = 1, |
| 108 | + renderDebounceTime = 100, |
| 109 | + searchDebounceTime = 100, |
| 110 | + escapeHTML = true, |
| 111 | + extraParameters = {}, |
| 112 | + } = widgetParams || ({} as typeof widgetParams); |
| 113 | + |
| 114 | + // @ts-ignore checking for the wrong value |
| 115 | + if (!queryLanguages || queryLanguages.length === 0) { |
| 116 | + throw new Error( |
| 117 | + withUsage('The `queryLanguages` expects an array of strings.') |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + const runConcurrentSafePromise = createConcurrentSafePromise< |
| 122 | + FindAnswersResponse<Hit> |
| 123 | + >(); |
| 124 | + |
| 125 | + let lastResult: Partial<FindAnswersResponse<Hit>>; |
| 126 | + let isLoading = false; |
| 127 | + const debouncedRender = debounce(renderFn, renderDebounceTime); |
| 128 | + let debouncedRefine; |
| 129 | + |
| 130 | + return { |
| 131 | + $$type: 'ais.answers', |
| 132 | + |
| 133 | + init(initOptions) { |
| 134 | + const { state, instantSearchInstance } = initOptions; |
| 135 | + const answersIndex = instantSearchInstance.client!.initIndex!( |
| 136 | + state.index |
| 137 | + ); |
| 138 | + if (!hasFindAnswersMethod(answersIndex)) { |
| 139 | + throw new Error(withUsage('`algoliasearch` >= 4.8.0 required.')); |
| 140 | + } |
| 141 | + debouncedRefine = debounce( |
| 142 | + answersIndex.findAnswers, |
| 143 | + searchDebounceTime |
| 144 | + ); |
| 145 | + |
| 146 | + renderFn( |
| 147 | + { |
| 148 | + ...this.getWidgetRenderState(initOptions), |
| 149 | + instantSearchInstance: initOptions.instantSearchInstance, |
| 150 | + }, |
| 151 | + true |
| 152 | + ); |
| 153 | + }, |
| 154 | + |
| 155 | + render(renderOptions) { |
| 156 | + const query = renderOptions.state.query; |
| 157 | + if (!query) { |
| 158 | + // renders nothing with empty query |
| 159 | + lastResult = {}; |
| 160 | + isLoading = false; |
| 161 | + renderFn( |
| 162 | + { |
| 163 | + ...this.getWidgetRenderState(renderOptions), |
| 164 | + instantSearchInstance: renderOptions.instantSearchInstance, |
| 165 | + }, |
| 166 | + false |
| 167 | + ); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + // render the loader |
| 172 | + lastResult = {}; |
| 173 | + isLoading = true; |
| 174 | + renderFn( |
| 175 | + { |
| 176 | + ...this.getWidgetRenderState(renderOptions), |
| 177 | + instantSearchInstance: renderOptions.instantSearchInstance, |
| 178 | + }, |
| 179 | + false |
| 180 | + ); |
| 181 | + |
| 182 | + // call /answers API |
| 183 | + runConcurrentSafePromise( |
| 184 | + debouncedRefine(query, queryLanguages, { |
| 185 | + ...extraParameters, |
| 186 | + nbHits, |
| 187 | + attributesForPrediction, |
| 188 | + }) |
| 189 | + ).then(results => { |
| 190 | + if (!results) { |
| 191 | + // It's undefined when it's debounced. |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + if (escapeHTML && results.hits.length > 0) { |
| 196 | + results.hits = escapeHits(results.hits); |
| 197 | + } |
| 198 | + const initialEscaped = (results.hits as ReturnType<typeof escapeHits>) |
| 199 | + .__escaped; |
| 200 | + |
| 201 | + results.hits = addAbsolutePosition<typeof results.hits[0]>( |
| 202 | + results.hits, |
| 203 | + 0, |
| 204 | + nbHits |
| 205 | + ); |
| 206 | + |
| 207 | + results.hits = addQueryID<typeof results.hits[0]>( |
| 208 | + results.hits, |
| 209 | + results.queryID |
| 210 | + ); |
| 211 | + |
| 212 | + // Make sure the escaped tag stays, even after mapping over the hits. |
| 213 | + // This prevents the hits from being double-escaped if there are multiple |
| 214 | + // hits widgets mounted on the page. |
| 215 | + (results.hits as ReturnType< |
| 216 | + typeof escapeHits |
| 217 | + >).__escaped = initialEscaped; |
| 218 | + |
| 219 | + lastResult = results; |
| 220 | + isLoading = false; |
| 221 | + debouncedRender( |
| 222 | + { |
| 223 | + ...this.getWidgetRenderState(renderOptions), |
| 224 | + instantSearchInstance: renderOptions.instantSearchInstance, |
| 225 | + }, |
| 226 | + false |
| 227 | + ); |
| 228 | + }); |
| 229 | + }, |
| 230 | + |
| 231 | + getRenderState(renderState, renderOptions) { |
| 232 | + return { |
| 233 | + ...renderState, |
| 234 | + answers: this.getWidgetRenderState(renderOptions), |
| 235 | + }; |
| 236 | + }, |
| 237 | + |
| 238 | + getWidgetRenderState() { |
| 239 | + return { |
| 240 | + hits: lastResult?.hits || [], |
| 241 | + isLoading, |
| 242 | + widgetParams, |
| 243 | + }; |
| 244 | + }, |
| 245 | + |
| 246 | + dispose({ state }) { |
| 247 | + unmountFn(); |
| 248 | + return state; |
| 249 | + }, |
| 250 | + |
| 251 | + getWidgetSearchParameters(state) { |
| 252 | + return state; |
| 253 | + }, |
| 254 | + }; |
| 255 | + }; |
| 256 | +}; |
| 257 | + |
| 258 | +export default connectAnswers; |
0 commit comments