|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import styles from './styles.module.css'; |
| 3 | +import pkg from './../../../../package.json'; |
| 4 | +import Button from '../Button'; |
| 5 | +import Stack from '../Stack'; |
| 6 | +import ButtonGroup from '../ButtonGroup'; |
| 7 | + |
| 8 | +function StatusBarAutoUpdate() { |
| 9 | + const [autoUpdateMessage, setAutoUpdateMessage] = useState(''); |
| 10 | + const [autoUpdateProgress, setAutoUpdateProgress] = useState(0); |
| 11 | + const [downloadCompleted, setDownloadCompleted] = useState(false); |
| 12 | + const [showUpdateModal, setShowUpdateModal] = useState(false); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + window.electron.listenCheckingForUpdate(() => |
| 16 | + setAutoUpdateMessage('Checking for update...') |
| 17 | + ); |
| 18 | + |
| 19 | + window.electron.listenUpdateNotAvailable(() => { |
| 20 | + setAutoUpdateMessage('Up to date'); |
| 21 | + }); |
| 22 | + |
| 23 | + window.electron.listenUpdateAvailable(console.log); |
| 24 | + |
| 25 | + window.electron.listenUpdateDownloadProgress((_, e) => { |
| 26 | + setAutoUpdateProgress(e.percent); |
| 27 | + }); |
| 28 | + |
| 29 | + window.electron.listenUpdateDownloaded(() => { |
| 30 | + setDownloadCompleted(true); |
| 31 | + setShowUpdateModal(true); |
| 32 | + }); |
| 33 | + |
| 34 | + window.electron.checkForUpdates(); |
| 35 | + console.log('Check for update'); |
| 36 | + }, [setAutoUpdateMessage, setDownloadCompleted, setShowUpdateModal]); |
| 37 | + |
| 38 | + if (downloadCompleted) { |
| 39 | + return ( |
| 40 | + <> |
| 41 | + {showUpdateModal && ( |
| 42 | + <div className={styles.popup}> |
| 43 | + <Stack vertical spacing="md"> |
| 44 | + <h1>New Update!</h1> |
| 45 | + <p> |
| 46 | + There is new update. Restart QueryMaster to use the latest |
| 47 | + version. |
| 48 | + </p> |
| 49 | + |
| 50 | + <ButtonGroup> |
| 51 | + <Button |
| 52 | + primary |
| 53 | + onClick={() => window.electron.quitAndInstall()} |
| 54 | + > |
| 55 | + Restart |
| 56 | + </Button> |
| 57 | + <Button primary onClick={() => setShowUpdateModal(false)}> |
| 58 | + Later |
| 59 | + </Button> |
| 60 | + </ButtonGroup> |
| 61 | + </Stack> |
| 62 | + </div> |
| 63 | + )} |
| 64 | + <li>New update is available. Restart for update</li> |
| 65 | + </> |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if (autoUpdateProgress > 0) { |
| 70 | + return ( |
| 71 | + <li style={{ display: 'flex', gap: 10 }}> |
| 72 | + Downloading Update ({autoUpdateProgress.toFixed(0)}%) |
| 73 | + <div |
| 74 | + style={{ |
| 75 | + width: 150, |
| 76 | + background: '#fffa', |
| 77 | + height: '100%', |
| 78 | + }} |
| 79 | + > |
| 80 | + <div |
| 81 | + style={{ |
| 82 | + width: `${autoUpdateProgress}%`, |
| 83 | + background: '#16a085', |
| 84 | + height: '100%', |
| 85 | + }} |
| 86 | + ></div> |
| 87 | + </div> |
| 88 | + </li> |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + return <li>{autoUpdateMessage}</li>; |
| 93 | +} |
| 94 | + |
| 95 | +export default function StatusBar() { |
| 96 | + return ( |
| 97 | + <div className={styles.statusBarContainer}> |
| 98 | + <ul> |
| 99 | + <li>QueryMaster v{pkg.version}</li> |
| 100 | + <li style={{ flexGrow: 1 }}></li> |
| 101 | + <StatusBarAutoUpdate /> |
| 102 | + </ul> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
0 commit comments