Something went wrong. Try again.
Bluesky app fork with some witchin' additions 💫 forked from jollywhoppers.com/witchsky.app
Something went wrong. Try again.
witchsky.app modules expo-background-notification-handler src BackgroundNotificationHandlerProvider.tsx
2.0 kB · 70 lines
TSX
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import React from 'react'
import {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 = React.createContext<BackgroundNotificationPreferencesContext>( {} as BackgroundNotificationPreferencesContext,)export const useBackgroundNotificationPreferences = () => React.useContext(Context)
export function BackgroundNotificationPreferencesProvider({ children,}: { children: React.ReactNode}) { const [preferences, setPreferences] = React.useState<BackgroundNotificationHandlerPreferences>({ playSoundChat: true, })
React.useEffect(() => { ;(async () => { const prefs = await BackgroundNotificationHandler.getAllPrefsAsync() setPreferences(prefs) })() }, [])
const value = React.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>}