Skip to content

Commit

Permalink
fix: track banner once
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Oct 6, 2023
1 parent beac185 commit 3f52f1c
Showing 1 changed file with 67 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Chip, Grid, SvgIcon, Typography, IconButton } from '@mui/material'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useCallback, useEffect } from 'react'
import { useCallback, useEffect, useRef } from 'react'
import type { ReactElement } from 'react'

import { CustomTooltip } from '@/components/common/CustomTooltip'
Expand Down Expand Up @@ -89,6 +89,21 @@ const getSafesToRegister = (addedSafes: AddedSafesState, allPreferences: PushNot
}, {})
}

const TrackBanner = (): null => {
const hasTracked = useRef(false)

useEffect(() => {
if (hasTracked.current) {
return
}

trackEvent(PUSH_NOTIFICATION_EVENTS.DISPLAY_BANNER)
hasTracked.current = true
}, [])

return null
}

export const PushNotificationsBanner = ({ children }: { children: ReactElement }): ReactElement => {
const isNotificationsEnabled = useHasFeature(FEATURES.PUSH_NOTIFICATIONS)
const addedSafes = useAppSelector(selectAllAddedSafes)
Expand All @@ -110,12 +125,6 @@ export const PushNotificationsBanner = ({ children }: { children: ReactElement }
dismissPushNotificationBanner(safe.chainId)
}, [dismissPushNotificationBanner, safe.chainId])

useEffect(() => {
if (shouldShowBanner) {
trackEvent(PUSH_NOTIFICATION_EVENTS.DISPLAY_BANNER)
}
}, [dismissBanner, shouldShowBanner])

const onEnableAll = async () => {
if (!onboard) {
return
Expand Down Expand Up @@ -148,55 +157,58 @@ export const PushNotificationsBanner = ({ children }: { children: ReactElement }
}

return (
<CustomTooltip
className={css.banner}
title={
<Grid container className={css.container}>
<Grid item xs={3}>
<Chip label="New" className={css.chip} />
<SvgIcon component={PushNotificationIcon} inheritViewBox fontSize="inherit" className={css.icon} />
</Grid>
<Grid item xs={9}>
<Typography variant="subtitle2" fontWeight={700}>
Enable push notifications
</Typography>
<IconButton onClick={dismissBanner} className={css.close}>
<SvgIcon component={CloseIcon} inheritViewBox color="border" fontSize="small" />
</IconButton>
<Typography mt={0.5} mb={1.5} variant="body2">
Get notified about pending signatures, incoming and outgoing transactions and more when Safe{`{Wallet}`}{' '}
is in the background or closed.
</Typography>
<div className={css.buttons}>
{totalAddedSafes > 0 && (
<CheckWallet>
{(isOk) => (
<Button
variant="contained"
size="small"
className={css.button}
onClick={onEnableAll}
disabled={!isOk || !onboard}
>
Enable all
<>
<TrackBanner />
<CustomTooltip
className={css.banner}
title={
<Grid container className={css.container}>
<Grid item xs={3}>
<Chip label="New" className={css.chip} />
<SvgIcon component={PushNotificationIcon} inheritViewBox fontSize="inherit" className={css.icon} />
</Grid>
<Grid item xs={9}>
<Typography variant="subtitle2" fontWeight={700}>
Enable push notifications
</Typography>
<IconButton onClick={dismissBanner} className={css.close}>
<SvgIcon component={CloseIcon} inheritViewBox color="border" fontSize="small" />
</IconButton>
<Typography mt={0.5} mb={1.5} variant="body2">
Get notified about pending signatures, incoming and outgoing transactions and more when Safe{`{Wallet}`}{' '}
is in the background or closed.
</Typography>
<div className={css.buttons}>
{totalAddedSafes > 0 && (
<CheckWallet>
{(isOk) => (
<Button
variant="contained"
size="small"
className={css.button}
onClick={onEnableAll}
disabled={!isOk || !onboard}
>
Enable all
</Button>
)}
</CheckWallet>
)}
{safe && (
<Link passHref href={{ pathname: AppRoutes.settings.notifications, query }} onClick={onCustomize}>
<Button variant="outlined" size="small" className={css.button}>
Customize
</Button>
)}
</CheckWallet>
)}
{safe && (
<Link passHref href={{ pathname: AppRoutes.settings.notifications, query }} onClick={onCustomize}>
<Button variant="outlined" size="small" className={css.button}>
Customize
</Button>
</Link>
)}
</div>
</Link>
)}
</div>
</Grid>
</Grid>
</Grid>
}
open
>
<span>{children}</span>
</CustomTooltip>
}
open
>
<span>{children}</span>
</CustomTooltip>
</>
)
}

0 comments on commit 3f52f1c

Please sign in to comment.