import { useState } from 'react' import { useEffect, useRef } from 'react' import type { CSSProperties } from 'react' import { Icon } from '../components/icons' import { t } from '../i18n' import { codeburn } from '../lib/ipc' import type { MacMenubarStatus } from '../lib/types' import { MenuBarAboutModal } from './MenuBarAbout' import styles from './Plugins.module.css' import menubarArt from '../assets/menubar-card-art.jpg' import menubarArtLight from '../assets/menubar-card-art-light.jpg' /** The menubar app and the desktop app already share the CLI, the cache and the config, so the * card has no link step: install, open, and the one switch the app draws a window for. */ const POLL_MS = 4000 type Action = 'install' | 'open' | 'dock' | 'quit' | 'uninstall' | 'update' | 'settings' /** * Polls only while this card is mounted (the Plugins page unmounts on navigation) and only * while the window is showing, so a backgrounded app costs nothing. No global timer, and the * poll never sets state to a value equal to the one held, so a still machine re-renders zero * times (PR 1352). */ function useMacMenubarStatus(): [MacMenubarStatus | null, (next: MacMenubarStatus) => void, () => void] { const [status, setStatus] = useState(null) const held = useRef('') const apply = (next: MacMenubarStatus) => { const key = JSON.stringify(next) if (key === held.current) return held.current = key setStatus(next) } const refresh = () => { void codeburn?.macMenubarStatus?.().then(apply).catch(() => {}) } useEffect(() => { let live = true const tick = () => { if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return void codeburn?.macMenubarStatus?.().then(next => { if (live) apply(next) }).catch(() => {}) } tick() const timer = setInterval(tick, POLL_MS) return () => { live = false; clearInterval(timer) } // eslint-disable-next-line react-hooks/exhaustive-deps }, []) return [status, apply, refresh] } export function MenuBarCard({ art = menubarArt, artLight = menubarArtLight }: { art?: string; artLight?: string } = {}) { const [status, apply, refresh] = useMacMenubarStatus() const [busy, setBusy] = useState(null) const [error, setError] = useState(null) // Advice, not a failure: the install worked, an older copy is still on disk. The CLI says // this on stdout, where only terminal users ever saw it. const [notice, setNotice] = useState(null) // Quit and Uninstall confirm in the card, the same way removing a plugin does on this page. const [confirming, setConfirming] = useState<'quit' | 'uninstall' | null>(null) // What the install is doing right now, from the CLI's own narration. An install takes about // half a minute, most of it an 8 MB download, and a button that only says "Installing…" for // that long reads as a hang. const [phase, setPhase] = useState(null) const [about, setAbout] = useState(false) if (!status?.supported) return null const act = async (kind: Action, call: () => Promise) => { if (busy) return setBusy(kind) setError(null) setNotice(null) setPhase(null) try { await call() } catch (err) { setError(err instanceof Error ? err.message : t('plugins.menuBar.errorGeneric')) } finally { setBusy(null) setPhase(null) setConfirming(null) refresh() } } const runInstall = (kind: 'install' | 'update') => act(kind, async () => { const stop = codeburn.onMacMenubarProgress?.(setPhase) try { const result = await codeburn.macMenubarInstall?.() if (!result) return // What landed decides the card, so a release older than this build can drive still comes // back as the outdated state rather than a cheerful Running. apply(result.status) if (!result.ok) setError(result.error ?? t('plugins.menuBar.errorInstall')) else if (result.leftovers?.length) setNotice(t('plugins.menuBar.leftoverNote', { path: result.leftovers[0]! })) } finally { stop?.() } }) const install = () => runInstall('install') const update = () => runInstall('update') const settings = () => act('settings', async () => { const result = await codeburn.macMenubarSettings?.() if (!result) return apply(result.status) if (!result.ok) setError(result.error ?? t('plugins.menuBar.errorSettings')) }) const open = () => act('open', async () => { const next = await codeburn.macMenubarOpen?.() if (next) apply(next) }) const toggleDock = () => act('dock', async () => { const next = await codeburn.macMenubarSetDock?.(!status.dock) if (next) apply(next) }) const quit = () => act('quit', async () => { const result = await codeburn.macMenubarQuit?.() if (!result) return apply(result.status) if (!result.ok) setError(result.error ?? t('plugins.menuBar.errorQuit')) }) const uninstall = () => act('uninstall', async () => { const result = await codeburn.macMenubarUninstall?.() if (!result) return apply(result.status) if (!result.ok) setError(result.error ?? t('plugins.menuBar.errorUninstall')) }) return (
{t('plugins.menuBar.name')}
{t('plugins.menuBar.description')}
{/* One note row, always present, so the card is the same height in every state. An error answers something the person just pressed, so it wins over the hint. */}
{error ?? notice ?? (status.outdated ? t('plugins.menuBar.updateHint') : '')}
{/* The status line rides the bottom-left of the card, clear of the wordmark baked into the artwork's bottom-right. */}
{status.running && ( {t('plugins.menuBar.running')} )} {status.version && v{status.version}}
{status.installed && ( )}
{status.installed ? ( <> {status.outdated && status.canInstall && ( )} {/* Icon-only, all visible: no window to bring forward while it is up, so Open shows only while it is down; Settings, Quit and Uninstall while it is up. */} {!status.running && ( )} {status.running && ( )} {/* Destructive, so a light guard: the first click arms the icon (it swaps to a check), a second confirms, and moving focus away disarms it. */} {status.running && ( )} ) : status.canInstall ? ( ) : ( {t('plugins.menuBar.website')} )}
{about && setAbout(false)} />}
) }