Cache enabled value for reloads, use real data for service config
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
import {STALE} from '#/state/queries'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
|
|
||||||
type ServiceConfig = {
|
type ServiceConfig = {
|
||||||
@@ -11,16 +12,16 @@ type ServiceConfig = {
|
|||||||
export function useServiceConfigQuery() {
|
export function useServiceConfigQuery() {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
return useQuery<ServiceConfig>({
|
return useQuery<ServiceConfig>({
|
||||||
|
refetchOnWindowFocus: true,
|
||||||
|
staleTime: STALE.MINUTES.ONE,
|
||||||
queryKey: ['service-config'],
|
queryKey: ['service-config'],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
try {
|
try {
|
||||||
const {data} = await agent.api.app.bsky.unspecced.getConfig()
|
const {data} = await agent.api.app.bsky.unspecced.getConfig()
|
||||||
return {
|
return {
|
||||||
checkEmailConfirmed: Boolean(data.checkEmailConfirmed),
|
checkEmailConfirmed: Boolean(data.checkEmailConfirmed),
|
||||||
// TODO
|
trendingTopicsEnabled: Boolean(data.trendingTopicsEnabled),
|
||||||
trendingTopicsEnabled: true, // Boolean(data.trendingTopicsEnabled),
|
trendingTopicsLangs: (data.trendingTopicsLangs ?? []) as string[],
|
||||||
// TODO
|
|
||||||
trendingTopicsLangs: ['en'], // data.trendingTopicsLangs ?? [],
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return {
|
return {
|
||||||
@@ -30,6 +31,5 @@ export function useServiceConfigQuery() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
staleTime: 5 * 60 * 1000,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import React from 'react'
|
|||||||
|
|
||||||
import {useLanguagePrefs} from '#/state/preferences/languages'
|
import {useLanguagePrefs} from '#/state/preferences/languages'
|
||||||
import {useServiceConfigQuery} from '#/state/queries/service-config'
|
import {useServiceConfigQuery} from '#/state/queries/service-config'
|
||||||
|
import {device} from '#/storage'
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
enabled: boolean
|
enabled: boolean
|
||||||
@@ -13,17 +14,23 @@ const Context = React.createContext<Context>({
|
|||||||
|
|
||||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
const langPrefs = useLanguagePrefs()
|
const langPrefs = useLanguagePrefs()
|
||||||
const {data: config} = useServiceConfigQuery()
|
// refetches at most every minute
|
||||||
|
const {data: config, isLoading: isInitialLoad} = useServiceConfigQuery()
|
||||||
const ctx = React.useMemo<Context>(() => {
|
const ctx = React.useMemo<Context>(() => {
|
||||||
// TODO maybe default to true
|
// previously cached value
|
||||||
|
const cachedEnabled = device.get(['trendingBetaEnabled'])
|
||||||
|
if (isInitialLoad) {
|
||||||
|
return {enabled: Boolean(cachedEnabled)}
|
||||||
|
}
|
||||||
const serviceEnabled = Boolean(config?.trendingTopicsEnabled)
|
const serviceEnabled = Boolean(config?.trendingTopicsEnabled)
|
||||||
const languageIsSupported = langPrefs.contentLanguages.some(lang => {
|
const languageIsSupported = langPrefs.contentLanguages.some(lang => {
|
||||||
return (config?.trendingTopicsLangs ?? []).includes(lang)
|
return (config?.trendingTopicsLangs ?? []).includes(lang)
|
||||||
})
|
})
|
||||||
return {
|
const enabled = serviceEnabled && languageIsSupported
|
||||||
enabled: serviceEnabled && languageIsSupported,
|
// update cache
|
||||||
}
|
device.set(['trendingBetaEnabled'], enabled)
|
||||||
}, [config, langPrefs])
|
return {enabled}
|
||||||
|
}, [isInitialLoad, config, langPrefs])
|
||||||
return <Context.Provider value={ctx}>{children}</Context.Provider>
|
return <Context.Provider value={ctx}>{children}</Context.Provider>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,4 +8,5 @@ export type Device = {
|
|||||||
geolocation?: {
|
geolocation?: {
|
||||||
countryCode: string | undefined
|
countryCode: string | undefined
|
||||||
}
|
}
|
||||||
|
trendingBetaEnabled: boolean
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user