codeburn/app/renderer/hooks/useUpdateStatus.ts
Resham Joshi 290d1c7c74
feat(update): desktop updates its companions and itself in one click (#1741)
The desktop app runs codeburn update for the CLI and menu bar on launch and after it updates itself, and the update banner installs and restarts in one click with a Try again path if the install fails.
2026-10-11 04:34:34 +02:00

75 lines
3.4 KiB
TypeScript

import { useEffect, useState } from 'react'
import { codeburn } from '../lib/ipc'
import type { CompanionUpdateStatus, UpdateStatus } from '../lib/types'
/**
* Reads the main-process update-availability status once on mount and stays live
* via the push event. Returns null until the first read resolves, or when the
* bridge predates the feature (an older preload, or a test mock) — both degrade
* to "no update", so callers can treat null as "nothing to show".
*/
export function useUpdateStatus(): UpdateStatus | null {
const [status, setStatus] = useState<UpdateStatus | null>(null)
useEffect(() => {
if (typeof codeburn.getUpdateStatus !== 'function') return
let active = true
codeburn.getUpdateStatus().then(next => { if (active) setStatus(next) }).catch(() => { /* offline — no nudge */ })
const unsubscribe = typeof codeburn.onUpdateStatus === 'function'
? codeburn.onUpdateStatus(next => { if (active) setStatus(next) })
: undefined
return () => { active = false; unsubscribe?.() }
}, [])
return status
}
/** The menubar app and global CLI update, read once and kept live like the desktop's own.
* Null where the bridge has no companions (an older preload, the IDE, a test mock). */
export function useCompanionUpdate(): CompanionUpdateStatus | null {
const [status, setStatus] = useState<CompanionUpdateStatus | null>(null)
useEffect(() => {
if (typeof codeburn.getCompanionUpdate !== 'function') return
let active = true
codeburn.getCompanionUpdate().then(next => { if (active) setStatus(next) }).catch(() => {})
const unsubscribe = codeburn.onCompanionUpdate?.(next => { if (active) setStatus(next) })
return () => { active = false; unsubscribe?.() }
}, [])
return status
}
/** GitHub release page for a desktop tag — the fallback Download target when
* no single direct asset fits (Linux ships three formats) or the platform is
* unknown. https-only, so it passes the openExternal allowlist. */
export function releasePageUrl(tag: string): string {
return `https://github.com/getagentseal/codeburn/releases/tag/${tag}`
}
/** Official signed Windows distribution. The Store handles installation and
* updates, avoiding the unsigned GitHub installer and SmartScreen warning. */
export const MICROSOFT_STORE_URL = 'https://apps.microsoft.com/detail/9P0R4ZL5XMB8'
/**
* Preferred install target for the running platform. macOS downloads the
* matching release asset; Windows opens the signed Microsoft Store listing.
* Returns null (callers fall back to the release page) for Linux — three
* formats, the user picks — and for unknown platforms or a preload without
* `arch`.
*/
export function directDownloadUrl(tag: string, platform: string | undefined, arch: string | undefined): string | null {
const version = tag.startsWith('desktop-v') ? tag.slice('desktop-v'.length) : null
if (!version) return null
let file: string | null = null
if (platform === 'darwin') {
if (!arch) return null
file = arch === 'arm64' ? `CodeBurn-${version}-arm64.dmg` : `CodeBurn-${version}.dmg`
} else if (platform === 'win32') {
return MICROSOFT_STORE_URL
}
if (!file) return null
return `https://github.com/getagentseal/codeburn/releases/download/${tag}/${file}`
}
/** The Download click target: direct asset when determinable, else the page. */
export function updateDownloadUrl(tag: string): string {
return directDownloadUrl(tag, codeburn.platform, codeburn.arch) ?? releasePageUrl(tag)
}