-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1857 from SUI-Components/feat-decide-toggle
Feat decide toggle
- Loading branch information
Showing
6 changed files
with
126 additions
and
68 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {useCallback, useContext} from 'react' | ||
|
||
import PdeContext from '../contexts/PdeContext.js' | ||
import {getPlatformStrategy} from './common/platformStrategies.js' | ||
|
||
/** | ||
* Hook to call decide | ||
* @return {function} | ||
*/ | ||
export default function useDecisionCallback() { | ||
const {pde} = useContext(PdeContext) | ||
|
||
if (pde === null) { | ||
throw new Error('[sui-pde: useDecision] sui-pde provider is required to work') | ||
} | ||
|
||
const decide = useCallback( | ||
(name, {attributes, trackExperimentViewed, isEventDisabled, queryString, adapterId} = {}) => { | ||
try { | ||
const strategy = getPlatformStrategy({ | ||
customTrackExperimentViewed: trackExperimentViewed | ||
}) | ||
|
||
const forced = strategy.getForcedValue({ | ||
key: name, | ||
queryString | ||
}) | ||
|
||
if (forced) { | ||
if (['on', 'off'].includes(forced)) { | ||
return {enabled: forced === 'on', flagKey: name} | ||
} | ||
|
||
return {enabled: true, flagKey: name, variationKey: forced} | ||
} | ||
|
||
const notificationId = pde.addDecideListener({ | ||
onDecide: ({type, decisionInfo: decision}) => { | ||
const {ruleKey, variationKey, decisionEventDispatched} = decision | ||
|
||
if (type === 'flag' && decisionEventDispatched) { | ||
strategy.trackExperiment({variationName: variationKey, experimentName: ruleKey}) | ||
} | ||
} | ||
}) | ||
|
||
const data = strategy.decide({ | ||
pde, | ||
name, | ||
attributes, | ||
adapterId, | ||
isEventDisabled | ||
}) | ||
|
||
pde.removeNotificationListener({notificationId}) | ||
|
||
return data | ||
} catch (error) { | ||
return {enabled: false, flagKey: name} | ||
} | ||
}, | ||
[] | ||
) | ||
|
||
return {decide} | ||
} |
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
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