-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #686 from near/fix/notifications-infinite-nan-loop
Fix Notifications Infinite "NaN" Loop
- Loading branch information
Showing
7 changed files
with
176 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import { VmComponent } from '@/components/vm/VmComponent'; | ||
import { useBosComponents } from '@/hooks/useBosComponents'; | ||
import { useIosDevice } from '@/hooks/useIosDevice'; | ||
import { useAuthStore } from '@/stores/auth'; | ||
import { | ||
handleOnCancel, | ||
handleTurnOn, | ||
recommendedIosVersionForNotifications, | ||
showNotificationModal, | ||
} from '@/utils/notifications'; | ||
import { isNotificationSupported, isPermisionGranted, isPushManagerSupported } from '@/utils/notificationsHelpers'; | ||
import { getNotificationLocalStorage, setNotificationsSessionStorage } from '@/utils/notificationsLocalStorage'; | ||
import type { TosData } from '@/utils/types'; | ||
|
||
type Props = { | ||
tosData: TosData | null; | ||
}; | ||
|
||
export const NotificationsAlert = ({ tosData }: Props) => { | ||
const signedIn = useAuthStore((store) => store.signedIn); | ||
const components = useBosComponents(); | ||
const [showNotificationModalState, setShowNotificationModalState] = useState(false); | ||
const accountId = useAuthStore((store) => store.accountId); | ||
const [isHomeScreenApp, setHomeScreenApp] = useState(false); | ||
const [iosHomeScreenPrompt, setIosHomeScreenPrompt] = useState(false); | ||
const { isIosDevice, versionOfIos } = useIosDevice(); | ||
|
||
const handleModalCloseOnEsc = useCallback(() => { | ||
setShowNotificationModalState(false); | ||
}, []); | ||
|
||
const handleHomeScreenClose = useCallback(() => { | ||
setIosHomeScreenPrompt(false); | ||
}, []); | ||
|
||
const turnNotificationsOn = useCallback(() => { | ||
// for iOS devices, show a different modal asking the user to add the app to their home screen | ||
// if the user has already added the app to their home screen, show the regular notification modal | ||
if (isIosDevice && !isHomeScreenApp) { | ||
setIosHomeScreenPrompt(true); | ||
setShowNotificationModalState(false); | ||
return; | ||
} | ||
return handleTurnOn(accountId, () => { | ||
setShowNotificationModalState(false); | ||
}); | ||
}, [accountId, isIosDevice, isHomeScreenApp]); | ||
|
||
const pauseNotifications = useCallback(() => { | ||
handleOnCancel(); | ||
setShowNotificationModalState(false); | ||
}, []); | ||
|
||
const checkNotificationModal = useCallback(() => { | ||
if (tosData && tosData.agreementsForUser.length > 0) { | ||
// show notification modal for new users | ||
const tosAccepted = | ||
tosData.agreementsForUser[tosData.agreementsForUser.length - 1].value === tosData.latestTosVersion; | ||
// check if user has already turned on notifications | ||
const { showOnTS } = getNotificationLocalStorage() || {}; | ||
|
||
if (!iosHomeScreenPrompt && ((tosAccepted && !showOnTS) || (tosAccepted && showOnTS < Date.now()))) { | ||
setTimeout(() => { | ||
setShowNotificationModalState(showNotificationModal()); | ||
}, 3000); | ||
} | ||
} | ||
}, [tosData, iosHomeScreenPrompt]); | ||
|
||
useEffect(() => { | ||
if (!signedIn) { | ||
return; | ||
} | ||
checkNotificationModal(); | ||
}, [signedIn, checkNotificationModal]); | ||
|
||
useEffect(() => { | ||
if (isIosDevice) { | ||
setHomeScreenApp(window.matchMedia('(display-mode: standalone)').matches); | ||
} | ||
}, [isIosDevice]); | ||
|
||
useEffect(() => { | ||
if (isIosDevice) { | ||
window.matchMedia('(display-mode: standalone)').addEventListener('change', (e) => setHomeScreenApp(e.matches)); | ||
// TODO: Remove event listener on cleanup | ||
} | ||
}, [isIosDevice]); | ||
|
||
if (!signedIn) return null; | ||
|
||
return ( | ||
<> | ||
<VmComponent | ||
src={components.nearOrg.notifications.alert} | ||
props={{ | ||
open: showNotificationModalState, | ||
handleTurnOn: turnNotificationsOn, | ||
handleOnCancel: pauseNotifications, | ||
isNotificationSupported, | ||
isPermisionGranted, | ||
isPushManagerSupported, | ||
setNotificationsSessionStorage, | ||
onOpenChange: handleModalCloseOnEsc, | ||
iOSDevice: isIosDevice, | ||
iOSVersion: versionOfIos, | ||
recomendedIOSVersion: recommendedIosVersionForNotifications, | ||
}} | ||
/> | ||
<VmComponent | ||
src={components.nearOrg.notifications.iosHomeScreenAlert} | ||
props={{ | ||
open: iosHomeScreenPrompt, | ||
onOpenChange: handleHomeScreenClose, | ||
}} | ||
/> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function detectIos() { | ||
if (typeof window === 'undefined') return; | ||
|
||
const isIosDevice = /iP(hone|od|ad)/.test(navigator.userAgent); | ||
let versionOfIos = 0; | ||
|
||
const versionMatch = navigator.userAgent.match(/OS (\d+)_(\d+)_?(\d+)?/); | ||
|
||
if (versionMatch) { | ||
versionOfIos = Number(`${versionMatch[1]}.${versionMatch[2]}`); | ||
|
||
if (isNaN(versionOfIos)) { | ||
/* | ||
We need to ensure that we never return "NaN". It isn't referentially stable and | ||
causes infinite loops inside logic like useEffect(). | ||
*/ | ||
versionOfIos = 0; | ||
} | ||
} | ||
|
||
return { | ||
isIosDevice, | ||
versionOfIos, | ||
}; | ||
} | ||
|
||
export function useIosDevice() { | ||
const result = detectIos(); | ||
|
||
return { | ||
isIosDevice: result?.isIosDevice, | ||
versionOfIos: result?.versionOfIos, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.