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 715056ab69
refactor(frontend): several tiny refactors (#9126)
* thrown an explicit error if can't render react app (shown on the toolbar authorize scene)

* remove reverse logic connection

* upgrade kea, remove deprecated code

* don't permanently auto-connect this logic

* add few direct connections

* remove automatic mount

* fix few extra setState calls

* improve howto create stories

* refactor new dashboard form

* use new form component for flags

* new flag story

* refactor scene stories to use <App />

* add even more default mocks

* fix two bugs

* fix more test issues

* increase the quality of mocked data

* fix new dashboard redirect logic, simplify code even further

* fix test

* update docs
2022-03-21 13:45:10 +01:00

38 lines
1.2 KiB
TypeScript

import { createMemoryHistory } from 'history'
import { initKea } from '~/initKea'
import { 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(): void {
worker.resetHandlers()
const history = createMemoryHistory({})
;(history as any).pushState = history.push
;(history as any).replaceState = history.replace
initKea({ 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>({ children }: { children: T }): T | JSX.Element | null {
const [didReset, setDidReset] = useState(false)
useEffect(() => {
if (!didReset) {
resetKeaStory()
setDidReset(true)
}
}, [didReset])
return didReset ? <Provider>{children || <App />}</Provider> : null
}