Skip to content

feat: add custom context fields to Unleash #11730

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/app/system/flags/Components/WrappedFlagProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import FlagProvider, { IConfig, useFlagsStatus } from "@unleash/proxy-client-rea
import { useUnleashEnvironment } from "app/system/flags/hooks/useUnleashEnvironment"
import { useUnleashInitializer } from "app/system/flags/hooks/useUnleashInitializer"
import { useUnleashListener } from "app/system/flags/hooks/useUnleashListener"
import { Platform } from "react-native"
import DeviceInfo from "react-native-device-info"
import Keys from "react-native-keys"

export const WrappedFlagProvider: React.FC = ({ children }) => {
Expand All @@ -11,6 +13,12 @@ export const WrappedFlagProvider: React.FC = ({ children }) => {
const storageName = (name: string) => `unleash-values:${name}`

const config: IConfig = {
context: {
properties: {
appVersion: DeviceInfo.getVersion(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be using the version attribute within app.json instead? Looks we use that when generating our user agent string. When running in a simulator, DeviceInfo.getVersion() doesn't properly return a known version number but we still use this utility function in a few places, including the About screen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeviceInfo.getVersion() retrieves the version of the app installed from the store. That's probably why we don't see a correct version when running in a simulator.

Do we use version number from app.json anywhere?

Copy link
Member Author

@dblandin dblandin Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're currently using the app.json value to construct the user agent string (source). Maybe not very significant before but we're now using that value in Metaphysics to alter the homeView content returned.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI – I opened another PR to bring more consistency to how we determine the current app version, preferring the app.json value based on this Slack conversation.

Holding on this change until that PR is resolved – then I'll rebase here.

appPlatformOS: Platform.OS,
},
},
url:
env === "production"
? Keys.secureFor("UNLEASH_PROXY_URL_PRODUCTION")
Expand Down
11 changes: 10 additions & 1 deletion src/app/system/flags/hooks/useUnleashInitializer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { useUnleashClient } from "@unleash/proxy-client-react"
import { GlobalStore } from "app/store/GlobalStore"
import { jwtDecode } from "jwt-decode"
import { useEffect } from "react"

export const useUnleashInitializer = () => {
const client = useUnleashClient()
const userID = GlobalStore.useAppState((state) => state.auth.userID)
const userAccessToken = GlobalStore.useAppState((state) => state.auth.userAccessToken)

useEffect(() => {
if (userID) {
if (client.getContext().userId !== userID) {
client.setContextField("userId", userID)

if (userAccessToken) {
const decodedToken = jwtDecode(userAccessToken) as { roles: string }

const userRoles = decodedToken["roles"] ?? ""
client.setContextField("userRoles", userRoles)
}
}

client.start()
Expand All @@ -20,5 +29,5 @@ export const useUnleashInitializer = () => {
return () => {
client.stop()
}
}, [userID])
}, [userID, userAccessToken])
}