import React from 'react'
import { type Meta } from '@storybook/react-vite'
import { getEnvironmentName } from '../../services/apiEndpointHelpers'
import { DocsTemplate } from '../../../.storybook'
export const Example = (): React.JSX.Element => {
const environment = getEnvironmentName()
// Example with custom detectors
const customEnvironment = getEnvironmentName({
customDetectors: [
{
name: 'development',
matcher: (hostname) => hostname.includes('my-custom-dev.com'),
},
],
})
// Example with fallback environment
const fallbackEnvironment = getEnvironmentName({
fallbackEnvironment: 'development',
})
return (
Default environment detection: {environment}
Custom detector example: {customEnvironment}
Fallback environment example: {fallbackEnvironment}
)
}
Example.storyName = 'getEnvironmentName'
export default {
title: 'Helper Functions/getEnvironmentName',
component: Example,
parameters: {
viewMode: 'docs',
previewTabs: {
canvas: { hidden: true },
},
docs: {
page: () => (
<>
hostname.includes('my-feature-branch.vercel.app')
},
{
name: 'demo',
matcher: (hostname) => hostname.includes('tenant1.demo.com')
}
]
})
// Override fallback environment
const envWithFallback = getEnvironmentName({
fallbackEnvironment: 'development'
})
// Combine custom detectors with fallback
const advancedEnv = getEnvironmentName({
customDetectors: [
{
name: 'stage',
matcher: (hostname) => hostname.includes('temp-staging.com')
}
],
fallbackEnvironment: 'development'
})`}
description="A flexible utility function that determines the current environment by analyzing the application's hostname. Now supports extensive customization options for one-off cases, allowing custom environment detection without modifying the core library."
infoBullets={[
Core features:
-
Automatically detects environment based on hostname patterns
using array-driven approach
-
Supports custom environment detectors for temporary or
special cases
-
Configurable fallback environment when no patterns match
- Client-side safe with server-side compatibility
,
Default environment detection patterns:
-
development: localhost, 127.0.0.1, and
chromatic.com
-
stage: URLs containing 'staging',
'-stage.pattern.com', or '-stage.patternasia.com.cn'
-
demo: Specifically for 'demo.usepredict.com'
-
production: Default fallback for all other
cases
,
Configuration options:
-
customDetectors: Array of custom environment
detectors checked before default logic
-
fallbackEnvironment: Environment to return when
no detectors match (default: 'production')
-
Custom detectors use matcher functions for flexible hostname
pattern matching
- Detectors are evaluated in order - first match wins
,
Common use cases:
- Configuring API endpoints per environment
- Enabling/disabling features based on environment
- Setting environment-specific defaults
- Temporary environment detection for feature branches
- Multi-tenant environment detection
- Testing with forced environment configurations
,
Benefits of the enhanced approach:
-
No Library Modifications: Handle one-off
cases without changing core code
-
Backward Compatible: Existing code
continues to work unchanged
-
Type Safe: Full TypeScript support with
proper type checking
-
Maintainable: Array-driven approach makes
adding/removing environments easier
,
]}
/>
>
),
},
},
} as Meta