This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c29098a
commit 137a3a1
Showing
1 changed file
with
1 addition
and
50 deletions.
There are no files selected for viewing
51 changes: 1 addition & 50 deletions
51
themes/gatsby-theme-deriv/src/components/hooks/use-third-party-flags.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,7 @@ | ||
import { useState, useEffect } from 'react' | ||
import { initializeApp } from 'firebase/app' | ||
import { getDatabase, ref, onValue } from 'firebase/database' | ||
// import useRegion from 'components/hooks/use-region' | ||
import featuresConfig from '../../../static/appConfig' | ||
|
||
const thirdPartyFlagsConfig = { | ||
databaseURL: process.env.GATSBY_DATABASE_URL, | ||
} | ||
|
||
const useThirdPartyFlags = (featurePath) => { | ||
const [data, setData] = useState(featuresConfig) | ||
const [feature, setFeature] = useState(null) | ||
|
||
useEffect(() => { | ||
const app = initializeApp(thirdPartyFlagsConfig, 'thirdPartyFlagsConfig') | ||
const db = getDatabase(app) | ||
|
||
// let fetchedData = featuresConfig | ||
const starCountRef = ref(db, 'remote_config/deriv-com') | ||
const flagsData = onValue( | ||
starCountRef, | ||
(snapshot) => { | ||
setData(snapshot.val()) | ||
}, | ||
(error) => { | ||
console.error(error) | ||
}, | ||
) | ||
return flagsData | ||
}, []) | ||
|
||
useEffect(() => { | ||
const pathParts = featurePath.split('.') | ||
let currentFeature = data | ||
for (const part of pathParts) { | ||
if (currentFeature[part] === undefined) { | ||
return // Feature path does not exist | ||
} | ||
currentFeature = currentFeature[part] | ||
} | ||
if (typeof currentFeature === 'object') { | ||
setFeature(currentFeature) | ||
} else { | ||
if (featurePath === 'maintenance_mode_content') { | ||
setFeature(currentFeature) | ||
} else { | ||
setFeature(Boolean(currentFeature)) | ||
} | ||
} | ||
}, [data, featurePath]) | ||
|
||
return feature | ||
return featurePath ? featuresConfig : featuresConfig | ||
} | ||
|
||
export default useThirdPartyFlags |