Skip to content

Commit

Permalink
Implement NotificationServices
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-edge committed Nov 1, 2024
1 parent d22b526 commit 6c9b41a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/components/services/NotificationServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// import * as React from 'react'

import { createDeviceNotifInfo, getDeviceSettings } from '../../actions/DeviceSettingsActions'
import { getLocalAccountSettings } from '../../actions/LocalSettingsActions'
import { useAsyncEffect } from '../../hooks/useAsyncEffect'
import { useWatch } from '../../hooks/useWatch'
import { useSelector } from '../../types/reactRedux'
import { getOtpReminderModal } from '../../util/otpReminder'

/**
* Checks for new notifications that have not yet been saved to `deviceNotifications`
*/
export const NotificationServices = () => {
const { deviceNotifState } = getDeviceSettings()
const accountNotifDismissInfo = getLocalAccountSettings().accountNotifDismissInfo

const account = useSelector(state => state.core.account)
const isLightAccountReminder = account.id != null && account.username == null
const detectedTokensRedux = useSelector(state => state.core.enabledDetectedTokens)
const isPwReminder = useSelector(state => state.ui.passwordReminder.needsPasswordCheck)

const wallets = useWatch(account, 'currencyWallets')
const otpKey = useWatch(account, 'otpKey')

// Either create new notification info or update the completed notification
// info with the latest date received
useAsyncEffect(
async () => {
// New token(s) detected
Object.keys(wallets).forEach(async walletId => {
const newTokenKey = `newToken-${walletId}`
const newTokenIds = detectedTokensRedux[walletId]

if (newTokenIds != null && newTokenIds.length > 0 && deviceNotifState[newTokenKey] == null) {
// Only happens once per notification info key.
// Don't need to update existing, only create new ones with the
// default props.
await createDeviceNotifInfo(newTokenKey)
}
})

// 2FA/OTP Reminder
const isOtpReminder = (await getOtpReminderModal(account)) != null
const isOtpReminderStale = deviceNotifState.otpReminder == null || deviceNotifState.otpReminder.isCompleted
if (isOtpReminder && isOtpReminderStale) {
await createDeviceNotifInfo('otpReminder', {
isPriority: true
})
}

// PW Reminder
const isPwReminderStale = deviceNotifState.pwReminder == null || deviceNotifState.pwReminder.isCompleted
if (isPwReminder && isPwReminderStale) {
await createDeviceNotifInfo('pwReminder', {
isPriority: true
})
}

// Light Account Backup
const isLightAccountStale = deviceNotifState.lightAccount == null || deviceNotifState.lightAccount.isCompleted
if (isLightAccountReminder && isLightAccountStale) {
await createDeviceNotifInfo('lightAccountReminder', {
isPriority: true
})
}

// IP 2FA
const isIp2FaReminder = !isLightAccountReminder && otpKey == null && accountNotifDismissInfo != null && !accountNotifDismissInfo.ip2FaNotifShown
const isIp2FaStale = deviceNotifState.ip2FaReminder == null || deviceNotifState.ip2FaReminder.isCompleted
if (isIp2FaReminder && isIp2FaStale) {
await createDeviceNotifInfo('ip2FaReminder', {
isPriority: true
})
}

// cleanup:
return () => {}
},
[account, accountNotifDismissInfo, detectedTokensRedux, deviceNotifState, otpKey, wallets],
'NotificationServices'
)

return null
}
2 changes: 2 additions & 0 deletions src/components/services/Services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { EdgeContextCallbackManager } from './EdgeContextCallbackManager'
import { FioService } from './FioService'
import { LoanManagerService } from './LoanManagerService'
import { NetworkActivity } from './NetworkActivity'
import { NotificationServices } from './NotificationServices'
import { PasswordReminderService } from './PasswordReminderService'
import { PermissionsManager } from './PermissionsManager'
import { SortedWalletList } from './SortedWalletList'
Expand Down Expand Up @@ -188,6 +189,7 @@ export function Services(props: Props) {
return (
<>
{ENV.BETA_FEATURES ? <ActionQueueService /> : null}
<NotificationServices />
<AutoLogout />
<ContactsLoader />
{account == null ? null : <AccountCallbackManager account={account} navigation={navigation} />}
Expand Down

0 comments on commit 6c9b41a

Please sign in to comment.