diff --git a/code/ui/manager/src/components/preview/FramesRenderer.tsx b/code/ui/manager/src/components/preview/FramesRenderer.tsx index 22d6dadf118a..e794577042a4 100644 --- a/code/ui/manager/src/components/preview/FramesRenderer.tsx +++ b/code/ui/manager/src/components/preview/FramesRenderer.tsx @@ -1,5 +1,5 @@ import type { FC } from 'react'; -import React, { Fragment, useMemo, useEffect, useState } from 'react'; +import React, { useRef, Fragment } from 'react'; import type { Combo } from '@storybook/manager-api'; import { Consumer } from '@storybook/manager-api'; import { Button, getStoryHref } from '@storybook/components'; @@ -40,9 +40,17 @@ const whenSidebarIsVisible = ({ state }: Combo) => ({ selectedStoryId: state.storyId, }); +const styles: CSSObject = { + '#root [data-is-storybook="false"]': { + display: 'none', + }, + '#root [data-is-storybook="true"]': { + display: 'block', + }, +}; + export const FramesRenderer: FC = ({ refs, - entry, scale, viewMode = 'story', refId, @@ -56,51 +64,28 @@ export const FramesRenderer: FC = ({ ...(version && { version }), }); const active = getActive(refId, refs); + const { current: frames } = useRef>({}); - const styles = useMemo(() => { - // add #root to make the selector high enough in specificity - return { - '#root [data-is-storybook="false"]': { - display: 'none', - }, - '#root [data-is-storybook="true"]': { - display: 'block', - }, - }; - }, []); + const refsToLoad = Object.values(refs).filter((ref) => { + return ref.type === 'auto-inject' || ref.id === refId; + }, {}); - const [frames, setFrames] = useState>({ - 'storybook-preview-iframe': getStoryHref(baseUrl, storyId, { + if (!frames['storybook-preview-iframe']) { + frames['storybook-preview-iframe'] = getStoryHref(baseUrl, storyId, { ...queryParams, ...(version && { version }), viewMode, - }), - }); - - useEffect(() => { - const newFrames = Object.values(refs) - .filter((r) => { - if (r.indexError) { - return false; - } - if (r.type === 'auto-inject') { - return true; - } - if (entry && r.id === entry.refId) { - return true; - } - - return false; - }) - .reduce((acc, r) => { - return { - ...acc, - [`storybook-ref-${r.id}`]: `${r.url}/iframe.html?id=${storyId}&viewMode=${viewMode}&refId=${r.id}${stringifiedQueryParams}`, - }; - }, frames); + }); + } - setFrames(newFrames); - }, [storyId, entry, refs]); + refsToLoad.forEach((ref) => { + const id = `storybook-ref-${ref.id}`; + const existingUrl = frames[id]?.split('/iframe.html')[0]; + if (!existingUrl || ref.url !== existingUrl) { + const newUrl = `${ref.url}/iframe.html?id=${storyId}&viewMode=${viewMode}&refId=${ref.id}${stringifiedQueryParams}`; + frames[id] = newUrl; + } + }); return ( @@ -117,19 +102,21 @@ export const FramesRenderer: FC = ({ return null; }} - {Object.entries(frames).map(([id, src]) => ( - -