Skip to content

Commit

Permalink
feat(Auto-update): Enables auto-updates for the Thorium Nova app.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Apr 30, 2022
1 parent 48b254a commit 264ac55
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 24 deletions.
23 changes: 19 additions & 4 deletions client/src/components/WelcomeLogo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ function useConnectionAddress() {
export const WelcomeLogo = ({className}: {className?: string}) => {
const connectionAddress = useConnectionAddress();
const clientData = useClientData();

const [updateText, setUpdateText] = useState("");
useEffect(() => {
window.thorium?.registerUpdateHandler(message => {
setUpdateText(message);
});
return () => {
window.thorium?.registerUpdateHandler(() => {});
};
}, []);
return (
<div className={className}>
<div className="flex items-end self-start ">
Expand All @@ -32,9 +40,16 @@ export const WelcomeLogo = ({className}: {className?: string}) => {
<h1 className="text-4xl ml-3 min-w-[12ch] text-white">Thorium Nova</h1>
</div>
<h2 className="text-2xl mt-2">
<Link className="text-purple-300 hover:text-purple-500" to="/releases">
Version {packageJson.version}
</Link>
{updateText ? (
updateText
) : (
<Link
className="text-purple-300 hover:text-purple-500"
to="/releases"
>
Version {packageJson.version}
</Link>
)}
</h2>
<div className="mt-6"></div>
<ClientButton />
Expand Down
1 change: 1 addition & 0 deletions client/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare global {
thorium: {
getAddress: () => Promise<string>;
getHostSecret: () => Promise<string>;
registerUpdateHandler: (handler: (update: string) => void) => void;
};
}
}
Expand Down
6 changes: 5 additions & 1 deletion desktop/main/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
stopThoriumServer,
} from "./helpers/startThoriumServer";
import {ipcHandlers} from "./helpers/ipcHandlers";
import {autoUpdater} from "electron-updater";
import {initWin} from "./helpers/autoUpdate";

let win: BrowserWindow | null = null;
app.enableSandbox();

Expand Down Expand Up @@ -66,7 +69,7 @@ async function createWindow() {
},
show: false,
});

initWin(win);
win.webContents.setWindowOpenHandler(({url}) => {
shell.openExternal(url);
return {action: "deny"};
Expand All @@ -90,6 +93,7 @@ async function createWindow() {
}

app.whenReady().then(() => {
autoUpdater.checkForUpdatesAndNotify();
createWindow();
});

Expand Down
37 changes: 37 additions & 0 deletions desktop/main/helpers/autoUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {autoUpdater} from "electron-updater";
import {BrowserWindow} from "electron";

let win: BrowserWindow | null = null;
export function initWin(newWin: BrowserWindow) {
win = newWin;
}
function sendStatusToWindow(text: string) {
win?.webContents.send("update-message", text);
}
autoUpdater.on("checking-for-update", () => {
sendStatusToWindow("Checking for update...");
});
autoUpdater.on("update-available", info => {
sendStatusToWindow("Update available.");
});
autoUpdater.on("update-not-available", info => {
sendStatusToWindow("");
});
autoUpdater.on("error", err => {
sendStatusToWindow("Error in auto-updater. " + err);
});
autoUpdater.on("download-progress", progressObj => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + " - Downloaded " + progressObj.percent + "%";
log_message =
log_message +
" (" +
progressObj.transferred +
"/" +
progressObj.total +
")";
sendStatusToWindow(log_message);
});
autoUpdater.on("update-downloaded", info => {
sendStatusToWindow("Update downloaded - restart to apply");
});
8 changes: 8 additions & 0 deletions desktop/main/preload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {ipcRenderer} from "electron";

let updateHandler = (message: string) => {};
ipcRenderer.on("update-message", (event, message) => {
updateHandler(message);
});

const thorium = {
getAddress: function () {
return ipcRenderer.invoke("get-address");
},
getHostSecret: function () {
return ipcRenderer.invoke("get-secret");
},
registerUpdateHandler: function (handler: typeof updateHandler) {
updateHandler = handler;
},
};
window.thorium = thorium;

Expand Down
3 changes: 2 additions & 1 deletion desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"scripts": {
"dev": "",
"start": "concurrently \"electron-esbuild dev\" \"copy-dir-cli resources ../dist/resources\"",
"start": "concurrently \"electron-esbuild dev\" \"npx copy-dir-cli resources ../dist/resources\"",
"build": "electron-esbuild build",
"typecheck": "tsc --noEmit"
},
Expand All @@ -19,6 +19,7 @@
"bonjour": "^3.5.0",
"electron-better-ipc": "^2.0.1",
"electron-store": "^8.0.1",
"electron-updater": "^5.0.1",
"electron-util": "^0.17.2"
}
}
Loading

0 comments on commit 264ac55

Please sign in to comment.