-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifications): ensure course files notifs cleared on screen blur
- Loading branch information
1 parent
d381e41
commit e1f0726
Showing
4 changed files
with
48 additions
and
4 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,38 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import { useFocusEffect, useNavigation } from '@react-navigation/native'; | ||
|
||
/** | ||
* Runs `onLeave` when the user leaves the current screen, optionally checking | ||
* if the user spent at least `delay` ms in the screen | ||
*/ | ||
export const useOnLeaveScreen = ( | ||
/** | ||
* Callback | ||
*/ | ||
onLeave: () => void, | ||
/** | ||
* Don't fire the event if the user stayed in the page less than `delay` ms | ||
*/ | ||
delay = 2000, | ||
) => { | ||
const { addListener } = useNavigation(); | ||
const [focusedAt, setFocusedAt] = useState<number>(); | ||
|
||
useFocusEffect( | ||
useCallback(() => { | ||
if (!focusedAt) { | ||
setFocusedAt(+new Date()); | ||
} | ||
}, [focusedAt]), | ||
); | ||
|
||
useEffect(() => { | ||
return addListener('beforeRemove', () => { | ||
if (!delay || +new Date() - focusedAt! >= delay) { | ||
onLeave(); | ||
setFocusedAt(undefined); | ||
} | ||
}); | ||
}, [addListener, delay, focusedAt, onLeave]); | ||
}; |
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
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