Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import CitationModal from './CitationModal';
import ErrorBoundary from './components/ErrorBoundary';
import ErrorDialog from './components/ErrorDialog';
import ModifiedDialog from './components/ModifiedDialog';
import UpdateDownloadDialog from './components/UpdateDownloadDialog';

const VerticalHeading = styled( Typography )( () => ( {
writingMode: 'vertical-rl',
Expand Down Expand Up @@ -522,6 +523,7 @@ const App = (): React.JSX.Element => {
</Modal>
)
: null}
<UpdateDownloadDialog download={store.updateDownload} />
<ErrorDialog error={store.error} title={store.errorTitle} onClose={store.clearError} />
</Box>
</React.Fragment>
Expand Down
54 changes: 54 additions & 0 deletions src/app/components/UpdateDownloadDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import LinearProgress from '@mui/material/LinearProgress';
import Typography from '@mui/material/Typography';
import type { UpdateDownload } from '../../common/rendererBridge';

export interface UpdateDownloadDialogProps {
download: UpdateDownload;
}

function dialogCopy( download: UpdateDownload ): { percent?: number; text: string; detail: string } {
switch ( download.status ) {
case 'connecting':
return { text: 'Starting…', detail: 'Connecting to the update server…' };
case 'downloading':
case 'restarted':
if ( download.percent < 100 ) {
return {
percent: download.percent,
text: `${Math.round( download.percent )}%`,
detail: download.status === 'restarted'
? 'Retrying with the full download…'
: 'RAxML-GUI will quit and install when the download finishes.',
};
}
break;
default:
}
return { percent: 100, text: '100%', detail: 'Download complete. Preparing to install…' };
}

export default function UpdateDownloadDialog( { download }: UpdateDownloadDialogProps ): React.JSX.Element | null {
if ( download.status === 'idle' ) {
return null;
}
const { percent, text, detail } = dialogCopy( download );
return (
<Dialog open={true} maxWidth="xs" fullWidth disableEscapeKeyDown aria-labelledby="update-download-title">
<DialogTitle id="update-download-title">Downloading update</DialogTitle>
<DialogContent>
<LinearProgress
variant={percent === undefined
? 'indeterminate'
: 'determinate'}
value={percent}
/>
<Typography variant="body2" sx={{ mt: 1 }}>{text}</Typography>
<Typography variant="body2" color="text.secondary">{detail}</Typography>
</DialogContent>
</Dialog>
);
}
10 changes: 10 additions & 0 deletions src/app/store/RunList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { observable, computed, action, makeObservable } from 'mobx';
import type { StateReport } from '../../main/getActiveState';
import type { UpdateDownload } from '../../common/rendererBridge';
import Run from './Run';
import AppStore from './AppStore';
import { runEvents } from './StoreBase';
Expand All @@ -23,6 +24,8 @@ class RunList extends AppStore {
deleteActive: action,
onError: action,
onUnhandledError: action,
updateDownload: observable,
onUpdateDownload: action,
onBackboneConstraint: action,
onMultifurcatingConstraint: action,
} );
Expand Down Expand Up @@ -77,6 +80,12 @@ class RunList extends AppStore {
this.error = error;
};

updateDownload: UpdateDownload = { status: 'idle' };

onUpdateDownload = ( download: UpdateDownload ): void => {
this.updateDownload = download;
};

onBackboneConstraint = (): void => {
this.activeRun.useBackboneConstraint =
!this.activeRun.useBackboneConstraint;
Expand All @@ -89,6 +98,7 @@ class RunList extends AppStore {

listen = (): void => {
this.subscribe( runEvents.onUnhandledError, this.onUnhandledError );
this.subscribe( runEvents.onUpdateDownload, this.onUpdateDownload );
this.subscribe( runEvents.onAddRun, this.addRun );
this.subscribe( runEvents.onRemoveRun, this.deleteActive );
this.subscribe( runEvents.onToggleBackboneConstraint, this.onBackboneConstraint );
Expand Down
1 change: 1 addition & 0 deletions src/app/store/StoreBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const runEvents = {
onToggleBackboneConstraint: 'onToggleBackboneConstraint',
onToggleMultifurcatingConstraint: 'onToggleMultifurcatingConstraint',
onUnhandledError: 'onUnhandledError',
onUpdateDownload: 'onUpdateDownload',
} as const;

export default class StoreBase {
Expand Down
67 changes: 67 additions & 0 deletions src/app/updateDownload.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect, it } from 'vitest';
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import Page from './index';
import store from './store';
import { installBridge } from '../test/setup';
import type { UpdateDownloadPayload } from '../common/rendererBridge';

function title() {
return document.getElementById( 'update-download-title' )?.textContent;
}

it( 'the page follows the update download in a dialog that cannot be dismissed', () => {
let hear: ( payload: UpdateDownloadPayload ) => void;
installBridge( {
onUpdateDownload( listener ) {
hear = listener;
return () => {};
},
} );
store.listen();

const div = document.createElement( 'div' );
document.body.appendChild( div );
const root = createRoot( div );
act( () => {
root.render( <Page /> );
} );
expect( title() ).toBeUndefined();

const rows: [UpdateDownloadPayload, string, string][] = [
[{ id: 'update', status: 'connecting' }, 'Starting…', 'Connecting to the update server…'],
[
{ id: 'update', status: 'downloading', percent: 42 },
'42%',
'RAxML-GUI will quit and install when the download finishes.',
],
[{ id: 'update', status: 'restarted', percent: 10 }, '10%', 'Retrying with the full download…'],
[{ id: 'update', status: 'done' }, '100%', 'Download complete. Preparing to install…'],
];
for ( const [payload, percent, status] of rows ) {
act( () => {
hear( payload );
} );
expect( title() ).toBe( 'Downloading update' );
const dialog = document.querySelector( '[role="dialog"]' );
expect( dialog.textContent ).toContain( percent );
expect( dialog.textContent ).toContain( status );
expect( dialog.querySelector( 'button' ) ).toBeNull();
}

act( () => {
document.querySelector( '[role="dialog"]' ).dispatchEvent( new KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
document.querySelector<HTMLElement>( '.MuiBackdrop-root' ).click();
} );
expect( title() ).toBe( 'Downloading update' );

act( () => {
hear( { id: 'update', status: 'idle' } );
} );
expect( title() ).toBeUndefined();

act( () => {
root.unmount();
} );
div.remove();
} );
3 changes: 3 additions & 0 deletions src/common/rendererBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type UpdateDownload =
| { status: 'restarted'; percent: number }
| { status: 'done' };

export type UpdateDownloadPayload = UpdateDownload & { id: string };

// An event payload the page does not read yet; the leaf that reads it tightens it.
type Listener = ( payload: unknown ) => void;
type Unsubscribe = () => void;
Expand Down Expand Up @@ -76,6 +78,7 @@ export interface RendererBridge {
onToggleBackboneConstraint: ( listener: Listener ) => Unsubscribe;
onToggleMultifurcatingConstraint: ( listener: Listener ) => Unsubscribe;
onUnhandledError: ( listener: Listener ) => Unsubscribe;
onUpdateDownload: ( listener: ( payload: UpdateDownloadPayload ) => void ) => Unsubscribe;
getPathForFile: ( file: File ) => string;
}

Expand Down
1 change: 1 addition & 0 deletions src/constants/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const ALIGNMENT_MODEL_SELECTION_SUCCESS =
export const ALIGNMENT_MODEL_SELECTION_FAILURE =
'ALIGNMENT_MODEL_SELECTION_FAILURE' as const;
export const UNHANDLED_ERROR = 'UNHANDLED_ERROR' as const;
export const UPDATE_DOWNLOAD = 'UPDATE_DOWNLOAD' as const;
export const TOGGLE_BACKBONE_CONSTRAINT = 'TOGGLE_BACKBONE_CONSTRAINT' as const;
export const TOGGLE_MULTIFURCATING_CONSTRAINT =
'TOGGLE_MULTIFURCATING_CONSTRAINT' as const;
Expand Down
25 changes: 23 additions & 2 deletions src/main/updates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ function updater( download: () => Promise<unknown> = () => Promise.resolve( [] )
}

function fakeWindow() {
return { setProgressBar: vi.fn<( value: number ) => void>() };
return {
setProgressBar: vi.fn<( value: number ) => void>(),
webContents: { send: vi.fn<( channel: string, payload: { status: string } ) => void>() },
};
}

function progress( percent: number ) {
Expand All @@ -60,6 +63,9 @@ async function startDownload( options: {
win?.setProgressBar.mockImplementation( ( value ) => {
calls.push( `bar ${value}` );
} );
win?.webContents.send.mockImplementation( ( _channel, payload ) => {
calls.push( `send ${payload.status}` );
} );
const choice = Promise.resolve( { response: 0 } );
const dialog = {
showMessageBox: vi.fn<( options: unknown ) => Promise<{ response: number }>>( () => choice ),
Expand Down Expand Up @@ -112,6 +118,21 @@ it( 'Yes downloads the update and shows its progress on the icon', async () => {
expect( dialog.showErrorBox ).not.toHaveBeenCalled();
} );

it( 'every state change is sent to the page', async () => {
const { updates, win } = await startDownload();
updates.emit( 'download-progress', progress( 42 ) );
updates.emit( 'download-progress', progress( 10 ) );
updates.emit( 'update-downloaded' );
updates.emit( 'error', new Error( 'network down' ) );
expect( win?.webContents.send.mock.calls ).toEqual( [
['UPDATE_DOWNLOAD', { id: 'update', status: 'connecting' }],
['UPDATE_DOWNLOAD', { id: 'update', status: 'downloading', percent: 42 }],
['UPDATE_DOWNLOAD', { id: 'update', status: 'restarted', percent: 10 }],
['UPDATE_DOWNLOAD', { id: 'update', status: 'done' }],
['UPDATE_DOWNLOAD', { id: 'update', status: 'idle' }],
] );
} );

it( 'the download-progress listener is registered before any download starts', () => {
const updates = updater();
listenForUpdates( updates as unknown as AppUpdater, {} as Dialog, () => null );
Expand Down Expand Up @@ -159,7 +180,7 @@ it( 'an error mid-download resets the state, clears the bar, then shows the erro
calls.length = 0;
updates.emit( 'error', new Error( 'network down' ) );
expect( getUpdateDownload() ).toEqual( { status: 'idle' } );
expect( calls ).toEqual( ['bar -1', 'error box'] );
expect( calls ).toEqual( ['send idle', 'bar -1', 'error box'] );
expect( logs() ).toEqual( [
'Update available.',
'Update download started.',
Expand Down
5 changes: 4 additions & 1 deletion src/main/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import log from 'electron-log';
import type { BrowserWindow, Dialog } from 'electron';
import type { AppUpdater, ProgressInfo } from 'electron-updater';
import type { UpdateDownload } from '../common/rendererBridge';
import { UPDATE_DOWNLOAD } from '../constants/ipc';

type WindowGetter = () => BrowserWindow | null;

Expand Down Expand Up @@ -35,7 +36,9 @@ export function applyUpdateDownloadBar( win: BrowserWindow | null ): void {

function setDownload( next: UpdateDownload ): void {
download = next;
applyUpdateDownloadBar( getWindow() );
const win = getWindow();
win?.webContents.send( UPDATE_DOWNLOAD, { id: 'update', ...next } );
applyUpdateDownloadBar( win );
}

// checkForUpdates rejects as well as emitting 'error'; the listener already reports it,
Expand Down
11 changes: 11 additions & 0 deletions src/preload-proofs/onUpdateDownload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"key": "updateDownload",
"method": "onUpdateDownload",
"channel": "UPDATE_DOWNLOAD",
"subscribe": true,
"result": {
"id": "update",
"status": "downloading",
"percent": 42
}
}
6 changes: 5 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ import {
TOGGLE_MULTIFURCATING_CONSTRAINT,
TREE_SELECT,
UNHANDLED_ERROR,
UPDATE_DOWNLOAD,
WRITE_FILE,
} from '../constants/ipc';
import type { RendererBridge, HostInfo } from '../common/rendererBridge';
import type { RendererBridge, HostInfo, UpdateDownloadPayload } from '../common/rendererBridge';
import type { CheckOutputArgs } from '../main/checkOutput';
import type { StateReport } from '../main/getActiveState';
import type { StartRunPayload, RunFinishedPayload, RunErrorPayload } from '../main/startRun';
Expand Down Expand Up @@ -191,6 +192,9 @@ const rendererBridge = {
onUnhandledError( listener: ( payload: unknown ) => void ) {
return onEvent( UNHANDLED_ERROR, listener );
},
onUpdateDownload( listener: ( payload: UpdateDownloadPayload ) => void ) {
return onEvent( UPDATE_DOWNLOAD, listener );
},
getPathForFile( file: File ): string {
return webUtils.getPathForFile( file );
},
Expand Down
Loading