Something went wrong. Try again.
Bluesky app fork with some witchin' additions 💫
Something went wrong. Try again.
witchsky.app modules expo-background-notification-handler src BackgroundNotificationHandlerProvider.tsx
2.0 kB · 69 lines
TSX
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import {createContext, useContext, useEffect, useMemo, useState} from 'react'
import {type BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotificationHandler.types'import {BackgroundNotificationHandler} from './ExpoBackgroundNotificationHandlerModule'
interface BackgroundNotificationPreferencesContext { preferences: BackgroundNotificationHandlerPreferences setPref: <Key extends keyof BackgroundNotificationHandlerPreferences>( key: Key, value: BackgroundNotificationHandlerPreferences[Key], ) => void}
const Context = createContext<BackgroundNotificationPreferencesContext>( {} as BackgroundNotificationPreferencesContext,)export const useBackgroundNotificationPreferences = () => useContext(Context)
export function BackgroundNotificationPreferencesProvider({ children,}: { children: React.ReactNode}) { const [preferences, setPreferences] = useState<BackgroundNotificationHandlerPreferences>({ playSoundChat: true, })
useEffect(() => { ;(async () => { const prefs = await BackgroundNotificationHandler.getAllPrefsAsync() setPreferences(prefs) })() }, [])
const value = useMemo( () => ({ preferences, setPref: async < Key extends keyof BackgroundNotificationHandlerPreferences, >( k: Key, v: BackgroundNotificationHandlerPreferences[Key], ) => { switch (typeof v) { case 'boolean': { await BackgroundNotificationHandler.setBoolAsync(k, v) break } case 'string': { await BackgroundNotificationHandler.setStringAsync(k, v) break } default: { throw new Error(`Invalid type for value: ${typeof v}`) } }
setPreferences(prev => ({ ...prev, [k]: v, })) }, }), [preferences], )
return <Context.Provider value={value}>{children}</Context.Provider>}