From 8de3aad4b917acc040ea796c7aa4ec2cfb4ed4be Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Tue, 1 Sep 2026 22:14:54 -0700 Subject: [PATCH] feat(windows): the update badge and the CLI update banner in the popover Two surfaces the mac has had and this build did not: the header badge that says an update is waiting, and the strip under the footer that says the CLI itself is behind. Both read one store (src/lib/update.ts), which is the observable half of the mac's UpdateChecker; the check itself is already in Rust and cached, so asking on mount costs nothing and the store only offers Rust the chance to re-check every twelve hours, well inside its own two-day interval. The badge is the mac's: it appears only when there is an update or a failed check, its label carries which stage failed, and the click does what the mac's does - retry a failed check, install an offered update, ask again otherwise. The banner keeps both of the mac's ways out: Update now, which runs the same one-click sequence, and the command itself, copyable, for anyone who would rather watch npm work in their own terminal. The banner's blue is the mac's Color.blue rather than the brand ember, deliberately: the star banner sits two rows below it in terracotta, and an informational strip that borrows the brand colour reads as a second one. --- windows/src/App.tsx | 3 + windows/src/components/CLIUpdateBanner.tsx | 55 ++++++ windows/src/components/Header.tsx | 10 +- windows/src/components/Icons.tsx | 10 ++ windows/src/components/UpdateBadge.tsx | 43 +++++ windows/src/lib/update.ts | 189 +++++++++++++++++++++ windows/src/styles.css | 59 +++++++ 7 files changed, 366 insertions(+), 3 deletions(-) create mode 100644 windows/src/components/CLIUpdateBanner.tsx create mode 100644 windows/src/components/UpdateBadge.tsx create mode 100644 windows/src/lib/update.ts diff --git a/windows/src/App.tsx b/windows/src/App.tsx index 33b494e5..b1b017e5 100644 --- a/windows/src/App.tsx +++ b/windows/src/App.tsx @@ -42,6 +42,7 @@ import { FooterBar } from './components/FooterBar' import { ErrorToast } from './components/ErrorToast' import { FetchErrorOverlay } from './components/FetchErrorOverlay' import { Header } from './components/Header' +import { CLIUpdateBanner } from './components/CLIUpdateBanner' import { accentById, applyAccent, type AccentPreset } from './lib/accent' const payloadCache = new PayloadCache() @@ -647,6 +648,8 @@ export function App() { footnote={footnote} /> + + {/* With something already on screen the failure is an aside, not a wall. */} diff --git a/windows/src/components/CLIUpdateBanner.tsx b/windows/src/components/CLIUpdateBanner.tsx new file mode 100644 index 00000000..d8ea185f --- /dev/null +++ b/windows/src/components/CLIUpdateBanner.tsx @@ -0,0 +1,55 @@ +import { useEffect, useState } from 'react' + +import { EMPTY_UPDATE, performUpdate, subscribeUpdate, type UpdateState } from '../lib/update' +import { ArrowUpRightCircleIcon, CheckIcon, CopyIcon } from './Icons' + +/// Port of CLIUpdateBanner in mac/.../Views/MenuBarContent.swift: a strip under the footer +/// when the installed codeburn is behind the latest release. Two ways out of it, as on the +/// mac: one click that runs the update here, and the command itself, copyable, for anyone +/// who would rather watch it happen in their own terminal. + +export function CLIUpdateBanner() { + const [update, setUpdate] = useState(EMPTY_UPDATE) + const [copied, setCopied] = useState(false) + useEffect(() => subscribeUpdate(setUpdate), []) + + const status = update.status + if (!status?.cliUpdateAvailable) return null + const command = status.cliUpdateCommand + + const copy = async () => { + try { + await navigator.clipboard.writeText(command) + setCopied(true) + setTimeout(() => setCopied(false), 1500) + } catch { + setCopied(false) + } + } + + return ( +
+ + CLI v{status.latestCliVersion} available + + + +
+ ) +} diff --git a/windows/src/components/Header.tsx b/windows/src/components/Header.tsx index 503c5a38..2911114e 100644 --- a/windows/src/components/Header.tsx +++ b/windows/src/components/Header.tsx @@ -1,11 +1,12 @@ import { useState } from 'react' import { ACCENT_PRESETS, type AccentPreset } from '../lib/accent' import { QuotaWarningRow } from './QuotaWarningRow' +import { UpdateBadge } from './UpdateBadge' import type { QuotaState } from '../lib/quota' /// Port of Header in mac/Sources/CodeBurnMenubar/Views/MenuBarContent.swift: the flame -/// wordmark and tagline on the left, the accent picker on the right, and the quota warning -/// row underneath. Package E adds the update badge between them. +/// wordmark and tagline on the left, the update badge and the accent picker on the right, +/// and the quota warning row underneath. type Props = { quota: QuotaState @@ -23,7 +24,10 @@ export function Header({ quota, showQuota, accent, onAccent, animate }: Props) {
Your AI Bill, Itemized
- +
+ + +
{showQuota && } diff --git a/windows/src/components/Icons.tsx b/windows/src/components/Icons.tsx index 597d5a41..eb34b64a 100644 --- a/windows/src/components/Icons.tsx +++ b/windows/src/components/Icons.tsx @@ -266,3 +266,13 @@ export function PowerIcon(p: IconProps) { ) } + +/// doc.on.doc, for the copyable command in the CLI update banner. +export function CopyIcon(p: IconProps) { + return ( + + + + + ) +} diff --git a/windows/src/components/UpdateBadge.tsx b/windows/src/components/UpdateBadge.tsx new file mode 100644 index 00000000..ceb2777a --- /dev/null +++ b/windows/src/components/UpdateBadge.tsx @@ -0,0 +1,43 @@ +import { useEffect, useState } from 'react' + +import { + EMPTY_UPDATE, badgeAction, badgeLabel, badgeVisible, checkUpdates, helpText, performUpdate, + subscribeUpdate, type UpdateState, +} from '../lib/update' +import { DownloadIcon, RefreshIcon, WarningIcon } from './Icons' + +/// Port of UpdateBadge in mac/.../Views/MenuBarContent.swift: a small prominent pill in the +/// header, there only when there is something to say. A failed check retries the check, an +/// offered update installs it in one click, and the tooltip carries the whole story. + +export function UpdateBadge() { + const [update, setUpdate] = useState(EMPTY_UPDATE) + useEffect(() => subscribeUpdate(setUpdate), []) + + if (!update.updating && !badgeVisible(update)) return null + + const label = badgeLabel(update) + const help = helpText(update) + const failed = update.status?.error != null + + return ( + + ) +} diff --git a/windows/src/lib/update.ts b/windows/src/lib/update.ts new file mode 100644 index 00000000..7472b5d0 --- /dev/null +++ b/windows/src/lib/update.ts @@ -0,0 +1,189 @@ +/// The one update source the page has. The header badge, the CLI banner and the About pane +/// all read this store, so one answer feeds them all rather than each asking Rust its own +/// question. +/// +/// Port of the observable half of mac/.../Data/UpdateChecker.swift. The check itself lives +/// in Rust (`src-tauri/src/update.rs`): it talks to GitHub, keeps the answer on disk and +/// only spends a request once the two-day interval is up, so asking on every mount is free. + +import { invoke } from '@tauri-apps/api/core' +import { listen } from '@tauri-apps/api/event' + +/// UpdateFailureStage: which half of the sequence failed, or which one is running. +export type UpdateStage = 'check' | 'cliUpdate' | 'menubarUpdate' + +export type UpdateStatus = { + currentVersion: string + latestVersion: string | null + latestCliVersion: string | null + installedCliVersion: string | null + updateAvailable: boolean + cliUpdateAvailable: boolean + /// Too old to install the app, whether or not it is also behind the latest release. + cliTooOld: boolean + cliUpdateCommand: string + checkedAt: number | null + failureStage: UpdateStage | null + error: string | null +} + +export type UpdateState = { + status: UpdateStatus | null + checking: boolean + updating: boolean + /// The stage the running update is on, for the label while it works. + stage: UpdateStage | null +} + +export const EMPTY_UPDATE: UpdateState = { + status: null, + checking: false, + updating: false, + stage: null, +} + +/// Rust decides whether a check costs a request; this only decides how often it is offered +/// the chance. Well inside the two-day interval, so an app left running for a week still +/// learns about a release, and far enough apart that the `codeburn --version` each ask +/// spawns is nothing beside the usage refresh. +const ASK_INTERVAL_MS = 12 * 60 * 60_000 + +type Listener = (state: UpdateState) => void + +let state: UpdateState = EMPTY_UPDATE +let listeners: Listener[] = [] +let timer: number | undefined +let inFlight: Promise | null = null +let stageListener: Promise<() => void> | null = null + +function publish(next: Partial) { + state = { ...state, ...next } + for (const listener of listeners) listener(state) +} + +/// `force` skips the two-day gate, which is what the buttons that say Check for Updates do. +export async function checkUpdates(force: boolean): Promise { + if (inFlight) return inFlight + publish({ checking: true }) + inFlight = (async () => { + try { + const status = await invoke('check_updates', { force }) + publish({ status, checking: false }) + } catch (err) { + // A command that refuses to run at all is still a failed check, and the badge says so + // rather than staying silent about a question nobody answered. + publish({ + checking: false, + status: { + ...(state.status ?? blankStatus()), + failureStage: 'check', + error: err instanceof Error ? err.message : String(err), + }, + }) + } + })().finally(() => { inFlight = null }) + return inFlight +} + +/// One click, both updates: Rust runs the CLI update and then the app install, and reports +/// the status the sequence ended on. +export async function performUpdate(): Promise { + if (state.updating) return + publish({ updating: true, stage: 'cliUpdate' }) + try { + const status = await invoke('perform_update') + publish({ status, updating: false, stage: null }) + } catch (err) { + publish({ + updating: false, + stage: null, + status: { + ...(state.status ?? blankStatus()), + failureStage: 'menubarUpdate', + error: err instanceof Error ? err.message : String(err), + }, + }) + } +} + +function blankStatus(): UpdateStatus { + return { + currentVersion: '', + latestVersion: null, + latestCliVersion: null, + installedCliVersion: null, + updateAvailable: false, + cliUpdateAvailable: false, + cliTooOld: false, + cliUpdateCommand: 'npm update -g codeburn', + checkedAt: null, + failureStage: null, + error: null, + } +} + +export function subscribeUpdate(listener: Listener): () => void { + listeners.push(listener) + listener(state) + if (listeners.length === 1) { + if (state.status === null) void checkUpdates(false) + timer = window.setInterval(() => { void checkUpdates(false) }, ASK_INTERVAL_MS) + // The running stage is what turns "Updating..." into something that says which half. + stageListener = listen('codeburn://update-stage', event => { + if (state.updating) publish({ stage: event.payload }) + }) + } + return () => { + listeners = listeners.filter(l => l !== listener) + if (listeners.length === 0) { + window.clearInterval(timer) + stageListener?.then(fn => fn()) + stageListener = null + } + } +} + +/// UpdateChecker.updateBadgeLabel. +export function badgeLabel(state: UpdateState): string { + if (state.updating) return 'Updating...' + switch (state.status?.failureStage) { + case 'check': return 'Update Check Failed' + case 'cliUpdate': return 'CLI Update Failed' + case 'menubarUpdate': return 'Menubar Update Failed' + default: return 'Update' + } +} + +const STAGE_SUMMARY: Record = { + check: 'CodeBurn could not check GitHub for updates.', + cliUpdate: 'CodeBurn could not update the CLI.', + menubarUpdate: 'CodeBurn could not update the tray app.', +} + +const STAGE_RETRY: Record = { + check: 'Click to retry the update check.', + cliUpdate: 'Click to retry the update.', + menubarUpdate: 'Click to retry the update.', +} + +/// UpdateChecker.updateHelpText. +export function helpText(state: UpdateState): string { + const stage = state.status?.failureStage + const error = state.status?.error + if (!stage || !error) return 'Update the CLI and the tray app to the latest release' + return `${STAGE_SUMMARY[stage]}\n\n${error}\n\n${STAGE_RETRY[stage]}` +} + +/// What a click on the badge does, from the mac's UpdateBadge: a failed check retries the +/// check, an offered update installs it, and anything else asks again. +export function badgeAction(state: UpdateState): 'check' | 'update' { + if (state.status?.failureStage === 'check') return 'check' + if (state.status?.updateAvailable || state.status?.cliUpdateAvailable) return 'update' + return 'check' +} + +export function badgeVisible(state: UpdateState): boolean { + const status = state.status + if (!status) return false + return status.updateAvailable || status.cliUpdateAvailable || status.error !== null +} diff --git a/windows/src/styles.css b/windows/src/styles.css index d41dc1d1..aa841ac5 100644 --- a/windows/src/styles.css +++ b/windows/src/styles.css @@ -40,6 +40,9 @@ --danger: #C83F2C; /* Routing waste, from the mac OptimizeInsight purple. */ --routing-ink: #8944AB; + /* The CLI update banner, from the mac's Color.blue: informational rather than branded, + so it never reads as the terracotta star banner two rows below it. */ + --update-ink: #007AFF; /* Quota severity, from QuotaSummary.Severity: under 50 green, then yellow, orange, red. Three sets, because one colour cannot do three jobs here. The bare token is the bar @@ -116,6 +119,7 @@ --sev-critical-tip: #C2610A; --sev-danger-tip: #C83F2C; --routing-ink: #BF5AF2; + --update-ink: #0A84FF; --icon-contrast: #1C1816; color-scheme: dark; } @@ -158,6 +162,7 @@ --sev-critical-tip: #C2610A; --sev-danger-tip: #C83F2C; --routing-ink: #BF5AF2; + --update-ink: #0A84FF; --icon-contrast: #1C1816; color-scheme: dark; } @@ -246,7 +251,30 @@ code { font-family: var(--font-mono); } .subhead { font-size: 10.5px; color: var(--label-2); } /* Accent picker: a dot showing the current preset, which reveals the nine swatches. */ +.header-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; } .accent-picker { display: flex; align-items: center; margin-left: auto; } + +/* The update badge: the mac's borderedProminent mini pill in the brand tint, sitting where + the header has room for it and gone entirely when there is nothing to update. */ +.update-badge { + display: inline-flex; + align-items: center; + gap: 4px; + height: 18px; + padding: 0 7px; + border: 0; + border-radius: var(--radius-pill); + background: var(--brand-accent); + color: #FFFFFF; + font-family: inherit; + font-size: 10px; + font-weight: 500; + white-space: nowrap; +} +.update-badge:hover:not(:disabled) { background: #B84A1A; } +.update-badge:active:not(:disabled) { background: var(--ember-deep); } +.update-badge:disabled { opacity: 0.75; } +.update-badge-spin { display: inline-flex; animation: spin 0.9s linear infinite; } .accent-swatches { display: flex; gap: 5px; @@ -1904,6 +1932,37 @@ code { font-family: var(--font-mono); } } .star-banner-close:hover { background: var(--fill-08); } +/* ---- CLI update banner ---------------------------------------------------------- */ + +.cli-banner { + display: flex; + align-items: center; + gap: 6px; + padding: 6px 12px; + background: color-mix(in srgb, var(--update-ink) 8%, transparent); + box-shadow: inset 0 0.5px 0 var(--fill-18); +} +.cli-banner-icon { color: var(--update-ink); display: inline-flex; flex-shrink: 0; } +.cli-banner-text { font-size: 10.5px; font-weight: 500; color: var(--label); white-space: nowrap; } +.cli-banner-action, +.cli-banner-copy { + display: inline-flex; + align-items: center; + gap: 3px; + padding: 0; + border: 0; + background: transparent; + color: var(--update-ink); + font-family: inherit; + font-size: 10px; + font-weight: 600; + white-space: nowrap; +} +.cli-banner-action:disabled { opacity: 0.6; } +.cli-banner-copy code { font-family: var(--font-mono); font-size: 10px; font-weight: 500; } +.cli-banner-copy:hover code { text-decoration: underline; } +.cli-banner-spacer { flex: 1; } + /* ---- error toast --------------------------------------------------------------- */ .error-toast {