Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update config cat implementation as per their latest docs

Joshen Limbee7b06fc6bc
diff --git a/studio/components/ui/Flag/FlagProvider.tsx b/studio/components/ui/Flag/FlagProvider.tsx
index bcbb1b05347..0ba89597d3d 100644
--- a/studio/components/ui/Flag/FlagProvider.tsx
+++ b/studio/components/ui/Flag/FlagProvider.tsx
@@ -1,41 +1,42 @@
import { FC, useEffect, useState } from 'react'
-import createConfigCatClient from 'configcat-js'
-import FlagContext from './FlagContext'
-import { IS_PLATFORM } from 'lib/constants'
-import { useStore } from 'hooks'
import { observer } from 'mobx-react-lite'
+import * as configcat from 'configcat-js'
+
import { User } from 'types'
+import { useStore } from 'hooks'
+import { IS_PLATFORM } from 'lib/constants'
+import FlagContext from './FlagContext'
const FlagProvider: FC = ({ children }) => {
- const [store, setStore] = useState({})
const { ui } = useStore()
- const { Provider } = FlagContext
const { profile } = ui
+ const { Provider } = FlagContext
+ const [store, setStore] = useState({})
+
useEffect(() => {
+ // [Joshen] getFlags get triggered everytime the tab refocuses but this should be okay
+ // as per https://configcat.com/docs/sdk-reference/js/#polling-modes:
+ // The polling downloads the config.json at the set interval and are stored in the internal cache
+ // which subsequently all getValueAsync() calls are served from there
if (IS_PLATFORM) getFlags(profile)
}, [profile])
const getFlags = async (user?: User) => {
- const setFlagValues = async () => {
- const flagValues =
- user !== undefined
- ? await client.getAllValuesAsync({ identifier: user.primary_email })
- : await client.getAllValuesAsync()
- const flagStore: any = {}
-
- flagValues.forEach((item: any) => {
- flagStore[item.settingKey] = item.settingValue
- })
- setStore(flagStore)
- }
-
- const client = createConfigCatClient(process.env.NEXT_PUBLIC_CONFIGCAT_SDK_KEY ?? '', {
- configChanged: setFlagValues,
- pollIntervalSeconds: 600,
+ const flagStore: any = {}
+ const client = configcat.getClient(
+ process.env.NEXT_PUBLIC_CONFIGCAT_SDK_KEY ?? '',
+ configcat.PollingMode.AutoPoll,
+ { pollIntervalSeconds: 10 }
+ )
+ const flagValues =
+ user !== undefined
+ ? await client.getAllValuesAsync(new configcat.User(user.primary_email))
+ : await client.getAllValuesAsync()
+ flagValues.forEach((item: any) => {
+ flagStore[item.settingKey] = item.settingValue
})
-
- await setFlagValues()
+ setStore(flagStore)
}
return <Provider value={store}>{children}</Provider>