Skip to content

Commit

Permalink
feature: add update modal and block system modal
Browse files Browse the repository at this point in the history
  • Loading branch information
maparr committed Dec 20, 2023
1 parent dbeb4c1 commit 813165c
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 18 deletions.
38 changes: 37 additions & 1 deletion app/components/common/Version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { getNetworkInfo } from '../../redux/network/selectors';
import { checkUpdates as checkUpdatesIco } from '../../assets/images';
import { AppThDispatch } from '../../types';
import updaterSlice from '../../redux/updater/slice';
import { SECOND } from '../../../shared/constants';
import { Loader } from '../../basicComponents';
import UpdateApplicationWarningModal from '../../screens/modal/UpdateApplicationWarningModal';
import FeedbackButton from './Feedback';

const Container = styled.div`
Expand Down Expand Up @@ -194,6 +197,29 @@ const UpdateStatus = () => {
const isDownloading = useSelector(isUpdateDownloading);
const isDownloaded = useSelector(isUpdateDownloaded);
const error = useSelector(getError);
const [
isOpenUpdateApplicationWarningModal,
setIsOpenUpdateApplicationWarningModal,
] = useState(false);
const [
showUpdateApplicationLoader,
setShowUpdateApplicationLoader,
] = useState(false);

const handleRestartNow = () => {
setIsOpenUpdateApplicationWarningModal(false);
setShowUpdateApplicationLoader(true);
eventsService.installUpdate();

setTimeout(() => {
setShowUpdateApplicationLoader(false);
}, 10 * SECOND);
};

const handlePostpone = () => {
setIsOpenUpdateApplicationWarningModal(false);
};

if (!isDownloading && !isDownloaded) return null;

if (progress !== null && !isDownloaded) {
Expand All @@ -207,9 +233,19 @@ const UpdateStatus = () => {
return (
<>
<ProgressChunk>Update is ready to install</ProgressChunk>
<PrimaryAction onClick={() => eventsService.installUpdate()}>
<PrimaryAction
onClick={() => setIsOpenUpdateApplicationWarningModal(true)}
>
Restart Smapp
</PrimaryAction>
<UpdateApplicationWarningModal
isOpen={isOpenUpdateApplicationWarningModal}
onApprove={handleRestartNow}
onCancel={handlePostpone}
/>
{showUpdateApplicationLoader && (
<Loader size={Loader.sizes.BIG} note="UPDATE IN PROGESS..." />
)}
</>
);
}
Expand Down
67 changes: 67 additions & 0 deletions app/screens/modal/UpdateApplicationWarningModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import styled from 'styled-components';
import Modal from '../../components/common/Modal';
import { Button } from '../../basicComponents';
import { smColors } from '../../vars';

const ButtonsWrapper = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
margin: auto 0 15px 0;
padding-top: 30px;
`;

const Message = styled.pre`
font-size: 14px;
line-height: 1.33em;
word-wrap: break-word;
white-space: pre-wrap;
overflow-y: auto;
margin-top: 15px;
ul {
list-style: none;
margin-left: 10px;
}
li {
margin: 10px 0;
}
li:before {
content: '• ';
padding: 5px;
}
`;

const UpdateApplicationWarningModal = ({ isOpen, onApprove, onCancel }) => {
if (!isOpen) return null;

return (
<Modal header="Update SMAPP" height={380}>
<Message>
<p>
Restarting now is <b>CRITICAL</b> and may impact your node’s
performance and rewards.
</p>
<ul>
<li>
Click <b style={{ color: smColors.green }}>RESTART NOW</b> to apply
the update immediately. Delaying the update could result in
potential loss of rewards.
</li>
<li>
Click <b style={{ color: smColors.purple }}>POSTPONE</b> to delay
the update. Be aware that postponing may slow down your node’s
performance and future rewards.
</li>
</ul>
</Message>
<ButtonsWrapper>
<Button onClick={onCancel} isPrimary={false} text="POSTPONE" />
<Button onClick={onApprove} text="RESTART NOW" />
</ButtonsWrapper>
</Modal>
);
};

