Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(browser): browserHistory store (localStorage, addEntry)

enes4a76c1382e88
diff --git a/src/renderer/state/browserHistory.test.ts b/src/renderer/state/browserHistory.test.ts
new file mode 100644
index 000000000..80cb4a433
--- /dev/null
+++ b/src/renderer/state/browserHistory.test.ts
@@ -0,0 +1,20 @@
+import { describe, it, expect } from 'vitest'
+import { addEntry, type HistoryEntry } from './browserHistory'
+
+const e = (url: string, ts: number): HistoryEntry => ({ url, title: url, ts })
+
+describe('addEntry', () => {
+ it('prepends the newest entry', () => {
+ const out = addEntry([e('a', 1)], e('b', 2), 10)
+ expect(out.map((x) => x.url)).toEqual(['b', 'a'])
+ })
+ it('dedups by url, bumping a revisit to the top', () => {
+ const out = addEntry([e('a', 1), e('b', 2)], e('a', 3), 10)
+ expect(out.map((x) => x.url)).toEqual(['a', 'b'])
+ expect(out[0].ts).toBe(3)
+ })
+ it('enforces the cap (drops the oldest)', () => {
+ const out = addEntry([e('a', 1), e('b', 2)], e('c', 3), 2)
+ expect(out.map((x) => x.url)).toEqual(['c', 'a'])
+ })
+})
diff --git a/src/renderer/state/browserHistory.ts b/src/renderer/state/browserHistory.ts
new file mode 100644
index 000000000..ece3cb656
--- /dev/null
+++ b/src/renderer/state/browserHistory.ts
@@ -0,0 +1,46 @@
+import { create } from 'zustand'
+
+export interface HistoryEntry {
+ url: string
+ title: string
+ ts: number
+}
+
+export const HISTORY_CAP = 100
+const KEY = 'nodeterm.browserHistory'
+
+/** Prepend `entry`, drop any existing entry with the same url (a revisit bumps to top), cap length. */
+export function addEntry(list: HistoryEntry[], entry: HistoryEntry, cap: number): HistoryEntry[] {
+ return [entry, ...list.filter((e) => e.url !== entry.url)].slice(0, cap)
+}
+
+function load(): HistoryEntry[] {
+ try {
+ const parsed = JSON.parse(localStorage.getItem(KEY) ?? '[]')
+ return Array.isArray(parsed) ? (parsed as HistoryEntry[]) : []
+ } catch {
+ return []
+ }
+}
+
+interface HistoryState {
+ entries: HistoryEntry[]
+ /** Record a visited page. Ignores non-http(s) urls (e.g. about:blank) so the start page isn't logged. */
+ record: (url: string, title: string) => void
+ recent: (n: number) => HistoryEntry[]
+}
+
+export const useBrowserHistory = create<HistoryState>((set, get) => ({
+ entries: load(),
+ record: (url, title) => {
+ if (!/^https?:\/\//i.test(url)) return
+ const next = addEntry(get().entries, { url, title: title || url, ts: Date.now() }, HISTORY_CAP)
+ set({ entries: next })
+ try {
+ localStorage.setItem(KEY, JSON.stringify(next))
+ } catch {
+ /* best-effort */
+ }
+ },
+ recent: (n) => get().entries.slice(0, n)
+}))