import React , { FC , createContext , useMemo , useContext } from 'react' ; import { useTranslation } from 'react-i18next' ; import { useExposure , StatusState } from 'react-native-exposure-notification-service' ; import { pauseNotification } from '../../services/notifications/pause' ; import { ReminderState , useReminder } from './reminder' ; export interface PauseValue { paused : boolean ; timestamp ? : ReminderState [ 'timestamp' ] ; checked : boolean ; pause : ( date : Date ) => Promise < void > ; unpause : ( ) => Promise < void > ; clear : ( ) => void ; } const initialPauseValue = { paused : false , checked : false , pause : async ( ) => { } , unpause : async ( ) => { } , clear : ( ) => { } } ; const PauseContext = createContext ( initialPauseValue ) ; export const PauseProvider = ( { children } ) => { const { t } = useTranslation ( ) ; const exposure = useExposure ( ) ; const { reminderState , dispatchReminder } = useReminder ( pauseNotification ) ; const pauseValue = useMemo ( ( ) => ( { paused : reminderState . active && exposure . status . state !== StatusState . active , timestamp : reminderState . timestamp , checked : reminderState . checked , pause : async ( date ) => { const timestamp = date . getTime ( ) ; dispatchReminder ( { type : 'set' , t , timestamp } ) ; await exposure . pause ( ) ; } , unpause : async ( ) => { dispatchReminder ( { type : 'delete' } ) ; await exposure . start ( ) ; } , clear : ( ) => { dispatchReminder ( { type : 'delete' } ) ; } } as PauseValue ) , [ reminderState , dispatchReminder , t , exposure . status . state ] ) ; return ( < PauseContext . Provider value = { pauseValue } > { children } < /PauseContext.Provider>); } ; export const usePause = ( ) => useContext ( PauseContext ) ;