about summary refs log tree commit diff
path: root/src/state/service-config.tsx
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2025-05-17 01:38:34 +0300
committerGitHub <noreply@github.com>2025-05-16 15:38:34 -0700
commit1cdbfc709235ed1933ba51403d941762f384690b (patch)
treef55aa9a16f05645bcc5c2d63839113725b1ca308 /src/state/service-config.tsx
parent75ffb3d243a5415d173f2bca8a5334b70451a1f4 (diff)
downloadvoidsky-1cdbfc709235ed1933ba51403d941762f384690b.tar.zst
Live via service config (#8378)
* add config (with temp config)

* only allow whitelisted domains in form

* move config to generic config

* use array-based config

* update deps

* rm expect-error
Diffstat (limited to 'src/state/service-config.tsx')
-rw-r--r--src/state/service-config.tsx88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/state/service-config.tsx b/src/state/service-config.tsx
new file mode 100644
index 000000000..37d5685bd
--- /dev/null
+++ b/src/state/service-config.tsx
@@ -0,0 +1,88 @@
+import {createContext, useContext, useMemo} from 'react'
+
+import {useLanguagePrefs} from '#/state/preferences/languages'
+import {useServiceConfigQuery} from '#/state/queries/service-config'
+import {device} from '#/storage'
+
+type TrendingContext = {
+  enabled: boolean
+}
+
+type LiveNowContext = {
+  did: string
+  domains: string[]
+}[]
+
+const TrendingContext = createContext<TrendingContext>({
+  enabled: false,
+})
+
+const LiveNowContext = createContext<LiveNowContext | null>(null)
+
+export function Provider({children}: {children: React.ReactNode}) {
+  const langPrefs = useLanguagePrefs()
+  const {data: config, isLoading: isInitialLoad} = useServiceConfigQuery()
+  const trending = useMemo<TrendingContext>(() => {
+    if (__DEV__) {
+      return {enabled: true}
+    }
+
+    /*
+     * Only English during beta period
+     */
+    if (
+      !!langPrefs.contentLanguages.length &&
+      !langPrefs.contentLanguages.includes('en')
+    ) {
+      return {enabled: false}
+    }
+
+    /*
+     * While loading, use cached value
+     */
+    const cachedEnabled = device.get(['trendingBetaEnabled'])
+    if (isInitialLoad) {
+      return {enabled: Boolean(cachedEnabled)}
+    }
+
+    /*
+     * Doing an extra check here to reduce hits to statsig. If it's disabled on
+     * the server, we can exit early.
+     */
+    const enabled = Boolean(config?.topicsEnabled)
+
+    // update cache
+    device.set(['trendingBetaEnabled'], enabled)
+
+    return {enabled}
+  }, [isInitialLoad, config, langPrefs.contentLanguages])
+
+  const liveNow = useMemo<LiveNowContext>(() => config?.liveNow ?? [], [config])
+
+  return (
+    <TrendingContext.Provider value={trending}>
+      <LiveNowContext.Provider value={liveNow}>
+        {children}
+      </LiveNowContext.Provider>
+    </TrendingContext.Provider>
+  )
+}
+
+export function useTrendingConfig() {
+  return useContext(TrendingContext)
+}
+
+export function useLiveNowConfig() {
+  const ctx = useContext(LiveNowContext)
+  if (!ctx) {
+    throw new Error(
+      'useLiveNowConfig must be used within a LiveNowConfigProvider',
+    )
+  }
+  return ctx
+}
+
+export function useCanGoLive(did?: string) {
+  const config = useLiveNowConfig()
+  return !!config.find(cfg => cfg.did === did)
+}