11import React , { createContext , useContext , useEffect , useState , type ReactNode } from "react" ;
22import { useAPI } from "@/browser/contexts/API" ;
33
4- function isStorybookIframe ( ) : boolean {
5- return typeof window !== "undefined" && window . location . pathname . endsWith ( "iframe.html" ) ;
4+ function isStorybook ( ) : boolean {
5+ if ( typeof window === "undefined" ) {
6+ return false ;
7+ }
8+
9+ // Storybook preview iframe is usually /iframe.html, but test-runner debug URLs
10+ // (and sometimes the manager itself) use ?path=/story/... .
11+ if ( window . location . pathname . endsWith ( "iframe.html" ) ) {
12+ return true ;
13+ }
14+
15+ const params = new URLSearchParams ( window . location . search ) ;
16+ const path = params . get ( "path" ) ;
17+ if ( path && path . startsWith ( "/story/" ) ) {
18+ return true ;
19+ }
20+
21+ // Some configurations pass story identity via ?id=...
22+ if ( params . has ( "id" ) ) {
23+ return true ;
24+ }
25+
26+ return false ;
627}
728
829export type StatsTabVariant = "control" | "stats" ;
@@ -31,7 +52,7 @@ export function useFeatureFlags(): FeatureFlagsContextValue {
3152export function FeatureFlagsProvider ( props : { children : ReactNode } ) {
3253 const { api } = useAPI ( ) ;
3354 const [ statsTabState , setStatsTabState ] = useState < StatsTabState | null > ( ( ) => {
34- if ( isStorybookIframe ( ) ) {
55+ if ( isStorybook ( ) ) {
3556 return { enabled : true , variant : "stats" , override : "default" } ;
3657 }
3758
@@ -58,7 +79,7 @@ export function FeatureFlagsProvider(props: { children: ReactNode }) {
5879 } ;
5980
6081 useEffect ( ( ) => {
61- if ( isStorybookIframe ( ) ) {
82+ if ( isStorybook ( ) ) {
6283 return ;
6384 }
6485
0 commit comments