digitalfabrik/integreat-app

View on GitHub
native/src/components/AppStateListener.ts

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
import { ReactElement, useEffect } from 'react'
import { AppState } from 'react-native'

import { RESUME_SIGNAL_NAME, SUSPEND_SIGNAL_NAME } from 'shared'

import sendTrackingSignal from '../utils/sendTrackingSignal'

const AppStateListener = (): ReactElement | null => {
  const handleAppStateChange = (nextAppState: string) => {
    if (nextAppState === 'active') {
      sendTrackingSignal({
        signal: {
          name: RESUME_SIGNAL_NAME,
        },
      })
    } else if (nextAppState === 'background') {
      sendTrackingSignal({
        signal: {
          name: SUSPEND_SIGNAL_NAME,
        },
      })
    }
  }

  useEffect(() => AppState.addEventListener('change', handleAppStateChange).remove, [])

  return null
}

export default AppStateListener