|
| 1 | +import { action } from '@storybook/addon-actions'; |
| 2 | +import algoliasearch from 'algoliasearch/lite'; |
| 3 | +import instantsearch from '../src/index'; |
| 4 | +import defaultPlayground from './playgrounds/default'; |
| 5 | + |
| 6 | +export const withHits = ( |
| 7 | + storyFn: ({ |
| 8 | + container, |
| 9 | + instantsearch, |
| 10 | + search, |
| 11 | + }: { |
| 12 | + container: HTMLElement; |
| 13 | + instantsearch: any; |
| 14 | + search: any; |
| 15 | + }) => void, |
| 16 | + searchOptions: any |
| 17 | +) => () => { |
| 18 | + const { |
| 19 | + appId = 'latency', |
| 20 | + apiKey = '6be0576ff61c053d5f9a3225e2a90f76', |
| 21 | + indexName = 'instant_search', |
| 22 | + searchParameters = {}, |
| 23 | + playground = defaultPlayground, |
| 24 | + ...instantsearchOptions |
| 25 | + } = searchOptions || {}; |
| 26 | + |
| 27 | + const urlLogger = action('Routing state'); |
| 28 | + const search = instantsearch({ |
| 29 | + indexName, |
| 30 | + searchClient: algoliasearch(appId, apiKey), |
| 31 | + searchParameters: { |
| 32 | + hitsPerPage: 4, |
| 33 | + attributesToSnippet: ['description:15'], |
| 34 | + snippetEllipsisText: '[…]', |
| 35 | + ...searchParameters, |
| 36 | + }, |
| 37 | + routing: { |
| 38 | + router: { |
| 39 | + write: (routeState: object) => { |
| 40 | + urlLogger(JSON.stringify(routeState, null, 2)); |
| 41 | + }, |
| 42 | + read: () => ({}), |
| 43 | + createURL: () => '', |
| 44 | + onUpdate: () => {}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + ...instantsearchOptions, |
| 48 | + }); |
| 49 | + |
| 50 | + const containerElement = document.createElement('div'); |
| 51 | + |
| 52 | + // Add the preview container to add the stories in |
| 53 | + const previewElement = document.createElement('div'); |
| 54 | + previewElement.classList.add('container', 'container-preview'); |
| 55 | + containerElement.appendChild(previewElement); |
| 56 | + |
| 57 | + // Add the playground container to add widgets into |
| 58 | + const playgroundElement = document.createElement('div'); |
| 59 | + playgroundElement.classList.add('container', 'container-playground'); |
| 60 | + containerElement.appendChild(playgroundElement); |
| 61 | + |
| 62 | + const leftPanelPlaygroundElement = document.createElement('div'); |
| 63 | + leftPanelPlaygroundElement.classList.add('panel-left'); |
| 64 | + playgroundElement.appendChild(leftPanelPlaygroundElement); |
| 65 | + |
| 66 | + const rightPanelPlaygroundElement = document.createElement('div'); |
| 67 | + rightPanelPlaygroundElement.classList.add('panel-right'); |
| 68 | + playgroundElement.appendChild(rightPanelPlaygroundElement); |
| 69 | + |
| 70 | + playground({ |
| 71 | + search, |
| 72 | + leftPanel: leftPanelPlaygroundElement, |
| 73 | + rightPanel: rightPanelPlaygroundElement, |
| 74 | + }); |
| 75 | + |
| 76 | + storyFn({ |
| 77 | + container: previewElement, |
| 78 | + instantsearch, |
| 79 | + search, |
| 80 | + }); |
| 81 | + |
| 82 | + search.start(); |
| 83 | + |
| 84 | + return containerElement; |
| 85 | +}; |
0 commit comments