Skip to content

Commit

Permalink
Merge pull request #25 from shapeshift/add-feature-flag-helper
Browse files Browse the repository at this point in the history
chore: add feature flag helper
  • Loading branch information
0xApotheosis authored Jan 9, 2024
2 parents 93fa363 + 94e058b commit 006787d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Feature flags
VITE_FEATURE_MIXPANEL=true
VITE_FEATURE_CHATWOOT=false
VITE_FEATURE_CHATWOOT=true

VITE_FOO=bar

Expand Down
3 changes: 2 additions & 1 deletion src/components/Chatwoot.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button } from '@chakra-ui/react'
import { useCallback, useEffect } from 'react'
import { getFeatureFlag } from 'lib/utils'

export const ChatwootButton: React.FC = () => {
const chatWootEnabled = import.meta.env.VITE_FEATURE_CHATWOOT
const chatWootEnabled = getFeatureFlag('chatwoot')
useEffect(() => {
if (!chatWootEnabled) return // Add Chatwoot Settings
;(window as any).chatwootSettings = {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mixpanel/mixpanelSingleton.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Mixpanel from 'mixpanel-browser'
import { getFeatureFlag } from 'lib/utils'

import type { MixPanelType } from './types'

// we need to be able to access this outside react
export const mixpanel = (() => {
let _mixPanel: MixPanelType | undefined = undefined

const mixPanelEnabled = import.meta.env.VITE_FEATURE_MIXPANEL
const mixPanelEnabled = getFeatureFlag('mixpanel')
if (!mixPanelEnabled) return undefined
if (_mixPanel) return _mixPanel
const mixpanelToken = import.meta.env.VITE_MIXPANEL_TOKEN
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getFeatureFlag = (flagName: string): boolean => {
const envVarName = `VITE_FEATURE_${flagName.toUpperCase()}`
return import.meta.env[envVarName] === 'true'
}

0 comments on commit 006787d

Please sign in to comment.