0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 18:26:15 +01:00
posthog/.storybook/kea-story.tsx
Marius Andra c271eb99a5
feat(storybook): cleanup volume 1 (#9033)
* rearrange stories

* fix bug in storybook with mock data

* convert to TS, fix static path deprecation

* wrap all scenes with KeaStory, deprecate getReduxState

* remove old text

* don't complain about postcss

* remove ApiSelector, revert to ".js" main

* preview ".ts"

* fix msw

* move kea story

* fix urls

* add /decide handler

* mount logics that are there on app

* reduce boilerplate

* refactor history list storybook api

* separate data
2022-03-15 17:36:51 +01:00

60 lines
1.8 KiB
TypeScript

import { createMemoryHistory } from 'history'
import { initKea } from '~/initKea'
import { combineUrl, router } from 'kea-router'
import { getContext, Provider } from 'kea'
import React, { useEffect, useState } from 'react'
import { App } from 'scenes/App'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { worker } from '~/mocks/browser'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
export function resetKeaStory(url?: string, state?: Record<string, any>): void {
worker.resetHandlers()
const initialLocation = url ? combineUrl(url) : state?.kea?.router?.location
const history = createMemoryHistory(initialLocation ? { initialEntries: [initialLocation] } : {})
;(history as any).pushState = history.push
;(history as any).replaceState = history.replace
initKea({ state, routerLocation: history.location, routerHistory: history })
featureFlagLogic.mount()
teamLogic.mount()
userLogic.mount()
router.mount()
const { store } = getContext()
store.dispatch({ type: 'storybook init' })
}
export function KeaStory<T = React.ReactNode>({
url,
state,
onInit,
children,
}: {
url?: string
state?: Record<string, any>
onInit?: () => void
children: T
}): T | JSX.Element | null {
const [didReset, setDidReset] = useState(false)
useEffect(() => {
if (!didReset) {
resetKeaStory(url, state)
onInit?.()
setDidReset(true)
}
}, [didReset])
return didReset ? <Provider>{children || <App />}</Provider> : null
}
export function keaStory(Component: any, json: any): () => JSX.Element {
return function KeaStoryInstance() {
return (
<KeaStory state={json}>
<Component />
</KeaStory>
)
}
}