mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: add payments feature access based on config
This commit is contained in:
parent
5a746a946d
commit
546e8a5f98
19 changed files with 98 additions and 43 deletions
36
apps/web/components/Hooks/useFeatureFlag.tsx
Normal file
36
apps/web/components/Hooks/useFeatureFlag.tsx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { useOrg } from '@components/Contexts/OrgContext'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
type FeatureType = {
|
||||
path: string[]
|
||||
defaultValue?: boolean
|
||||
}
|
||||
|
||||
function useFeatureFlag(feature: FeatureType) {
|
||||
const org = useOrg() as any
|
||||
const [isEnabled, setIsEnabled] = useState<boolean>(!!feature.defaultValue)
|
||||
|
||||
useEffect(() => {
|
||||
if (org?.config?.config) {
|
||||
let currentValue = org.config.config
|
||||
|
||||
// Traverse the path to get the feature flag value
|
||||
for (const key of feature.path) {
|
||||
if (currentValue && typeof currentValue === 'object') {
|
||||
currentValue = currentValue[key]
|
||||
} else {
|
||||
currentValue = feature.defaultValue || false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
setIsEnabled(!!currentValue)
|
||||
} else {
|
||||
setIsEnabled(!!feature.defaultValue)
|
||||
}
|
||||
}, [org, feature])
|
||||
|
||||
return isEnabled
|
||||
}
|
||||
|
||||
export default useFeatureFlag
|
||||
Loading…
Add table
Add a link
Reference in a new issue