From 3b17953a82a4e32c0456d6a112496b0f1c13225c Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Sun, 19 May 2024 01:39:50 -0300 Subject: [PATCH] create electron event --- .../events/autoupdater/check-for-updates.ts | 42 +++++++++++++++++++ src/main/events/index.ts | 1 + src/main/services/window-manager.ts | 11 ++++- src/preload/index.ts | 5 +++ src/renderer/splash.html | 16 ------- src/renderer/src/app.tsx | 6 +-- src/renderer/src/declaration.d.ts | 3 ++ src/renderer/src/main.tsx | 12 +++--- src/renderer/src/pages/splash/splash.css.ts | 2 +- src/renderer/src/pages/splash/splash.tsx | 8 ++++ src/types/index.ts | 34 +++++++++++++++ 11 files changed, 113 insertions(+), 27 deletions(-) create mode 100644 src/main/events/autoupdater/check-for-updates.ts delete mode 100644 src/renderer/splash.html diff --git a/src/main/events/autoupdater/check-for-updates.ts b/src/main/events/autoupdater/check-for-updates.ts new file mode 100644 index 00000000..cdffb260 --- /dev/null +++ b/src/main/events/autoupdater/check-for-updates.ts @@ -0,0 +1,42 @@ +import { AppUpdaterEvents } from "@types"; +import { registerEvent } from "../register-event"; +import updater, { + ProgressInfo, + UpdateDownloadedEvent, + UpdateInfo, +} from "electron-updater"; + +const { autoUpdater } = updater; + +const checkForUpdates = async ( + _event: Electron.IpcMainInvokeEvent, + cb: (value: AppUpdaterEvents) => void +) => { + console.log("check for updates event"); + autoUpdater + .addListener("error", (error: Error, message?: string) => { + cb({ error, message }); + }) + .addListener("checking-for-update", () => { + cb("checking-for-updates"); + }) + .addListener("update-not-available", (info: UpdateInfo) => { + cb(info); + }) + .addListener("update-available", (info: UpdateInfo) => { + cb(info); + }) + .addListener("update-downloaded", (event: UpdateDownloadedEvent) => { + cb(event); + }) + .addListener("download-progress", (info: ProgressInfo) => { + cb(info); + }) + .addListener("update-cancelled", (info: UpdateInfo) => { + cb(info); + }); + + autoUpdater.checkForUpdates(); +}; + +registerEvent("checkForUpdates", checkForUpdates); diff --git a/src/main/events/index.ts b/src/main/events/index.ts index 5d721c62..3d749d0c 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -27,6 +27,7 @@ import "./torrenting/start-game-download"; import "./user-preferences/get-user-preferences"; import "./user-preferences/update-user-preferences"; import "./user-preferences/auto-launch"; +import "./autoupdater/check-for-updates"; ipcMain.handle("ping", () => "pong"); ipcMain.handle("getVersion", () => app.getVersion()); diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index f1e56256..e2128a0c 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -41,11 +41,14 @@ export class WindowManager { // Load the remote URL for development or the local html file for production. if (is.dev && process.env["ELECTRON_RENDERER_URL"]) { this.splashWindow?.loadURL( - `${process.env["ELECTRON_RENDERER_URL"]}/splash.html` + `${process.env["ELECTRON_RENDERER_URL"]}#/splash` ); } else { this.splashWindow?.loadFile( - path.join(__dirname, "../renderer/splash.html") + path.join(__dirname, "../renderer/index.html"), + { + hash: "splash", + } ); } } @@ -56,6 +59,10 @@ export class WindowManager { height: 400, frame: true, alwaysOnTop: false, + webPreferences: { + preload: path.join(__dirname, "../preload/index.mjs"), + sandbox: false, + }, }); this.loadSplashURL(); diff --git a/src/preload/index.ts b/src/preload/index.ts index 6a209787..0a12218f 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -7,6 +7,7 @@ import type { GameShop, TorrentProgress, UserPreferences, + AppUpdaterEvents, } from "@types"; contextBridge.exposeInMainWorld("electron", { @@ -112,4 +113,8 @@ contextBridge.exposeInMainWorld("electron", { showOpenDialog: (options: Electron.OpenDialogOptions) => ipcRenderer.invoke("showOpenDialog", options), platform: process.platform, + + /* Splash */ + checkForUpdates: (cb: (value: AppUpdaterEvents) => void) => + ipcRenderer.invoke("checkForUpdates", cb), }); diff --git a/src/renderer/splash.html b/src/renderer/splash.html deleted file mode 100644 index bb0b5553..00000000 --- a/src/renderer/splash.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - Hydra - - - -
- - - diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index da95f292..a1dd3d9a 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -12,7 +12,7 @@ import { import * as styles from "./app.css"; import { themeClass } from "./theme.css"; -import { useLocation, useNavigate } from "react-router-dom"; +import { Outlet, useLocation, useNavigate } from "react-router-dom"; import { setSearch, clearSearch, @@ -27,7 +27,7 @@ export interface AppProps { children: React.ReactNode; } -export function App({ children }: AppProps) { +export function App() { const contentRef = useRef(null); const { updateLibrary } = useLibrary(); @@ -128,7 +128,7 @@ export function App({ children }: AppProps) { />
- {children} +
diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 045915bb..1f3a3a9e 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -90,6 +90,9 @@ declare global { options: Electron.OpenDialogOptions ) => Promise; platform: NodeJS.Platform; + + /* Splash */ + checkForUpdates: (cb: (value: AppUpdaterEvents) => void) => Promise; } interface Window { diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index f44653cb..19456ff9 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -27,6 +27,7 @@ import { import { store } from "./store"; import * as resources from "@locales"; +import Splash from "./pages/splash/splash"; i18n .use(LanguageDetector) @@ -46,16 +47,17 @@ ReactDOM.createRoot(document.getElementById("root")!).render( - - - + + + }> + - - + + diff --git a/src/renderer/src/pages/splash/splash.css.ts b/src/renderer/src/pages/splash/splash.css.ts index 794a6057..4a7761b4 100644 --- a/src/renderer/src/pages/splash/splash.css.ts +++ b/src/renderer/src/pages/splash/splash.css.ts @@ -1,5 +1,5 @@ import { style } from "@vanilla-extract/css"; -import { SPACING_UNIT, vars } from "../../theme.css"; +import { SPACING_UNIT } from "../../theme.css"; export const main = style({ width: "100%", diff --git a/src/renderer/src/pages/splash/splash.tsx b/src/renderer/src/pages/splash/splash.tsx index f4fe8ae9..90ce8c67 100644 --- a/src/renderer/src/pages/splash/splash.tsx +++ b/src/renderer/src/pages/splash/splash.tsx @@ -3,10 +3,18 @@ import * as styles from "./splash.css"; import { themeClass } from "../../theme.css"; import "../../app.css"; +import { useEffect } from "react"; document.body.classList.add(themeClass); export default function Splash() { + useEffect(() => { + window.electron.checkForUpdates((event) => { + console.log("-----------"); + console.log(event); + }); + }, []); + return (
diff --git a/src/types/index.ts b/src/types/index.ts index 7895686d..9518ce3e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,9 @@ import type { Downloader, GameStatus } from "@shared"; +import { + ProgressInfo, + UpdateDownloadedEvent, + UpdateInfo, +} from "electron-updater"; export type GameShop = "steam" | "epic"; export type CatalogueCategory = "recently_added" | "trending"; @@ -143,3 +148,32 @@ export interface SteamGame { name: string; clientIcon: string | null; } + +interface ErrorEvent { + error: Error; + message?: string; +} + +interface UpdateNotAvailable { + info: UpdateInfo; +} +interface UpdateAvailable { + info: UpdateInfo; +} +interface UpdateDownloaded { + event: UpdateDownloadedEvent; +} +interface DownloadProgress { + info: ProgressInfo; +} +interface UpdateCancelled { + info: UpdateInfo; +} + +export type AppUpdaterEvents = + | ErrorEvent + | UpdateNotAvailable + | UpdateAvailable + | UpdateDownloaded + | DownloadProgress + | UpdateCancelled;