Skip to content

The agent that grows with you

feat(desktop): butterbar store — register notices from anywhere

Brooklyn Nicholsona10faf9e5ade
diff --git a/apps/desktop/src/store/butterbar.test.ts b/apps/desktop/src/store/butterbar.test.ts
new file mode 100644
index 00000000000..bc95a825953
--- /dev/null
+++ b/apps/desktop/src/store/butterbar.test.ts
@@ -0,0 +1,60 @@
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { $butterbarDismissed, $butterbarItems, dismissButterbar, registerButterbar } from './butterbar'
+
+const offs: (() => void)[] = []
+
+const register = (...args: Parameters<typeof registerButterbar>) => {
+ const off = registerButterbar(...args)
+ offs.push(off)
+
+ return off
+}
+
+const ids = () => $butterbarItems.get().map(item => item.id)
+
+afterEach(() => {
+ offs.splice(0).forEach(off => off())
+ $butterbarDismissed.set([])
+})
+
+describe('butterbar store', () => {
+ it('is empty until something registers', () => {
+ expect(ids()).toEqual([])
+ })
+
+ it('orders by priority, then registration order, and re-registering replaces in place', () => {
+ register({ id: 'a', node: 'a' })
+ register({ id: 'b', node: 'b', priority: 2 })
+ register({ id: 'c', node: 'c' })
+ register({ id: 'a', node: 'a2' })
+
+ expect(ids()).toEqual(['b', 'a', 'c'])
+ expect($butterbarItems.get()[1].node).toBe('a2')
+ })
+
+ it('a stale unregister does not remove the item that replaced it', () => {
+ const off = register({ id: 'a', node: 'first' })
+ register({ id: 'a', node: 'second' })
+ off()
+
+ expect($butterbarItems.get().map(item => item.node)).toEqual(['second'])
+ })
+
+ it('a keyed close persists across re-registration; a keyless close lasts this run', () => {
+ const keyed = { id: 'keyed', node: 'k', persistKey: 'notice-v1' }
+ const off = register(keyed)
+ register({ id: 'keyless', node: 'x', closeable: true })
+
+ dismissButterbar(keyed)
+ dismissButterbar($butterbarItems.get().find(item => item.id === 'keyless')!)
+ off()
+ register(keyed)
+
+ expect(ids()).toEqual([])
+ expect($butterbarDismissed.get()).toEqual(['notice-v1'])
+
+ register({ ...keyed, persistKey: 'notice-v2' })
+ expect(ids()).toEqual(['keyed'])
+ })
+})
diff --git a/apps/desktop/src/store/butterbar.ts b/apps/desktop/src/store/butterbar.ts
new file mode 100644
index 00000000000..c26586f78af
--- /dev/null
+++ b/apps/desktop/src/store/butterbar.ts
@@ -0,0 +1,85 @@
+import { atom, computed } from 'nanostores'
+import { type ReactNode, useEffect } from 'react'
+
+import { Codecs, persistentAtom } from '@/lib/persisted'
+
+export type ButterbarTone = 'accent' | 'danger' | 'info' | 'neutral' | 'success' | 'warn'
+
+export interface ButterbarItem {
+ id: string
+ node: ReactNode
+ /** Shows a close button. Implied by `persistKey`. */
+ closeable?: boolean
+ /** Remembers the close across launches under this key. Without it a close
+ * lasts for this run only. Bump the key to show a notice again. */
+ persistKey?: string
+ tone?: ButterbarTone
+ /** Any CSS color. Fill, text, links and dots all derive from it with
+ * color-mix, so one value restyles the whole bar. Wins over `tone`. */
+ color?: string
+ /** `soft` tints the chrome (default); `solid` paints the color itself. */
+ variant?: 'soft' | 'solid'
+ icon?: ReactNode
+ /** Higher sorts first; ties keep registration order. */
+ priority?: number
+ /** Autoplay dwell for this slide when the bar holds several. */
+ durationMs?: number
+}
+
+const sanitizeKeys = (value: unknown): string[] =>
+ Array.isArray(value) ? value.filter((key): key is string => typeof key === 'string' && key.length > 0) : []
+
+const $registered = atom<readonly ButterbarItem[]>([])
+const $closedThisRun = atom<readonly string[]>([])
+
+export const $butterbarDismissed = persistentAtom<string[]>(
+ 'hermes.desktop.butterbar.dismissed',
+ [],
+ Codecs.json<string[]>(sanitizeKeys)
+)
+
+export const $butterbarItems = computed([$registered, $closedThisRun, $butterbarDismissed], (items, closed, dismissed) =>
+ items
+ .filter(item => !closed.includes(item.id) && !(item.persistKey && dismissed.includes(item.persistKey)))
+ .map((item, order) => ({ item, order }))
+ .sort((a, b) => (b.item.priority ?? 0) - (a.item.priority ?? 0) || a.order - b.order)
+ .map(({ item }) => item)
+)
+
+/** Put a notice in the bar from anywhere. Re-registering an id replaces it in
+ * place. Returns the unregister. */
+export function registerButterbar(item: ButterbarItem): () => void {
+ const current = $registered.get()
+ const at = current.findIndex(entry => entry.id === item.id)
+
+ $registered.set(at < 0 ? [...current, item] : current.map((entry, i) => (i === at ? item : entry)))
+
+ return () => {
+ if ($registered.get().includes(item)) {
+ $registered.set($registered.get().filter(entry => entry !== item))
+ }
+ }
+}
+
+export function dismissButterbar(item: ButterbarItem) {
+ if (item.persistKey) {
+ const dismissed = $butterbarDismissed.get()
+
+ if (!dismissed.includes(item.persistKey)) {
+ $butterbarDismissed.set([...dismissed, item.persistKey])
+ }
+
+ return
+ }
+
+ $closedThisRun.set([...$closedThisRun.get(), item.id])
+}
+
+export function isButterbarCloseable(item: ButterbarItem): boolean {
+ return item.closeable ?? Boolean(item.persistKey)
+}
+
+/** Component-scoped registration: shows while mounted (and `item` is set). */
+export function useButterbar(item: ButterbarItem | null) {
+ useEffect(() => (item ? registerButterbar(item) : undefined), [item])
+}