diff options
author | 2022-09-06 22:32:04 +0200 | |
---|---|---|
committer | 2022-09-08 01:24:50 +0200 | |
commit | 5f8f4e6484faff23821ca7c009e309382fba914d (patch) | |
tree | 6d399fb90b4aa3527c30c502ba474cd83b5c94b2 /subprojects/frontend/src/utils/useDelayedSnackbar.ts | |
parent | feat(frontend): handle page hide events (diff) | |
download | refinery-5f8f4e6484faff23821ca7c009e309382fba914d.tar.gz refinery-5f8f4e6484faff23821ca7c009e309382fba914d.tar.zst refinery-5f8f4e6484faff23821ca7c009e309382fba914d.zip |
feat(frontend): check for updates periodically
Diffstat (limited to 'subprojects/frontend/src/utils/useDelayedSnackbar.ts')
-rw-r--r-- | subprojects/frontend/src/utils/useDelayedSnackbar.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/subprojects/frontend/src/utils/useDelayedSnackbar.ts b/subprojects/frontend/src/utils/useDelayedSnackbar.ts new file mode 100644 index 00000000..03ad6caa --- /dev/null +++ b/subprojects/frontend/src/utils/useDelayedSnackbar.ts | |||
@@ -0,0 +1,39 @@ | |||
1 | import { | ||
2 | useSnackbar, | ||
3 | type SnackbarKey, | ||
4 | type SnackbarMessage, | ||
5 | type OptionsObject, | ||
6 | } from 'notistack'; | ||
7 | import { useCallback } from 'react'; | ||
8 | |||
9 | export default function useDelayedSnackbar( | ||
10 | defaultDelay = 0, | ||
11 | ): ( | ||
12 | message: SnackbarMessage, | ||
13 | options?: OptionsObject | undefined, | ||
14 | delay?: number | undefined, | ||
15 | ) => () => void { | ||
16 | const { enqueueSnackbar, closeSnackbar } = useSnackbar(); | ||
17 | return useCallback( | ||
18 | ( | ||
19 | message: SnackbarMessage, | ||
20 | options?: OptionsObject | undefined, | ||
21 | delay = defaultDelay, | ||
22 | ) => { | ||
23 | let key: SnackbarKey | undefined; | ||
24 | let timeout: number | undefined = setTimeout(() => { | ||
25 | timeout = undefined; | ||
26 | key = enqueueSnackbar(message, options); | ||
27 | }, delay); | ||
28 | return () => { | ||
29 | if (timeout !== undefined) { | ||
30 | clearTimeout(timeout); | ||
31 | } | ||
32 | if (key !== undefined) { | ||
33 | closeSnackbar(key); | ||
34 | } | ||
35 | }; | ||
36 | }, | ||
37 | [defaultDelay, enqueueSnackbar, closeSnackbar], | ||
38 | ); | ||
39 | } | ||