-
Notifications
You must be signed in to change notification settings - Fork 52
FFL-908 Implement Flags OpenFeature provider for React Native #1106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yugisu-flux
wants to merge
3
commits into
feature/flags
Choose a base branch
from
dima/FFL-908-implement-flags-react-native-open-feature-provider
base: feature/flags
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,7 +11,13 @@ import { | |
| RumConfiguration, | ||
| DdFlags, | ||
| } from '@datadog/mobile-react-native'; | ||
| import React from 'react'; | ||
| import {DatadogProvider} from '@datadog/openfeature-react-native'; | ||
| import { | ||
| OpenFeature, | ||
| OpenFeatureProvider, | ||
| useObjectFlagDetails, | ||
| } from '@openfeature/react-sdk'; | ||
| import React, {Suspense} from 'react'; | ||
| import type {PropsWithChildren} from 'react'; | ||
| import { | ||
| ActivityIndicator, | ||
|
|
@@ -35,119 +41,91 @@ import { | |
| import {APPLICATION_ID, CLIENT_TOKEN, ENVIRONMENT} from './ddCredentials'; | ||
|
|
||
| (async () => { | ||
| const config = new CoreConfiguration( | ||
| CLIENT_TOKEN, | ||
| ENVIRONMENT, | ||
| ); | ||
| const config = new CoreConfiguration(CLIENT_TOKEN, ENVIRONMENT); | ||
| config.verbosity = SdkVerbosity.DEBUG; | ||
| config.uploadFrequency = UploadFrequency.FREQUENT; | ||
| config.batchSize = BatchSize.SMALL; | ||
|
|
||
| // Enable RUM. | ||
| config.rumConfiguration = new RumConfiguration( | ||
| APPLICATION_ID, | ||
| true, | ||
| true, | ||
| true | ||
| ) | ||
| true, | ||
| ); | ||
| config.rumConfiguration.sessionSampleRate = 100; | ||
| config.rumConfiguration.telemetrySampleRate = 100; | ||
|
|
||
| // Initialize the Datadog SDK. | ||
| await DdSdkReactNative.initialize(config); | ||
|
|
||
| // Enable Flags. | ||
| await DdFlags.enable(); | ||
|
|
||
| // Usage examples. | ||
| await DdRum.startView('main', 'Main'); | ||
| setTimeout(async () => { | ||
| await DdRum.addTiming('one_second'); | ||
| }, 1000); | ||
| await DdRum.addAction(RumActionType.CUSTOM, 'custom action'); | ||
|
|
||
| await DdLogs.info('info log'); | ||
|
|
||
| const spanId = await DdTrace.startSpan('test span'); | ||
| await DdTrace.finishSpan(spanId); | ||
| })(); | ||
|
|
||
| type SectionProps = PropsWithChildren<{ | ||
| title: string; | ||
| }>; | ||
| function AppWithProviders() { | ||
| React.useEffect(() => { | ||
| const userId = 'user-123' | ||
|
|
||
| const evaluationContext = { | ||
| targetingKey: userId, | ||
| favoriteFruit: 'apple' | ||
| } | ||
|
|
||
| const provider = new DatadogProvider(); | ||
| OpenFeature.setProvider(provider, evaluationContext); | ||
| }, []) | ||
|
|
||
| function Section({children, title}: SectionProps): React.JSX.Element { | ||
| const isDarkMode = useColorScheme() === 'dark'; | ||
| return ( | ||
| <View style={styles.sectionContainer}> | ||
| <Text | ||
| style={[ | ||
| styles.sectionTitle, | ||
| { | ||
| color: isDarkMode ? Colors.white : Colors.black, | ||
| }, | ||
| ]}> | ||
| {title} | ||
| </Text> | ||
| <Text | ||
| style={[ | ||
| styles.sectionDescription, | ||
| { | ||
| color: isDarkMode ? Colors.light : Colors.dark, | ||
| }, | ||
| ]}> | ||
| {children} | ||
| </Text> | ||
| </View> | ||
| <Suspense | ||
| fallback={ | ||
| <SafeAreaView style={{height: '100%', justifyContent: 'center'}}> | ||
| <ActivityIndicator /> | ||
| </SafeAreaView> | ||
| }> | ||
| <OpenFeatureProvider suspendUntilReady> | ||
| <App /> | ||
| </OpenFeatureProvider> | ||
| </Suspense> | ||
| ); | ||
| } | ||
|
|
||
| function App(): React.JSX.Element { | ||
| const [isInitialized, setIsInitialized] = React.useState(false); | ||
|
|
||
| React.useEffect(() => { | ||
| (async () => { | ||
| // This is a blocking async app initialization effect. | ||
| // It simulates the way most React Native applications are initialized. | ||
| await DdFlags.enable(); | ||
| const client = DdFlags.getClient(); | ||
|
|
||
| const userId = 'test-user-1'; | ||
| const userAttributes = { | ||
| country: 'US', | ||
| }; | ||
|
|
||
| await client.setEvaluationContext({targetingKey: userId, attributes: userAttributes}); | ||
|
|
||
| setIsInitialized(true); | ||
| })().catch(console.error); | ||
| }, []); | ||
| const greetingFlag = useObjectFlagDetails('rn-sdk-test-json-flag', {greeting: 'Default greeting'}); | ||
|
|
||
| const isDarkMode = useColorScheme() === 'dark'; | ||
| const backgroundStyle = { | ||
| backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, | ||
| }; | ||
|
|
||
| if (!isInitialized) { | ||
| return ( | ||
| <SafeAreaView style={{height: '100%', justifyContent: 'center'}}> | ||
| <ActivityIndicator /> | ||
| </SafeAreaView> | ||
| ); | ||
| } | ||
|
|
||
| // TODO: [FFL-908] Use OpenFeature SDK instead of a manual client call. | ||
| const testFlagKey = 'rn-sdk-test-json-flag'; | ||
| const testFlag = DdFlags.getClient().getObjectValue(testFlagKey, {greeting: "Default greeting"}); // https://app.datadoghq.com/feature-flags/bcf75cd6-96d8-4182-8871-0b66ad76127a?environmentId=d114cd9a-79ed-4c56-bcf3-bcac9293653b | ||
|
|
||
| return ( | ||
| <SafeAreaView style={backgroundStyle}> | ||
| <StatusBar | ||
| barStyle={isDarkMode ? 'light-content' : 'dark-content'} | ||
| backgroundColor={backgroundStyle.backgroundColor} | ||
| /> | ||
| <ScrollView | ||
| contentInsetAdjustmentBehavior="automatic" | ||
| style={backgroundStyle}> | ||
| <ScrollView contentInsetAdjustmentBehavior="automatic" style={backgroundStyle}> | ||
| <Header /> | ||
| <View | ||
| style={{ | ||
| backgroundColor: isDarkMode ? Colors.black : Colors.white, | ||
| }}> | ||
| <Section title="Feature Flags"> | ||
| Flag value for <Text style={styles.highlight}>{testFlagKey}</Text> is{'\n'} | ||
| <Text style={styles.highlight}>{JSON.stringify(testFlag)}</Text> | ||
|
|
||
| <View style={{backgroundColor: isDarkMode ? Colors.black : Colors.white}}> | ||
| <Section title={greetingFlag.value.greeting}> | ||
| The title of this section is based on the <Text style={styles.highlight}>{greetingFlag.flagKey}</Text> feature flag.{'\n\n'} | ||
|
|
||
| If it's different from "Default greeting", then it is coming from the feature flag evaluation. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| </Section> | ||
|
|
||
| <Section title="Step One"> | ||
| Edit <Text style={styles.highlight}>App.tsx</Text> to change this | ||
| screen and then come back to see your edits. | ||
|
|
@@ -168,6 +146,36 @@ function App(): React.JSX.Element { | |
| ); | ||
| } | ||
|
|
||
| type SectionProps = PropsWithChildren<{ | ||
| title: string; | ||
| }>; | ||
|
|
||
| function Section({children, title}: SectionProps): React.JSX.Element { | ||
| const isDarkMode = useColorScheme() === 'dark'; | ||
| return ( | ||
| <View style={styles.sectionContainer}> | ||
| <Text | ||
| style={[ | ||
| styles.sectionTitle, | ||
| { | ||
| color: isDarkMode ? Colors.white : Colors.black, | ||
| }, | ||
| ]}> | ||
| {title} | ||
| </Text> | ||
| <Text | ||
| style={[ | ||
| styles.sectionDescription, | ||
| { | ||
| color: isDarkMode ? Colors.light : Colors.dark, | ||
| }, | ||
| ]}> | ||
| {children} | ||
| </Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| sectionContainer: { | ||
| marginTop: 32, | ||
|
|
@@ -187,4 +195,4 @@ const styles = StyleSheet.create({ | |
| }, | ||
| }); | ||
|
|
||
| export default App; | ||
| export default AppWithProviders; | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧑🍳