index.ts42 lines · main
| 1 | import type { ReactNode } from 'react' |
| 2 | |
| 3 | import * as DevToolbarModule from './DevToolbar' |
| 4 | import * as DevToolbarContextModule from './DevToolbarContext' |
| 5 | import * as DevToolbarTriggerModule from './DevToolbarTrigger' |
| 6 | import type { DevTelemetryToolbarContextType } from './types' |
| 7 | |
| 8 | // Tree-shaking pattern: conditionally export stubs outside local/staging. |
| 9 | // The bundler replaces NEXT_PUBLIC_ENVIRONMENT at build time, making the |
| 10 | // ternary static. In production builds (env === 'prod'), the implementation |
| 11 | // modules are eliminated from the bundle. |
| 12 | // Duplicated for tree-shaking — bundler must see literal process.env reference. |
| 13 | // Keep in sync: DevToolbarContext.tsx, DevToolbar.tsx, DevToolbarTrigger.tsx, feature-flags.tsx |
| 14 | const env = process.env.NEXT_PUBLIC_ENVIRONMENT |
| 15 | const isToolbarEnabled = env === 'local' || env === 'staging' |
| 16 | |
| 17 | const noopContext: DevTelemetryToolbarContextType = { |
| 18 | isEnabled: false, |
| 19 | isOpen: false, |
| 20 | setIsOpen: () => {}, |
| 21 | events: [], |
| 22 | setEvents: () => {}, |
| 23 | dismissToolbar: () => {}, |
| 24 | } |
| 25 | |
| 26 | export const DevToolbarProvider = !isToolbarEnabled |
| 27 | ? ({ children }: { children: ReactNode; apiUrl?: string }) => children |
| 28 | : DevToolbarContextModule.DevToolbarProvider |
| 29 | |
| 30 | export const useDevToolbar = !isToolbarEnabled |
| 31 | ? () => noopContext |
| 32 | : DevToolbarContextModule.useDevToolbar |
| 33 | |
| 34 | export const DevToolbar = !isToolbarEnabled |
| 35 | ? (_props: { extraTabs?: import('./types').ExtraTab[] }) => null |
| 36 | : DevToolbarModule.DevToolbar |
| 37 | |
| 38 | export const DevToolbarTrigger = !isToolbarEnabled |
| 39 | ? () => null |
| 40 | : DevToolbarTriggerModule.DevToolbarTrigger |
| 41 | |
| 42 | export type { DevTelemetryEvent, DevToolbarConfig, ExtraTab } from './types' |