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/RegisterServiceWorker.tsx | |
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/RegisterServiceWorker.tsx')
-rw-r--r-- | subprojects/frontend/src/RegisterServiceWorker.tsx | 86 |
1 files changed, 0 insertions, 86 deletions
diff --git a/subprojects/frontend/src/RegisterServiceWorker.tsx b/subprojects/frontend/src/RegisterServiceWorker.tsx deleted file mode 100644 index 5f46bc3d..00000000 --- a/subprojects/frontend/src/RegisterServiceWorker.tsx +++ /dev/null | |||
@@ -1,86 +0,0 @@ | |||
1 | import Button from '@mui/material/Button'; | ||
2 | import { | ||
3 | type OptionsObject as SnackbarOptionsObject, | ||
4 | useSnackbar, | ||
5 | } from 'notistack'; | ||
6 | import React, { useEffect } from 'react'; | ||
7 | // eslint-disable-next-line import/no-unresolved -- Importing virtual module. | ||
8 | import { registerSW } from 'virtual:pwa-register'; | ||
9 | |||
10 | import { ContrastThemeProvider } from './theme/ThemeProvider'; | ||
11 | import getLogger from './utils/getLogger'; | ||
12 | |||
13 | const log = getLogger('RegisterServiceWorker'); | ||
14 | |||
15 | function UpdateSnackbarActions({ | ||
16 | closeCurrentSnackbar, | ||
17 | enqueueSnackbar, | ||
18 | updateSW, | ||
19 | }: { | ||
20 | closeCurrentSnackbar: () => void; | ||
21 | enqueueSnackbar: ( | ||
22 | message: string, | ||
23 | options?: SnackbarOptionsObject | undefined, | ||
24 | ) => void; | ||
25 | updateSW: (reloadPage: boolean) => Promise<void>; | ||
26 | }): JSX.Element { | ||
27 | return ( | ||
28 | <ContrastThemeProvider> | ||
29 | <Button | ||
30 | color="primary" | ||
31 | onClick={() => { | ||
32 | closeCurrentSnackbar(); | ||
33 | updateSW(true).catch((error) => { | ||
34 | log.error('Failed to update service worker', error); | ||
35 | enqueueSnackbar('Failed to download update', { | ||
36 | variant: 'error', | ||
37 | }); | ||
38 | }); | ||
39 | }} | ||
40 | > | ||
41 | Reload | ||
42 | </Button> | ||
43 | <Button color="inherit" onClick={closeCurrentSnackbar}> | ||
44 | Dismiss | ||
45 | </Button> | ||
46 | </ContrastThemeProvider> | ||
47 | ); | ||
48 | } | ||
49 | |||
50 | export default function RegisterServiceWorker(): null { | ||
51 | const { enqueueSnackbar, closeSnackbar } = useSnackbar(); | ||
52 | useEffect(() => { | ||
53 | if (window.location.host === 'localhost') { | ||
54 | // Do not register service worker during local development. | ||
55 | return; | ||
56 | } | ||
57 | if (!('serviceWorker' in navigator)) { | ||
58 | log.debug('No service worker support found'); | ||
59 | return; | ||
60 | } | ||
61 | const updateSW = registerSW({ | ||
62 | onNeedRefresh() { | ||
63 | const key = enqueueSnackbar('An update for Refinery is available', { | ||
64 | persist: true, | ||
65 | action: ( | ||
66 | <UpdateSnackbarActions | ||
67 | closeCurrentSnackbar={() => closeSnackbar(key)} | ||
68 | enqueueSnackbar={enqueueSnackbar} | ||
69 | updateSW={updateSW} | ||
70 | /> | ||
71 | ), | ||
72 | }); | ||
73 | }, | ||
74 | onOfflineReady() { | ||
75 | log.debug('Service worker is ready for offline use'); | ||
76 | }, | ||
77 | onRegistered() { | ||
78 | log.debug('Registered service worker'); | ||
79 | }, | ||
80 | onRegisterError(error) { | ||
81 | log.error('Failed to register service worker', error); | ||
82 | }, | ||
83 | }); | ||
84 | }, [enqueueSnackbar, closeSnackbar]); | ||
85 | return null; | ||
86 | } | ||