Files
bsky-social-app/src/state/service-config.tsx
T
Eric Bailey 2223babc2b [APP-1759] Live Now Open Beta (#9699)
* Add report dialog to LiveStatus dialog

* Mr Worldwide™

* Bug fix bug fix

* Copy update

* Simplify parsing

* Bump api sdk

* update dev-env

* Update yarn.lock

* Bump api sdk

* Refactor useActorStatus, add disablement and appeal dialog

* Fix types

* Guard against chat profile views

* Go live (disabled)

* Fix types

* Update config and gates

* Add allowed services

* Merge conflicts

* Fix gradient handling for nudge

* Only show nudge on your own profile

* Fix live avi overflow breakage

* Add TODO

* Feedback

* Update nux image

---------

Co-authored-by: Samuel Newman <mozzius@protonmail.com>
2026-01-14 16:57:21 -06:00

121 lines
3.3 KiB
TypeScript

import {createContext, useContext, useMemo} from 'react'
import {useGate} from '#/lib/statsig/statsig'
import {useLanguagePrefs} from '#/state/preferences/languages'
import {useServiceConfigQuery} from '#/state/queries/service-config'
import {useSession} from '#/state/session'
import {IS_DEV} from '#/env'
import {device} from '#/storage'
type TrendingContext = {
enabled: boolean
}
type LiveNowContext = {
did: string
domains: string[]
}[]
const TrendingContext = createContext<TrendingContext>({
enabled: false,
})
TrendingContext.displayName = 'TrendingContext'
const LiveNowContext = createContext<LiveNowContext>([])
LiveNowContext.displayName = 'LiveNowContext'
const CheckEmailConfirmedContext = createContext<boolean | 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])
// probably true, so default to true when loading
// if the call fails, the query will set it to false for us
const checkEmailConfirmed = config?.checkEmailConfirmed ?? true
return (
<TrendingContext.Provider value={trending}>
<LiveNowContext.Provider value={liveNow}>
<CheckEmailConfirmedContext.Provider value={checkEmailConfirmed}>
{children}
</CheckEmailConfirmedContext.Provider>
</LiveNowContext.Provider>
</TrendingContext.Provider>
)
}
export function useTrendingConfig() {
return useContext(TrendingContext)
}
const DEFAULT_LIVE_ALLOWED_DOMAINS = ['twitch.tv', 'www.twitch.tv']
export type LiveNowConfig = {
allowedDomains: Set<string>
}
export function useLiveNowConfig(): LiveNowConfig {
const ctx = useContext(LiveNowContext)
const canGoLive = useCanGoLive()
const {currentAccount} = useSession()
if (!currentAccount?.did || !canGoLive) return {allowedDomains: new Set()}
const vip = ctx.find(live => live.did === currentAccount.did)
return {
allowedDomains: new Set(
DEFAULT_LIVE_ALLOWED_DOMAINS.concat(vip ? vip.domains : []),
),
}
}
export function useCanGoLive() {
const gate = useGate()
const {hasSession} = useSession()
if (!hasSession) return false
return IS_DEV ? true : gate('live_now_beta')
}
export function useCheckEmailConfirmed() {
const ctx = useContext(CheckEmailConfirmedContext)
if (ctx === null) {
throw new Error(
'useCheckEmailConfirmed must be used within a ServiceConfigManager',
)
}
return ctx
}