aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/UpdateNotification.tsx
blob: d86c0703092d7b97c3674d4771bb28b4856e0961 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import Button from '@mui/material/Button';
import { observer } from 'mobx-react-lite';
import { useEffect } from 'react';

import { useRootStore } from './RootStoreProvider';
import { ContrastThemeProvider } from './theme/ThemeProvider';
import useDelayedSnackbar from './utils/useDelayedSnackbar';

export default observer(function UpdateNotification(): null {
  const { pwaStore } = useRootStore();
  const { needsUpdate, updateError } = pwaStore;
  const enqueueLater = useDelayedSnackbar();

  useEffect(() => {
    if (needsUpdate) {
      return enqueueLater('An update for Refinery is available', {
        persist: true,
        action: (
          <ContrastThemeProvider>
            <Button color="primary" onClick={() => pwaStore.reloadWithUpdate()}>
              Reload
            </Button>
            <Button color="inherit" onClick={() => pwaStore.dismissUpdate()}>
              Dismiss
            </Button>
          </ContrastThemeProvider>
        ),
      });
    }

    if (updateError) {
      return enqueueLater('Failed to download update', {
        variant: 'error',
        action: (
          <ContrastThemeProvider>
            <Button color="inherit" onClick={() => pwaStore.checkForUpdates()}>
              Try again
            </Button>
            <Button color="inherit" onClick={() => pwaStore.dismissError()}>
              Dismiss
            </Button>
          </ContrastThemeProvider>
        ),
      });
    }

    return () => {};
  }, [pwaStore, needsUpdate, updateError, enqueueLater]);

  return null;
});