Skip to content

Commit

Permalink
feat(update): add electron-store for persistent settings and enhance …
Browse files Browse the repository at this point in the history
…update handling
  • Loading branch information
Red-Asuka committed Dec 17, 2024
1 parent 7488862 commit 7d5a02e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 15 deletions.
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@mqttx/ui": "workspace:*",
"better-sqlite3": "^11.6.0",
"drizzle-orm": "^0.36.4",
"electron-store": "^10.0.0",
"electron-updater": "^6.3.9",
"element-plus": "^2.8.7",
"markdown-it": "^14.1.0",
Expand Down
17 changes: 9 additions & 8 deletions apps/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import { useAppUpdater } from './update'

// const IsMacOS = process.platform === 'darwin'

let existingSettings: SelectSettings | undefined

async function createWindow() {
let data: SelectSettings | undefined
data = await db.query.settings.findFirst()
if (!data) {
existingSettings = await db.query.settings.findFirst()
if (!existingSettings) {
await db.insert(settings).values({})
}
data = await db.query.settings.findFirst() as SelectSettings
existingSettings = await db.query.settings.findFirst() as SelectSettings

const width = data.width || 1024
const height = data.height || 749
const currentTheme = data.currentTheme || 'light'
const width = existingSettings.width || 1024
const height = existingSettings.height || 749
const currentTheme = existingSettings.currentTheme || 'light'
const bgColor = {
dark: '#232323',
night: '#212328',
Expand Down Expand Up @@ -94,7 +95,7 @@ app.whenReady().then(async () => {

await createWindow()

useAppUpdater()
useAppUpdater(existingSettings!)

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
Expand Down
40 changes: 34 additions & 6 deletions apps/desktop/src/main/update.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { Lang } from 'mqttx'
import type { SelectSettings } from '../database/schemas/settings'
import type { UpdateEvent } from '../preload/index.d'
import { BrowserWindow, ipcMain } from 'electron'
import { app, BrowserWindow, ipcMain } from 'electron'
import Store from 'electron-store'
import pkg from 'electron-updater'

// FIXME: https://github.com/sindresorhus/electron-store/issues/276
const store = new Store() as any
const { autoUpdater } = pkg

if (process.env.NODE_ENV === 'development') {
autoUpdater.forceDevUpdateConfig = true
}

autoUpdater.autoDownload = false

// autoUpdater.checkForUpdates()
// autoUpdater.downloadUpdate()
// autoUpdater.quitAndInstall()
autoUpdater.autoInstallOnAppQuit = false

autoUpdater.on('checking-for-update', () => {
sendUpdateStatus({ status: 'checking-for-update' })
Expand Down Expand Up @@ -52,6 +54,27 @@ async function fetchReleaseNotes(version: string): Promise<string> {
})
}

async function showReleaseNotesWindow(lang: Lang) {
const language = ['en', 'zh', 'ja'].includes(lang) ? lang : 'en'
const version = app.getVersion()
const link = `https://mqttx.app/${language}/changelogs/v${version}`

try {
const response = await fetch(link, { method: 'GET', signal: AbortSignal.timeout(5000) })
if (response.status !== 200) {
return
}
const releaseNotesWindow = new BrowserWindow({
width: 600,
height: 500,
alwaysOnTop: true,
})
releaseNotesWindow.loadURL(link)
} catch (e) {
console.error(e)
}
}

function sendUpdateStatus(updateEvent: UpdateEvent) {
const windows = BrowserWindow.getAllWindows()
windows.forEach((window) => {
Expand All @@ -63,7 +86,12 @@ function sendUpdateStatus(updateEvent: UpdateEvent) {
})
}

function useAppUpdater() {
function useAppUpdater(settings: SelectSettings) {
const version = app.getVersion()
if (store.get('version') !== version) {
showReleaseNotesWindow(settings.currentLang)
store.set('version', version)
}
ipcMain.handle('check-for-updates', async () => {
return await autoUpdater.checkForUpdates()
})
Expand Down
86 changes: 85 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit 7d5a02e

Please sign in to comment.