about summary refs log tree commit diff
path: root/src/state
diff options
context:
space:
mode:
Diffstat (limited to 'src/state')
-rw-r--r--src/state/queries/service-config.ts6
-rw-r--r--src/state/service-config.tsx (renamed from src/state/trending-config.tsx)45
2 files changed, 44 insertions, 7 deletions
diff --git a/src/state/queries/service-config.ts b/src/state/queries/service-config.ts
index 12d2cc6be..890a49a5c 100644
--- a/src/state/queries/service-config.ts
+++ b/src/state/queries/service-config.ts
@@ -6,6 +6,10 @@ import {useAgent} from '#/state/session'
 type ServiceConfig = {
   checkEmailConfirmed: boolean
   topicsEnabled: boolean
+  liveNow: {
+    did: string
+    domains: string[]
+  }[]
 }
 
 export function useServiceConfigQuery() {
@@ -21,11 +25,13 @@ export function useServiceConfigQuery() {
           checkEmailConfirmed: Boolean(data.checkEmailConfirmed),
           // @ts-expect-error not included in types atm
           topicsEnabled: Boolean(data.topicsEnabled),
+          liveNow: data.liveNow ?? [],
         }
       } catch (e) {
         return {
           checkEmailConfirmed: false,
           topicsEnabled: false,
+          liveNow: [],
         }
       }
     },
diff --git a/src/state/trending-config.tsx b/src/state/service-config.tsx
index 1e5db9dc9..37d5685bd 100644
--- a/src/state/trending-config.tsx
+++ b/src/state/service-config.tsx
@@ -1,21 +1,28 @@
-import React from 'react'
+import {createContext, useContext, useMemo} from 'react'
 
 import {useLanguagePrefs} from '#/state/preferences/languages'
 import {useServiceConfigQuery} from '#/state/queries/service-config'
 import {device} from '#/storage'
 
-type Context = {
+type TrendingContext = {
   enabled: boolean
 }
 
-const Context = React.createContext<Context>({
+type LiveNowContext = {
+  did: string
+  domains: string[]
+}[]
+
+const TrendingContext = createContext<TrendingContext>({
   enabled: false,
 })
 
-export function Provider({children}: React.PropsWithChildren<{}>) {
+const LiveNowContext = createContext<LiveNowContext | null>(null)
+
+export function Provider({children}: {children: React.ReactNode}) {
   const langPrefs = useLanguagePrefs()
   const {data: config, isLoading: isInitialLoad} = useServiceConfigQuery()
-  const ctx = React.useMemo<Context>(() => {
+  const trending = useMemo<TrendingContext>(() => {
     if (__DEV__) {
       return {enabled: true}
     }
@@ -49,9 +56,33 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
 
     return {enabled}
   }, [isInitialLoad, config, langPrefs.contentLanguages])
-  return <Context.Provider value={ctx}>{children}</Context.Provider>
+
+  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 React.useContext(Context)
+  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)
 }