Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX-2366 Desktop Edition unfinished jobs warning message on close #2373

Merged
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/WitsmlExplorer.Desktop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Witsml Explorer Desktop Edition is an easy-to-install version of Witsml Explorer. However, this edition does not support all features as the web application.

> :warning: **Please be aware that there is a potential risk associated with running jobs while using the desktop edition**: As the API is running locally on your machine, there is a potential risk that critical jobs may not be able to finish. This could be due to exiting the application while running a job, computer shutdown, power outage or any other cause that forces the computer or application to shut down.

## Supported operating systems

- Windows 64-bit
Expand Down
39 changes: 37 additions & 2 deletions Src/WitsmlExplorer.Desktop/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
/// <reference types="vite/client" />

import { spawn } from "cross-spawn";
import { app, BrowserWindow, dialog, ipcMain } from "electron";
import {
app,
BrowserWindow,
dialog,
Event,
ipcMain,
MessageBoxOptions
} from "electron";
import * as fs from "fs";
import * as path from "path";

let mainWindow;
let mainWindow: BrowserWindow;
let apiProcess: any;

const isDevelopment = process.env.NODE_ENV === "development";
Expand Down Expand Up @@ -69,6 +76,18 @@ function showErrorAndQuit(message: string) {
app.quit();
}

function showUnfinishedJobsWarningOnClose() {
const options: MessageBoxOptions = {
type: "warning",
buttons: ["Cancel", "Quit"],
title: "WARNING",
message: "Unfinished jobs will be terminated",
detail:
"You have unfinished jobs that may cause issues if not completed before exiting.\n\nAre you sure you want to exit the application?"
};
return dialog.showMessageBoxSync(mainWindow, options);
}

interface Deferred<T> {
promise: Promise<T>;
resolve: (value?: T | PromiseLike<T>) => void;
Expand Down Expand Up @@ -181,13 +200,29 @@ function createWindow() {
} else {
mainWindow.loadFile(path.join(__dirname, "../renderer/index.html"));
}

mainWindow.on("close", async (e: Event) => {
e.preventDefault();
mainWindow.webContents.send("closeWindow");
});

mainWindow.on("closed", (): void => (mainWindow = null));
}

app.whenReady().then(async () => {
const appConfig = readOrCreateAppConfig();
await startApi(appConfig);
ipcMain.handle("getConfig", () => appConfig);

ipcMain.on("closeWindowResponse", async (_event, isUnfinishedJobs) => {
if (isUnfinishedJobs) {
const dialogResponse = showUnfinishedJobsWarningOnClose();
if (dialogResponse === 1) mainWindow.destroy();
} else {
mainWindow.destroy();
}
});

createWindow();

// From Electron docs: macOS apps generally continue running even without any windows open, and activating the app when no windows are available should open a new one.
Expand Down
8 changes: 7 additions & 1 deletion Src/WitsmlExplorer.Desktop/src/preload/preload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { contextBridge, ipcRenderer } from "electron";

contextBridge.exposeInMainWorld("electronAPI", {
getConfig: () => ipcRenderer.invoke("getConfig")
getConfig: () => ipcRenderer.invoke("getConfig"),
onCloseWindow: (callback: any) =>
ipcRenderer.on("closeWindow", () => callback()),
removeCloseWindowListener: () =>
ipcRenderer.removeAllListeners("closeWindow"),
closeWindowResponse: (isUnfinishedJobs: boolean) =>
ipcRenderer.send("closeWindowResponse", isUnfinishedJobs)
});
38 changes: 38 additions & 0 deletions Src/WitsmlExplorer.Frontend/components/CloseDesktopAppHandler.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useConnectedServer } from "contexts/connectedServerContext";
import { useGetJobInfo } from "hooks/query/useGetJobInfo";
import JobStatus from "models/jobStatus";
import { ReactElement, useCallback, useEffect } from "react";

export function CloseDesktopAppHandler(): ReactElement {
const { connectedServer } = useConnectedServer();
const { jobInfos } = useGetJobInfo(false, {
enabled: !!connectedServer,
placeholderData: []
});

const listener = useCallback(() => {
const unfinishedJobs = jobInfos.filter(
(jobInfo) => jobInfo.status === JobStatus.Started
);

if (unfinishedJobs.length > 0) {
// @ts-ignore
window.electronAPI.closeWindowResponse(true);
} else {
// @ts-ignore
window.electronAPI.closeWindowResponse(false);
}
}, [jobInfos]);

useEffect(() => {
// @ts-ignore
window.electronAPI.onCloseWindow(listener);

return () => {
// @ts-ignore
window.electronAPI.removeCloseWindowListener(listener);
};
}, [listener]);

return null;
}
3 changes: 3 additions & 0 deletions Src/WitsmlExplorer.Frontend/routes/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { InteractionType } from "@azure/msal-browser";
import { MsalAuthenticationTemplate, MsalProvider } from "@azure/msal-react";
import { ThemeProvider } from "@mui/material";
import { CloseDesktopAppHandler } from "components/CloseDesktopAppHandler";
import { LoggedInUsernamesProvider } from "contexts/loggedInUsernamesContext";
import { SnackbarProvider } from "notistack";
import { useEffect } from "react";
import { isDesktopApp } from "tools/desktopAppHelpers";
import ContextMenuPresenter from "../components/ContextMenus/ContextMenuPresenter";
import GlobalStyles from "../components/GlobalStyles";
import ModalPresenter from "../components/Modals/ModalPresenter";
Expand Down Expand Up @@ -122,6 +124,7 @@ export default function Root() {
<SidebarProvider>
<FilterContextProvider>
<QueryContextProvider>
{isDesktopApp() && <CloseDesktopAppHandler />}
<RefreshHandler />
<SnackbarProvider>
<Snackbar />
Expand Down