Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 53 additions & 43 deletions code/addons/docs/src/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,58 @@ import {
import type { SourceParameters } from './blocks/blocks';
import { Source } from './blocks/components/Source';

const CodePanel = ({
active,
lastEvent,
currentStoryId,
}: {
active: boolean | undefined;
lastEvent: any | undefined;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try to type this, then I noticed that where it is emitted, format is not actually set....

addons.getChannel().emit(SNIPPET_RENDERED, {
id,
source: result,
args: unmappedArgs,
});

currentStoryId: string | undefined;
}) => {
const [codeSnippet, setSourceCode] = useState<{
source: string | undefined;
format: SyntaxHighlighterFormatTypes | undefined;
}>({
source: lastEvent?.source,
format: lastEvent?.format ?? undefined,
});

const parameter = useParameter(PARAM_KEY, {
source: { code: '' } as SourceParameters,
theme: 'dark',
});

useEffect(() => {
setSourceCode({
source: undefined,
format: undefined,
});
}, [currentStoryId]);

useChannel({
[SNIPPET_RENDERED]: ({ source, format }) => {
setSourceCode({ source, format });
},
});

const theme = useTheme();
const isDark = theme.base !== 'light';

return (
<AddonPanel active={!!active}>
<SourceStyles>
<Source
{...parameter.source}
code={parameter.source?.code || codeSnippet.source || parameter.source?.originalSource}
format={codeSnippet.format}
dark={isDark}
/>
</SourceStyles>
</AddonPanel>
);
};

addons.register(ADDON_ID, (api) => {
addons.add(PANEL_ID, {
title: 'Code',
Expand All @@ -40,49 +92,7 @@ addons.register(ADDON_ID, (api) => {

const lastEvent = channel?.last(SNIPPET_RENDERED)?.[0];

const [codeSnippet, setSourceCode] = useState<{
source: string | undefined;
format: SyntaxHighlighterFormatTypes | undefined;
}>({
source: lastEvent?.source,
format: lastEvent?.format ?? undefined,
});

const parameter = useParameter(PARAM_KEY, {
source: { code: '' } as SourceParameters,
theme: 'dark',
});

useEffect(() => {
setSourceCode({
source: undefined,
format: undefined,
});
}, [currentStory?.id]);

useChannel({
[SNIPPET_RENDERED]: ({ source, format }) => {
setSourceCode({ source, format });
},
});

const theme = useTheme();
const isDark = theme.base !== 'light';

return (
<AddonPanel active={!!active}>
<SourceStyles>
<Source
{...parameter.source}
code={
parameter.source?.code || codeSnippet.source || parameter.source?.originalSource
}
format={codeSnippet.format}
dark={isDark}
/>
</SourceStyles>
</AddonPanel>
);
return <CodePanel currentStoryId={currentStory?.id} lastEvent={lastEvent} active={active} />;
},
});
});
Expand Down