0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-25 11:17:50 +01:00
posthog/.storybook/preview.tsx

132 lines
4.0 KiB
TypeScript
Raw Normal View History

import '~/styles'
import type { Meta, Parameters, Preview } from '@storybook/react'
import { Title, Subtitle, Description, Primary, Controls, Stories } from '@storybook/blocks'
import { worker } from '~/mocks/browser'
chore(correlation): funnel story with correlation events and properties (#6389) * chore(correlation): funnel story with correlation events and properties This commit: 1. adds msw so we can easily mock the API requests 2. starts msw on storybook page load 3. adds the `mockServiceWorker.js` to storybook public folder so it can be loaded by the frontend 4. adds a Funnel story that defines the funnel and correlation responses Note that we also define types for requests and responses. These could be shared with the actual application code, so we can explicitly couple the application changes to updating storybooks, i.e. to help avoid stories going stale. The requests and responses are quite basic, but they are sufficient to be able to easily see and make changes. It has a requirement that it should be easy for anyone else who comes to the code to easily parse and be able to make updates. We don't want these to be rotten stories but *the place* that people go to first when making their changes. Having simple handwritten request/responses also introduces the stability needed to, for example, use regression tests. Changes are deliberate. * refactor(storybook): move funnel stories to their own file This also does quite a bit of cleaning up of the storybook and funnel story code. Trying to come down on an acceptable way to manage the sample data. * dev(storybook): specify public dir when building storybook I'm hoping this means that chromatic will function properly :pray: * chore: add back the withApi decorator * setup msw first * dev(storybook): Make posthogjs work in chromatic * dont use as cast for response types * make hot module reload work
2021-10-13 06:45:51 +02:00
import { loadPostHogJS } from '~/loadPostHogJS'
import { withKea } from './decorators/withKea'
import { withMockDate } from './decorators/withMockDate'
import { defaultMocks } from '~/mocks/handlers'
import { withFeatureFlags } from './decorators/withFeatureFlags'
import { withTheme } from './decorators/withTheme'
import { apiHostOrigin } from 'lib/utils/apiHost'
import { getStorybookAppContext } from './app-context'
chore(correlation): funnel story with correlation events and properties (#6389) * chore(correlation): funnel story with correlation events and properties This commit: 1. adds msw so we can easily mock the API requests 2. starts msw on storybook page load 3. adds the `mockServiceWorker.js` to storybook public folder so it can be loaded by the frontend 4. adds a Funnel story that defines the funnel and correlation responses Note that we also define types for requests and responses. These could be shared with the actual application code, so we can explicitly couple the application changes to updating storybooks, i.e. to help avoid stories going stale. The requests and responses are quite basic, but they are sufficient to be able to easily see and make changes. It has a requirement that it should be easy for anyone else who comes to the code to easily parse and be able to make updates. We don't want these to be rotten stories but *the place* that people go to first when making their changes. Having simple handwritten request/responses also introduces the stability needed to, for example, use regression tests. Changes are deliberate. * refactor(storybook): move funnel stories to their own file This also does quite a bit of cleaning up of the storybook and funnel story code. Trying to come down on an acceptable way to manage the sample data. * dev(storybook): specify public dir when building storybook I'm hoping this means that chromatic will function properly :pray: * chore: add back the withApi decorator * setup msw first * dev(storybook): Make posthogjs work in chromatic * dont use as cast for response types * make hot module reload work
2021-10-13 06:45:51 +02:00
const setupMsw = () => {
// Make sure the msw worker is started
worker.start({
quiet: true,
onUnhandledRequest(request, print) {
// MSW warns on all unhandled requests, but we don't necessarily care
const pathAllowList = ['/images/']
if (pathAllowList.some((path) => request.url.pathname.startsWith(path))) {
return
}
// Otherwise, default MSW warning behavior
print.warning()
},
})
;(window as any).__mockServiceWorker = worker
;(window as any).POSTHOG_APP_CONTEXT = getStorybookAppContext()
chore(correlation): funnel story with correlation events and properties (#6389) * chore(correlation): funnel story with correlation events and properties This commit: 1. adds msw so we can easily mock the API requests 2. starts msw on storybook page load 3. adds the `mockServiceWorker.js` to storybook public folder so it can be loaded by the frontend 4. adds a Funnel story that defines the funnel and correlation responses Note that we also define types for requests and responses. These could be shared with the actual application code, so we can explicitly couple the application changes to updating storybooks, i.e. to help avoid stories going stale. The requests and responses are quite basic, but they are sufficient to be able to easily see and make changes. It has a requirement that it should be easy for anyone else who comes to the code to easily parse and be able to make updates. We don't want these to be rotten stories but *the place* that people go to first when making their changes. Having simple handwritten request/responses also introduces the stability needed to, for example, use regression tests. Changes are deliberate. * refactor(storybook): move funnel stories to their own file This also does quite a bit of cleaning up of the storybook and funnel story code. Trying to come down on an acceptable way to manage the sample data. * dev(storybook): specify public dir when building storybook I'm hoping this means that chromatic will function properly :pray: * chore: add back the withApi decorator * setup msw first * dev(storybook): Make posthogjs work in chromatic * dont use as cast for response types * make hot module reload work
2021-10-13 06:45:51 +02:00
}
setupMsw()
const setupPosthogJs = () => {
// Make sure we don't hit production posthog. We want to control requests to,
// e.g. `/decide/` for feature flags
window.JS_POSTHOG_HOST = apiHostOrigin()
chore(correlation): funnel story with correlation events and properties (#6389) * chore(correlation): funnel story with correlation events and properties This commit: 1. adds msw so we can easily mock the API requests 2. starts msw on storybook page load 3. adds the `mockServiceWorker.js` to storybook public folder so it can be loaded by the frontend 4. adds a Funnel story that defines the funnel and correlation responses Note that we also define types for requests and responses. These could be shared with the actual application code, so we can explicitly couple the application changes to updating storybooks, i.e. to help avoid stories going stale. The requests and responses are quite basic, but they are sufficient to be able to easily see and make changes. It has a requirement that it should be easy for anyone else who comes to the code to easily parse and be able to make updates. We don't want these to be rotten stories but *the place* that people go to first when making their changes. Having simple handwritten request/responses also introduces the stability needed to, for example, use regression tests. Changes are deliberate. * refactor(storybook): move funnel stories to their own file This also does quite a bit of cleaning up of the storybook and funnel story code. Trying to come down on an acceptable way to manage the sample data. * dev(storybook): specify public dir when building storybook I'm hoping this means that chromatic will function properly :pray: * chore: add back the withApi decorator * setup msw first * dev(storybook): Make posthogjs work in chromatic * dont use as cast for response types * make hot module reload work
2021-10-13 06:45:51 +02:00
loadPostHogJS()
}
setupPosthogJs()
/** Storybook global parameters. See https://storybook.js.org/docs/react/writing-stories/parameters#global-parameters */
export const parameters: Parameters = {
actions: { argTypesRegex: '^on[A-Z].*', disabled: true },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
options: {
// automatically show code panel
2022-08-08 08:27:00 +02:00
showPanel: false,
storySort: {
method: 'alphabetical',
order: [
'Lemon UI',
['Overview', 'Utilities', 'Icons'],
'Components',
'Forms',
['Field'],
'Filters',
'Layout',
],
},
},
viewMode: 'docs',
// auto-expand code blocks in docs
docs: {
2022-08-08 08:27:00 +02:00
source: { state: 'closed' },
},
msw: {
mocks: defaultMocks,
},
}
chore(correlation): funnel story with correlation events and properties (#6389) * chore(correlation): funnel story with correlation events and properties This commit: 1. adds msw so we can easily mock the API requests 2. starts msw on storybook page load 3. adds the `mockServiceWorker.js` to storybook public folder so it can be loaded by the frontend 4. adds a Funnel story that defines the funnel and correlation responses Note that we also define types for requests and responses. These could be shared with the actual application code, so we can explicitly couple the application changes to updating storybooks, i.e. to help avoid stories going stale. The requests and responses are quite basic, but they are sufficient to be able to easily see and make changes. It has a requirement that it should be easy for anyone else who comes to the code to easily parse and be able to make updates. We don't want these to be rotten stories but *the place* that people go to first when making their changes. Having simple handwritten request/responses also introduces the stability needed to, for example, use regression tests. Changes are deliberate. * refactor(storybook): move funnel stories to their own file This also does quite a bit of cleaning up of the storybook and funnel story code. Trying to come down on an acceptable way to manage the sample data. * dev(storybook): specify public dir when building storybook I'm hoping this means that chromatic will function properly :pray: * chore: add back the withApi decorator * setup msw first * dev(storybook): Make posthogjs work in chromatic * dont use as cast for response types * make hot module reload work
2021-10-13 06:45:51 +02:00
// Setup storybook global decorators. See https://storybook.js.org/docs/react/writing-stories/decorators#global-decorators
export const decorators: Meta['decorators'] = [
// Make sure the msw service worker is started, and reset the handlers to defaults.
withKea,
// Allow us to time travel to ensure our stories don't change over time.
// To mock a date for a story, set the `mockDate` parameter.
withMockDate,
// Allow us to easily set feature flags in stories.
withFeatureFlags,
// Set theme from global context
withTheme,
chore(correlation): funnel story with correlation events and properties (#6389) * chore(correlation): funnel story with correlation events and properties This commit: 1. adds msw so we can easily mock the API requests 2. starts msw on storybook page load 3. adds the `mockServiceWorker.js` to storybook public folder so it can be loaded by the frontend 4. adds a Funnel story that defines the funnel and correlation responses Note that we also define types for requests and responses. These could be shared with the actual application code, so we can explicitly couple the application changes to updating storybooks, i.e. to help avoid stories going stale. The requests and responses are quite basic, but they are sufficient to be able to easily see and make changes. It has a requirement that it should be easy for anyone else who comes to the code to easily parse and be able to make updates. We don't want these to be rotten stories but *the place* that people go to first when making their changes. Having simple handwritten request/responses also introduces the stability needed to, for example, use regression tests. Changes are deliberate. * refactor(storybook): move funnel stories to their own file This also does quite a bit of cleaning up of the storybook and funnel story code. Trying to come down on an acceptable way to manage the sample data. * dev(storybook): specify public dir when building storybook I'm hoping this means that chromatic will function properly :pray: * chore: add back the withApi decorator * setup msw first * dev(storybook): Make posthogjs work in chromatic * dont use as cast for response types * make hot module reload work
2021-10-13 06:45:51 +02:00
]
const preview: Preview = {
parameters: {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
docs: {
page: () => (
<>
<Title />
<Subtitle />
<Description />
<Primary />
<Controls />
<Stories />
</>
),
},
},
globalTypes: {
theme: {
description: '',
defaultValue: 'light',
toolbar: {
title: 'Theme',
items: [
{ value: 'light', icon: 'sun', title: 'Light' },
{ value: 'dark', icon: 'moon', title: 'Dark' },
],
// change the title based on the selected value
dynamicTitle: true,
},
},
},
}
export default preview