checkpoint

This commit is contained in:
pavel 2026-02-27 10:57:13 +01:00
commit 208ab8042e
3 changed files with 296 additions and 26 deletions

137
main.js
View file

@ -1,8 +1,82 @@
const { app, BrowserWindow, session, desktopCapturer, ipcMain, clipboard } = require('electron');
const { autoUpdater } = require('electron-updater');
const path = require('path');
const fs = require('fs');
const url = require('url');
const updateState = {
status: 'idle', // idle | checking | available | downloading | downloaded | not-available | error
info: null,
error: null,
};
function broadcastUpdateState() {
const wins = BrowserWindow.getAllWindows();
for (const win of wins) {
if (!win.isDestroyed()) {
win.webContents.send('update-state', updateState);
}
}
}
function getStorageFilePath() {
return path.join(app.getPath('userData'), 'renderer-storage.json');
}
function readPersistentStore() {
const filePath = getStorageFilePath();
try {
if (!fs.existsSync(filePath)) return {};
const raw = fs.readFileSync(filePath, 'utf8');
const parsed = JSON.parse(raw);
return parsed && typeof parsed === 'object' ? parsed : {};
} catch (err) {
console.error('Failed to read persistent store', err);
return {};
}
}
function writePersistentStore(store) {
const filePath = getStorageFilePath();
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, JSON.stringify(store), 'utf8');
} catch (err) {
console.error('Failed to write persistent store', err);
}
}
function isNewerVersionAvailable(info) {
const next = info && typeof info.version === 'string' ? info.version : '';
return Boolean(next) && next !== app.getVersion();
}
async function runUpdateCheck(reason = 'manual') {
updateState.status = 'checking';
updateState.error = null;
broadcastUpdateState();
try {
const result = await autoUpdater.checkForUpdates();
const info = result && result.updateInfo ? result.updateInfo : null;
if (isNewerVersionAvailable(info)) {
updateState.status = 'available';
updateState.info = info;
updateState.error = null;
broadcastUpdateState();
} else {
updateState.status = 'not-available';
updateState.info = info;
updateState.error = null;
broadcastUpdateState();
}
} catch (err) {
updateState.status = 'error';
updateState.error = err && err.message ? err.message : String(err);
broadcastUpdateState();
console.error(`Update check failed (${reason})`, err);
}
}
function createWindow() {
// Create a persistent session for chattz to keep the user logged in
const sess = session.fromPartition('persist:chattz');
@ -34,6 +108,34 @@ function createWindow() {
return true;
});
ipcMain.handle('storage-get', (event, key) => {
if (typeof key !== 'string' || key.length === 0) return null;
const store = readPersistentStore();
return Object.prototype.hasOwnProperty.call(store, key) ? store[key] : null;
});
ipcMain.handle('storage-set', (event, key, value) => {
if (typeof key !== 'string' || key.length === 0) return false;
const store = readPersistentStore();
store[key] = value;
writePersistentStore(store);
return true;
});
ipcMain.handle('storage-remove', (event, key) => {
if (typeof key !== 'string' || key.length === 0) return false;
const store = readPersistentStore();
delete store[key];
writePersistentStore(store);
return true;
});
ipcMain.handle('get-update-state', () => ({ ...updateState }));
ipcMain.handle('check-for-updates-now', async () => {
await runUpdateCheck('renderer-direct');
return { ...updateState };
});
// Auto-approve media permissions (camera, microphone)
sess.setPermissionCheckHandler((webContents, permission) => {
if (permission === 'media' || permission === 'clipboard-read' || permission === 'clipboard-write') {
@ -112,6 +214,14 @@ function createWindow() {
loadDesktopApp();
// Ensure renderer receives latest updater state after any (re)load.
win.webContents.on('did-finish-load', () => {
broadcastUpdateState();
// Login flow swaps pages; force a check after desktop UI reloads so
// renderer always gets fresh updater state.
void runUpdateCheck('did-finish-load');
});
// Uncomment to debug
// win.webContents.openDevTools();
}
@ -127,27 +237,46 @@ app.whenReady().then(() => {
autoUpdater.logger = console;
autoUpdater.on('update-available', (info) => {
updateState.status = 'available';
updateState.info = info;
updateState.error = null;
broadcastUpdateState();
const wins = BrowserWindow.getAllWindows();
if (wins.length > 0) wins[0].webContents.send('update-available', info);
});
autoUpdater.on('update-not-available', (info) => {
updateState.status = 'not-available';
updateState.info = info || null;
updateState.error = null;
broadcastUpdateState();
});
autoUpdater.on('update-downloaded', (info) => {
updateState.status = 'downloaded';
updateState.info = info;
updateState.error = null;
broadcastUpdateState();
const wins = BrowserWindow.getAllWindows();
if (wins.length > 0) wins[0].webContents.send('update-downloaded', info);
});
autoUpdater.on('error', (err) => {
updateState.status = 'error';
updateState.error = err.message;
broadcastUpdateState();
const wins = BrowserWindow.getAllWindows();
if (wins.length > 0) wins[0].webContents.send('update-error', err.message);
});
ipcMain.on('check-for-updates', () => {
autoUpdater.checkForUpdatesAndNotify().catch(err => {
console.error("Manual update check failed", err);
});
void runUpdateCheck('manual');
});
ipcMain.on('download-update', () => {
updateState.status = 'downloading';
updateState.error = null;
broadcastUpdateState();
autoUpdater.downloadUpdate();
});
@ -157,7 +286,7 @@ app.whenReady().then(() => {
// Check once on startup
setTimeout(() => {
autoUpdater.checkForUpdatesAndNotify().catch(() => { });
void runUpdateCheck('startup');
}, 5000);
app.on('activate', () => {