Skip to content

Commit

Permalink
fix: storybook beta breaking SET_CURRENT_STORY
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 22, 2020
1 parent 74d3b51 commit 11e0386
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
18 changes: 15 additions & 3 deletions misc/storybook-custom-docs/src/components/ManagerContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { API } from '@storybook/api';
import { API, Consumer, Combo } from '@storybook/api';
import { UPDATE_STORY_CONTEXT } from '../types';

interface ManagerContainerProps {
active?: boolean;
Expand All @@ -11,7 +12,7 @@ interface ManagerContainerProps {
export const ManagerContainer: React.FC<ManagerContainerProps> = props => {
const { active, api, route } = props;
const channel = React.useMemo(() => api.getChannel(), [api]);

const storyRef = React.useRef('');
React.useEffect(() => {
const ATTACH_DOCS_PAGE = `attach_docs_page_${route}`;

Expand All @@ -29,5 +30,16 @@ export const ManagerContainer: React.FC<ManagerContainerProps> = props => {
};
updateDOM();
}, [active, api, route, channel]);
return null;

return (
<Consumer>
{({ state }: Combo) => {
if (state.storyId !== storyRef.current) {
storyRef.current = state.storyId;
channel.emit(UPDATE_STORY_CONTEXT, state);
}
return null;
}}
</Consumer>
);
};
6 changes: 3 additions & 3 deletions misc/storybook-custom-docs/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import addons from '@storybook/addons';
import { SET_CURRENT_STORY } from '@storybook/core-events';
import { ConfigApi } from '@storybook/client-api';
import { UPDATE_STORY_CONTEXT } from './types';
export * from './types';

/**
Expand Down Expand Up @@ -49,10 +49,10 @@ export const useStoryId = () => {
const onStoryChange = ({ storyId: id }: { storyId: string }) => {
setStoryId(id);
};
channel.on(SET_CURRENT_STORY, onStoryChange);
channel.on(UPDATE_STORY_CONTEXT, onStoryChange);

return () => {
channel.off(SET_CURRENT_STORY, onStoryChange);
channel.off(UPDATE_STORY_CONTEXT, onStoryChange);
};
}, [channel]);
return storyId;
Expand Down
2 changes: 2 additions & 0 deletions misc/storybook-custom-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ export interface CustomPageDef {
// Return an object that can be rendered from ReactDOM.render
render: CustomPageRenderFn;
}

export const UPDATE_STORY_CONTEXT = `update_story_context`;
1 change: 0 additions & 1 deletion ui/blocks/src/context/block/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const BlockContextProvider: React.FC<BlockContextInputProps> = ({
options,
}) => {
let storyId = propsStoryId;
console.log(propsStoryId, propsDocId);
const docId = storyId || propsDocId;
if (!storyId && docId) {
const doc = store.getStoryDoc(docId);
Expand Down

0 comments on commit 11e0386

Please sign in to comment.