forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {createContext, useContext, useMemo} from 'react'
2
3import {useLanguagePrefs} from '#/state/preferences/languages'
4import {useServiceConfigQuery} from '#/state/queries/service-config'
5import {device} from '#/storage'
6
7type TrendingContext = {
8 enabled: boolean
9}
10
11type LiveNowContext = {
12 did: string
13 domains: string[]
14}[]
15
16const TrendingContext = createContext<TrendingContext>({
17 enabled: false,
18})
19TrendingContext.displayName = 'TrendingContext'
20
21const LiveNowContext = createContext<LiveNowContext | null>(null)
22LiveNowContext.displayName = 'LiveNowContext'
23
24const CheckEmailConfirmedContext = createContext<boolean | null>(null)
25
26export function Provider({children}: {children: React.ReactNode}) {
27 const langPrefs = useLanguagePrefs()
28 const {data: config, isLoading: isInitialLoad} = useServiceConfigQuery()
29 const trending = useMemo<TrendingContext>(() => {
30 if (__DEV__) {
31 return {enabled: true}
32 }
33
34 /*
35 * Only English during beta period
36 */
37 if (
38 !!langPrefs.contentLanguages.length &&
39 !langPrefs.contentLanguages.includes('en')
40 ) {
41 return {enabled: false}
42 }
43
44 /*
45 * While loading, use cached value
46 */
47 const cachedEnabled = device.get(['trendingBetaEnabled'])
48 if (isInitialLoad) {
49 return {enabled: Boolean(cachedEnabled)}
50 }
51
52 /*
53 * Doing an extra check here to reduce hits to statsig. If it's disabled on
54 * the server, we can exit early.
55 */
56 const enabled = Boolean(config?.topicsEnabled)
57
58 // update cache
59 device.set(['trendingBetaEnabled'], enabled)
60
61 return {enabled}
62 }, [isInitialLoad, config, langPrefs.contentLanguages])
63
64 const liveNow = useMemo<LiveNowContext>(() => config?.liveNow ?? [], [config])
65
66 // probably true, so default to true when loading
67 // if the call fails, the query will set it to false for us
68 const checkEmailConfirmed = config?.checkEmailConfirmed ?? true
69
70 return (
71 <TrendingContext.Provider value={trending}>
72 <LiveNowContext.Provider value={liveNow}>
73 <CheckEmailConfirmedContext.Provider value={checkEmailConfirmed}>
74 {children}
75 </CheckEmailConfirmedContext.Provider>
76 </LiveNowContext.Provider>
77 </TrendingContext.Provider>
78 )
79}
80
81export function useTrendingConfig() {
82 return useContext(TrendingContext)
83}
84
85export function useLiveNowConfig() {
86 const ctx = useContext(LiveNowContext)
87 if (!ctx) {
88 throw new Error(
89 'useLiveNowConfig must be used within a ServiceConfigManager',
90 )
91 }
92 return ctx
93}
94
95export function useCanGoLive(did?: string) {
96 const config = useLiveNowConfig()
97 return !!config.find(cfg => cfg.did === did)
98}
99
100export function useCheckEmailConfirmed() {
101 const ctx = useContext(CheckEmailConfirmedContext)
102 if (ctx === null) {
103 throw new Error(
104 'useCheckEmailConfirmed must be used within a ServiceConfigManager',
105 )
106 }
107 return ctx
108}