Skip to content

Commit

Permalink
get instance issue, toast actions, manual update
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Trowbridge committed Dec 19, 2022
1 parent c8fca12 commit 49054cf
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 35 deletions.
4 changes: 2 additions & 2 deletions release/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acuamtica-dev-tools",
"version": "1.0.4",
"version": "1.0.5",
"description": "Acumatica ERP Development Tools",
"license": "MIT",
"author": {
Expand Down
23 changes: 3 additions & 20 deletions src/main/actions/getInstances.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const path = require('path');
const fs = require('fs');
const fsp = require('fs').promises;

const sql = require('mssql');
const convert = require('xml-js');
const xmlOptions = { compact: true, spaces: 4 };

const { GetSettings, SendToast } = require('./../helpers');
const { SendToast, WindowsPath } = require('./../helpers');

export default async function GetInstances(mainWindow, database) {
try {
Expand All @@ -25,7 +26,7 @@ export default async function GetInstances(mainWindow, database) {
var db = null;

if (site.virtualDirectory._attributes.physicalPath?.toLocaleLowerCase().startsWith(settings.instanceLocation?.toLocaleLowerCase())) {
var acuConfig = await loadXml(`${site.virtualDirectory._attributes.physicalPath}Web.config`);
var acuConfig = await loadXml(WindowsPath(path.join(site.virtualDirectory._attributes.physicalPath, 'Web.config')));
acumaticaVersion = acuConfig.configuration.appSettings.add.find((a) => a._attributes.key == 'Version')._attributes.value;

try {
Expand Down Expand Up @@ -95,21 +96,3 @@ async function loadXml(path) {
const data = await fsp.readFile(path, 'utf8');
return convert.xml2js(data, xmlOptions);
}
async function loadHtml(path) {
const data = await fsp.readFile(path, 'utf8');
return data;
}
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === '-') {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
/* next line works with strings and numbers,
* and you may want to customize it to your needs
*/
var result = a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0;
return result * sortOrder;
};
}
1 change: 1 addition & 0 deletions src/main/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function SendToast(mainWindow: BrowserWindow, alert: SnackbarAlert) {
options: {
...alert.options,
},
action: alert.action,
});
}

Expand Down
35 changes: 32 additions & 3 deletions src/main/ipcBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import GetInstances from './actions/getInstances';
import { GetSettings, SendToast } from './helpers';
import { OpenExplorer, WindowsPath } from './helpers';
import DownloadBuild, { BuildRow } from './actions/downloadBuild';
import { AppUpdater } from 'electron-updater';

export default class IpcBuilder {
app: Electron.App;
db: sqlite.Database;
mainWindow: BrowserWindow;
autoUpdater: AppUpdater;
db: sqlite.Database;

constructor(app: Electron.App, mainWindow: BrowserWindow, db: sqlite.Database) {
constructor(app: Electron.App, mainWindow: BrowserWindow, autoUpdater: AppUpdater, db: sqlite.Database) {
this.app = app;
this.db = db;
this.mainWindow = mainWindow;
this.autoUpdater = autoUpdater;
this.db = db;
}

buildIpc() {
Expand Down Expand Up @@ -47,6 +50,32 @@ export default class IpcBuilder {
});
});

ipcMain.handle('checkForAppUpdate', async (event, args) => {
SendToast(this.mainWindow, {
text: 'Checking for updates...',
options: {
variant: 'info',
},
});

this.autoUpdater.checkForUpdates().then(() => {
this.autoUpdater.on('update-not-available', (info) => {
SendToast(this.mainWindow, {
text: `You're already running the lastest version: ${info.version}`,
options: {
variant: 'success',
},
});
this.autoUpdater.off('update-not-available', () => {});
});
});
});

ipcMain.on('restartToUpdate', async (event, args) => {
console.log('restartToUpdate');
this.autoUpdater.quitAndInstall(false, true);
});

ipcMain.handle('launchApp', async (event, path) => {
path = WindowsPath(path);
return new Promise(async (resolve, reject) => {
Expand Down
24 changes: 19 additions & 5 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const createWindow = async () => {
// const menuBuilder = new MenuBuilder(mainWindow);
// menuBuilder.buildMenu();

const ipcBuilder = new IpcBuilder(app, mainWindow, db);
const ipcBuilder = new IpcBuilder(app, mainWindow, autoUpdater, db);
ipcBuilder.buildIpc();

// Open urls in the user's browser
Expand Down Expand Up @@ -240,21 +240,35 @@ function StartTasks(mainWindow: BrowserWindow) {
autoUpdater.checkForUpdates();
});

autoUpdater.on('update-available', () => {
autoUpdater.on('update-available', (info) => {
SendToast(mainWindow, {
text: 'Downloading App Update...',
text: `Downloading App Update... ${info.version}`,
options: {
variant: 'info',
},
});
});
autoUpdater.on('update-downloaded', () => {
autoUpdater.on('update-downloaded', (info) => {
SendToast(mainWindow, {
text: 'A new version has been downloaded. Restart the app to apply the update.',
text: `A new version has been downloaded. Restart the app to apply the update. `,
options: {
variant: 'success',
autoHideDuration: null,
},

action: {
btnText: 'Restart',
event: 'restartToUpdate',
},
});
});

autoUpdater.on('error', (info) => {
SendToast(mainWindow, {
text: info.message,
options: {
variant: 'error',
},
});
});
}
1 change: 1 addition & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
windowEvents: (action: string) => ipcRenderer.invoke('windowEvents', action),
sendToast: (alert: SnackbarAlert) => ipcRenderer.invoke('sendToast', alert),
getAppVersion: () => ipcRenderer.invoke('getAppVersion'),
checkForAppUpdate: () => ipcRenderer.invoke('checkForAppUpdate'),
launchApp: (path: string) => ipcRenderer.invoke('launchApp', path),
openDirectory: (path: string) => ipcRenderer.invoke('openDirectory', path),

Expand Down
17 changes: 15 additions & 2 deletions src/renderer/Pages/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as React from 'react';

import Grid from '@mui/material/Grid';
import TextField from '@mui/material/TextField';
import CardContent from '@mui/material/CardContent';
import Card from '@mui/material/Card';
import Typography from '@mui/material/Typography';

import Grid from '@mui/material/Grid';
import Button from '@mui/material/Button';
import SecurityUpdateIcon from '@mui/icons-material/SecurityUpdate';

export default function Instances() {
const [hostname, setHostname] = React.useState(window.appSettings.hostname);
Expand Down Expand Up @@ -115,7 +117,18 @@ export default function Instances() {
<Typography gutterBottom variant='h5' component='div'>
About
</Typography>
Version: {version}
<Typography gutterBottom variant='body1' component='div'>
Version: {version}
</Typography>
<Button
variant='contained'
endIcon={<SecurityUpdateIcon />}
onClick={() => {
window.electronAPI.checkForAppUpdate();
}}
>
Check for updates
</Button>
</CardContent>
</Card>
</Grid>
Expand Down
36 changes: 35 additions & 1 deletion src/renderer/Toasts.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
import * as React from 'react';

import { useSnackbar } from 'notistack';
import { SnackbarKey, useSnackbar } from 'notistack';

import IconButton from '@mui/material/IconButton';
import CloseIcon from '@mui/icons-material/Close';

import App from './App';
import { SnackbarAlert } from './types';
import Button from '@mui/material/Button';

type ButtonActionProps = {
key: SnackbarKey;
action:
| {
event: string;
btnText: string;
args?: any[];
}
| undefined;
};

function ButtonAction(props: ButtonActionProps) {
// console.log(props);
const { closeSnackbar } = useSnackbar();

return (
<Button
onClick={() => {
window.electronAPI.events.sendMessage(props.action?.event, props.action?.args);
closeSnackbar(props.key);
}}
sx={{ marginRight: 1 }}
variant='contained'
color='inherit'
>
<div style={{ color: '#000' }}>{props.action?.btnText}</div>
</Button>
);
}

export default function Toasts() {
const { enqueueSnackbar, closeSnackbar } = useSnackbar();

window.electronAPI.events.on('alert', (arg) => {
let alert: SnackbarAlert = arg as SnackbarAlert;

console.log(alert);

enqueueSnackbar(alert.text, {
...alert.options,
preventDuplicate: true,
action: (key) => (
<>
{alert.action && <ButtonAction key={key} action={alert.action} />}
<IconButton aria-label='close' size='small' onClick={() => closeSnackbar(key)}>
<CloseIcon />
</IconButton>
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IElectronAPI {
windowEvents: (action: string) => void;
sendToast: (alert: SnackbarAlert) => void;
getAppVersion: () => Promise<string>;
checkForAppUpdate: () => void;
launchApp: (path: string) => Promise<boolean>;
openDirectory: (path: string) => Promise<boolean>;

Expand All @@ -21,7 +22,7 @@ export interface IElectronAPI {
downloadBuild: (build: BuildRow, extractMsi: boolean) => void;

events: {
sendMessage(channel: string, args: unknown[]): void;
sendMessage(channel: string | undefined, args: unknown[] | undefined): void;
on(channel: string, func: (...args: unknown[]) => void): (() => void) | undefined;
once(channel: string, func: (...args: unknown[]) => void): void;
};
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ export type SnackbarAlert = {
variant: AlertColor;
autoHideDuration?: number | null | undefined;
};
action?: {
btnText: string;
event: string;
args?: any[];
};
};

0 comments on commit 49054cf

Please sign in to comment.