|
| 1 | +import { SearchResults } from 'algoliasearch-helper'; |
| 2 | +import { |
| 3 | + checkRendering, |
| 4 | + createDocumentationMessageGenerator, |
| 5 | + getWidgetAttribute, |
| 6 | + noop, |
| 7 | +} from '../../lib/utils'; |
| 8 | +import { Connector, Widget } from '../../types'; |
| 9 | + |
| 10 | +const withUsage = createDocumentationMessageGenerator({ |
| 11 | + name: 'dynamic-widgets', |
| 12 | + connector: true, |
| 13 | +}); |
| 14 | + |
| 15 | +export type DynamicWidgetsRenderState = { |
| 16 | + attributesToRender: string[]; |
| 17 | +}; |
| 18 | + |
| 19 | +export type DynamicWidgetsConnectorParams = { |
| 20 | + widgets: Widget[]; |
| 21 | + transformItems( |
| 22 | + items: string[], |
| 23 | + metadata: { results: SearchResults } |
| 24 | + ): string[]; |
| 25 | +}; |
| 26 | + |
| 27 | +export type DynamicWidgetsWidgetDescription = { |
| 28 | + $$type: 'ais.dynamicWidgets'; |
| 29 | + renderState: DynamicWidgetsRenderState; |
| 30 | + indexRenderState: { |
| 31 | + dynamicWidgets: DynamicWidgetsRenderState; |
| 32 | + }; |
| 33 | +}; |
| 34 | + |
| 35 | +export type DynamicWidgetsConnector = Connector< |
| 36 | + DynamicWidgetsWidgetDescription, |
| 37 | + DynamicWidgetsConnectorParams |
| 38 | +>; |
| 39 | + |
| 40 | +const connectDynamicWidgets: DynamicWidgetsConnector = function connectDynamicWidgets( |
| 41 | + renderFn, |
| 42 | + unmountFn = noop |
| 43 | +) { |
| 44 | + checkRendering(renderFn, withUsage()); |
| 45 | + |
| 46 | + return widgetParams => { |
| 47 | + const { widgets, transformItems } = widgetParams; |
| 48 | + |
| 49 | + if ( |
| 50 | + !widgets || |
| 51 | + !Array.isArray(widgets) || |
| 52 | + widgets.some(widget => typeof widget !== 'object') |
| 53 | + ) { |
| 54 | + throw new Error( |
| 55 | + withUsage('The `widgets` option expects an array of widgets.') |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + // @TODO once the attributes are computed from the results, make this optional |
| 60 | + if (typeof transformItems !== 'function') { |
| 61 | + throw new Error( |
| 62 | + withUsage('the `transformItems` option is required to be a function.') |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if ( |
| 67 | + !widgets || |
| 68 | + !Array.isArray(widgets) || |
| 69 | + widgets.some(widget => typeof widget !== 'object') |
| 70 | + ) { |
| 71 | + throw new Error( |
| 72 | + withUsage('The `widgets` option expects an array of widgets.') |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const localWidgets: Map< |
| 77 | + string, |
| 78 | + { widget: Widget; isMounted: boolean } |
| 79 | + > = new Map(); |
| 80 | + |
| 81 | + return { |
| 82 | + $$type: 'ais.dynamicWidgets', |
| 83 | + init(initOptions) { |
| 84 | + widgets.forEach(widget => { |
| 85 | + const attribute = getWidgetAttribute(widget, initOptions); |
| 86 | + localWidgets.set(attribute, { widget, isMounted: true }); |
| 87 | + }); |
| 88 | + initOptions.parent!.addWidgets(widgets); |
| 89 | + |
| 90 | + renderFn( |
| 91 | + { |
| 92 | + ...this.getWidgetRenderState(initOptions), |
| 93 | + instantSearchInstance: initOptions.instantSearchInstance, |
| 94 | + }, |
| 95 | + true |
| 96 | + ); |
| 97 | + }, |
| 98 | + render(renderOptions) { |
| 99 | + const { parent } = renderOptions; |
| 100 | + const renderState = this.getWidgetRenderState(renderOptions); |
| 101 | + |
| 102 | + const widgetsToUnmount: Widget[] = []; |
| 103 | + const widgetsToMount: Widget[] = []; |
| 104 | + |
| 105 | + localWidgets.forEach(({ widget, isMounted }, attribute) => { |
| 106 | + const shouldMount = |
| 107 | + renderState.attributesToRender.indexOf(attribute) > -1; |
| 108 | + |
| 109 | + if (!isMounted && shouldMount) { |
| 110 | + widgetsToMount.push(widget); |
| 111 | + localWidgets.set(attribute, { |
| 112 | + widget, |
| 113 | + isMounted: true, |
| 114 | + }); |
| 115 | + } else if (isMounted && !shouldMount) { |
| 116 | + widgetsToUnmount.push(widget); |
| 117 | + localWidgets.set(attribute, { |
| 118 | + widget, |
| 119 | + isMounted: false, |
| 120 | + }); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + parent!.addWidgets(widgetsToMount); |
| 125 | + // make sure this only happens after the regular render, otherwise it |
| 126 | + // happens too quick, since render is "deferred" for the next microtask, |
| 127 | + // so this needs to be a whole task later |
| 128 | + setTimeout(() => parent!.removeWidgets(widgetsToUnmount), 0); |
| 129 | + |
| 130 | + renderFn( |
| 131 | + { |
| 132 | + ...renderState, |
| 133 | + instantSearchInstance: renderOptions.instantSearchInstance, |
| 134 | + }, |
| 135 | + false |
| 136 | + ); |
| 137 | + }, |
| 138 | + dispose({ parent }) { |
| 139 | + const toRemove: Widget[] = []; |
| 140 | + localWidgets.forEach(({ widget, isMounted }) => { |
| 141 | + if (isMounted) { |
| 142 | + toRemove.push(widget); |
| 143 | + } |
| 144 | + }); |
| 145 | + parent!.removeWidgets(toRemove); |
| 146 | + |
| 147 | + unmountFn(); |
| 148 | + }, |
| 149 | + getRenderState(renderState, renderOptions) { |
| 150 | + return { |
| 151 | + ...renderState, |
| 152 | + dynamicWidgets: this.getWidgetRenderState(renderOptions), |
| 153 | + }; |
| 154 | + }, |
| 155 | + getWidgetRenderState({ results }) { |
| 156 | + if (!results) { |
| 157 | + return { attributesToRender: [], widgetParams }; |
| 158 | + } |
| 159 | + |
| 160 | + // @TODO: retrieve the facet order out of the results: |
| 161 | + // results.renderContext.facetOrder.map(facet => facet.attribute) |
| 162 | + const attributesToRender = transformItems([], { results }); |
| 163 | + |
| 164 | + return { attributesToRender, widgetParams }; |
| 165 | + }, |
| 166 | + }; |
| 167 | + }; |
| 168 | +}; |
| 169 | + |
| 170 | +export default connectDynamicWidgets; |
0 commit comments