export default UpdateApplicationWarningModal;
1 change: 0 additions & 1 deletion desktop/main/autoUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export const checkUpdates = async (
autoUpdater.setFeedURL(feedUrl);
try {
const result = await autoUpdater.checkForUpdates();

if (result) {
return {
status: UpdateInfoStatus.UpdateAvailable,
Expand Down
6 changes: 4 additions & 2 deletions desktop/main/createMainWindow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { app, BrowserWindow, ipcMain } from 'electron';
import { autoUpdater } from 'electron-updater';
import { app, BrowserWindow, ipcMain, autoUpdater } from 'electron';
import {
BehaviorSubject,
combineLatest,
Expand Down Expand Up @@ -35,13 +34,15 @@ export default () => {
const $quit = new Subject<Electron.IpcMainEvent>();
const $activate = fromAppEvent('activate');
const $secondInstance = fromAppEvent('second-instance');
const $isUpdateInProgress = new BehaviorSubject<boolean>(false);

//
// Subscriptions & Reactions
//

// Push to $quit event stream
const handleQuit = async (event) => {
logger.log('handleQuit', $isAppClosing.value);
if (!$isAppClosing.value) {
$quit.next(event);
}
Expand Down Expand Up @@ -84,5 +85,6 @@ export default () => {
$showWindowOnLoad,
$isWindowReady,
$isSmappActivated: $activate,
$isUpdateInProgress,
};
};
9 changes: 6 additions & 3 deletions desktop/main/promptBeforeClose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const promptBeforeClose = (
mainWindow: BrowserWindow,
managers: Partial<Managers>,
$isAppClosing: BehaviorSubject<boolean>,
$showWindowOnLoad: Subject<boolean>
$showWindowOnLoad: Subject<boolean>,
$isUpdateInProgress: BehaviorSubject<boolean>
) => {
const showPrompt = async () => {
if (!mainWindow) return CloseAppPromptResult.KEEP_SMESHING;
Expand Down Expand Up @@ -64,11 +65,13 @@ const promptBeforeClose = (
};

const handleClosingApp = async (event: Electron.Event) => {
event.preventDefault();
if (!mainWindow) {
event?.preventDefault();

if ($isUpdateInProgress.value || !mainWindow) {
await quit();
return;
}

if (isCloseTriggered) {
mainWindow.hide();
return;
Expand Down
8 changes: 5 additions & 3 deletions desktop/main/reactions/handleCloseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ export default (
$managers: Subject<Managers>,
$mainWindow: Subject<BrowserWindow>,
$isAppClosing: BehaviorSubject<boolean>,
$showWindowOnLoad: Subject<boolean>
$showWindowOnLoad: Subject<boolean>,
$isUpdateInProgress: BehaviorSubject<boolean>
) =>
makeSubscription(
$quit.pipe(withLatestFrom($mainWindow, $managers)),
$quit.pipe(withLatestFrom($mainWindow, $managers, $isUpdateInProgress)),
([event, mw, managers]) =>
mw &&
promptBeforeClose(
mw,
managers || {},
$isAppClosing,
$showWindowOnLoad
$showWindowOnLoad,
$isUpdateInProgress
)(event).catch((err) => logger.error('promptBeforeClose', err))
);
12 changes: 8 additions & 4 deletions desktop/main/sources/autoUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserWindow } from 'electron';
import { UpdateInfo } from 'electron-updater';
import {
BehaviorSubject,
combineLatest,
filter,
first,
Expand Down Expand Up @@ -40,7 +41,8 @@ const handleAutoUpdates = (
everyMs: number,
$mainWindow: Observable<BrowserWindow>,
$managers: Observable<Managers>,
$currentNetwork: Observable<Network | null>
$currentNetwork: Observable<Network | null>,
$isUpdateInProgress: BehaviorSubject<boolean>
) => {
type DoDownload = boolean;
type Data = [BrowserWindow, Network, DoDownload];
Expand Down Expand Up @@ -107,6 +109,7 @@ const handleAutoUpdates = (
StoreService.set('startNodeOnNextLaunch', true);
await managers.node.stopNode();
}

installUpdate();
} catch (err) {
logger.error('forceUpdate', err);
Expand Down Expand Up @@ -136,9 +139,10 @@ const handleAutoUpdates = (
$request.next(true);
}),
// Trigger installation
fromIPC<void>(ipcConsts.AU_REQUEST_INSTALL).subscribe(() =>
installUpdate()
),
fromIPC<void>(ipcConsts.AU_REQUEST_INSTALL).subscribe(() => {
$isUpdateInProgress.next(true);
return installUpdate();
}),
];

return () => subs.forEach((sub) => sub.unsubscribe());
Expand Down
7 changes: 5 additions & 2 deletions desktop/main/startApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const startApp = (): AppStore => {
$isAppClosing,
$showWindowOnLoad,
$isWindowReady,
$isUpdateInProgress,
} = createMainWindow();
// Store
const $storeService = observeStoreService();
Expand Down Expand Up @@ -218,7 +219,8 @@ const startApp = (): AppStore => {
$managers,
$mainWindow,
$isAppClosing,
$showWindowOnLoad
$showWindowOnLoad,
$isUpdateInProgress
),
// Unlock / Create wallet
// Switch network
Expand Down Expand Up @@ -264,7 +266,8 @@ const startApp = (): AppStore => {
CHECK_UPDATES_INTERVAL,
$mainWindow,
$managers,
$currentNetwork
$currentNetwork,
$isUpdateInProgress
),
handleOpenDashboard($mainWindow, $currentNetwork),
sendWarningsToRenderer($warnings, $mainWindow, $isWindowReady),
Expand Down
4 changes: 2 additions & 2 deletions shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export enum ExternalLinks {
}

export const BITS_PER_LABEL = 128;

export const MINUTE = 60 * 1000;
export const SECOND = 1000;
export const MINUTE = 60 * SECOND;

export const HOUR = MINUTE * 60;

Expand Down

0 comments on commit 813165c

Please sign in to comment.