Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

7.5 KB
Raw Blame
// The readable palettes and type choices both apps save, beside the theme (src/ui/theme-choice.ts): a curated warm
// palette for long reading, a font and a reading size. No imports and no React, so the pre-paint scripts, both apps'
// useAppearance hooks and the viewer bundle can all take the one definition.

/** The curated palettes, in the order both apps offer them. "Default" keeps the palette each theme already has. */
export const THEME_PRESETS = ['default', 'warm-cream', 'neutral-light', 'warm-tint', 'neutral-dark'] as const
export type ThemePreset = (typeof THEME_PRESETS)[number]

export const THEME_PRESET_LABELS: Record<ThemePreset, string> = {
  default: 'Current default',
  'warm-cream': 'Warm cream',
  'neutral-light': 'Neutral light',
  'warm-tint': 'Warm tint',
  'neutral-dark': 'Neutral dark',
}

/**
 * The theme each palette belongs to. A palette's tokens apply only under its theme (both stylesheets scope them), and
 * choosing a palette also chooses its theme, so a light palette never paints under dark components or the reverse.
 * "Default" belongs to neither: it keeps whichever theme is chosen.
 */
export const PALETTE_THEMES: Record<ThemePreset, 'light' | 'dark' | null> = {
  default: null,
  'warm-cream': 'light',
  'neutral-light': 'light',
  'warm-tint': 'light',
  'neutral-dark': 'dark',
}

/** The faces words can use. "Default" is the app's own (Geist); the others are faces people already have. */
export const FONT_CHOICES = ['default', 'sans', 'serif', 'mono'] as const
export type FontChoice = (typeof FONT_CHOICES)[number]

export const FONT_CHOICE_LABELS: Record<FontChoice, string> = {
  default: 'Geist',
  sans: 'System sans',
  serif: 'Serif',
  mono: 'Monospace',
}

/** How large reading text runs. Each also sets its line height (compact 1.45, default 1.54, comfortable 1.65). */
export const READING_CHOICES = ['compact', 'default', 'comfortable'] as const
export type ReadingChoice = (typeof READING_CHOICES)[number]

export const READING_CHOICE_LABELS: Record<ReadingChoice, string> = {
  compact: 'Compact',
  default: 'Default',
  comfortable: 'Comfortable',
}

/** The words the appearance controls show. Passed through t(); tests/viewer/theme-appearance.test.ts checks every
 * catalog has them, since the literal t('…') scan cannot see words picked from a table. */
export const APPEARANCE_WORDS = {
  appearance: 'Appearance',
  palette: 'Reading palette',
  font: 'Font',
  reading: 'Text size',
} as const

/** The localStorage key the appearance is saved under, beside the theme's `claude-mem-theme`. */
export const THEME_APPEARANCE_STORAGE_KEY = 'claude-mem-theme-appearance'

export interface ThemeAppearance {
  preset: ThemePreset
  font: FontChoice
  reading: ReadingChoice
}

/** What both apps use when nothing (or nothing readable) is saved. */
export const DEFAULT_THEME_APPEARANCE: ThemeAppearance = Object.freeze({
  preset: 'default',
  font: 'default',
  reading: 'default',
})

/** The part of localStorage the appearance uses; left out, the browser's localStorage. */
export type AppearanceStorage = Pick<Storage, 'getItem' | 'setItem'>

export function isThemePreset(value: unknown): value is ThemePreset {
  return (THEME_PRESETS as readonly unknown[]).includes(value)
}

export function isFontChoice(value: unknown): value is FontChoice {
  return (FONT_CHOICES as readonly unknown[]).includes(value)
}

export function isReadingChoice(value: unknown): value is ReadingChoice {
  return (READING_CHOICES as readonly unknown[]).includes(value)
}

/** A saved or hand-edited value in this version's meaning: each field a choice it knows, else its default. */
export function normalizeThemeAppearance(value: unknown): ThemeAppearance {
  const saved = (value ?? {}) as Partial<Record<keyof ThemeAppearance, unknown>>
  return {
    preset: isThemePreset(saved.preset) ? saved.preset : DEFAULT_THEME_APPEARANCE.preset,
    font: isFontChoice(saved.font) ? saved.font : DEFAULT_THEME_APPEARANCE.font,
    reading: isReadingChoice(saved.reading) ? saved.reading : DEFAULT_THEME_APPEARANCE.reading,
  }
}

/**
 * The saved appearance, or the default when none is saved or the saved value is not readable. Storage that cannot be
 * read (blocked site data) is said in the console and reads as the default, as the theme's saved value does.
 */
export function readSavedThemeAppearance(storage?: AppearanceStorage): ThemeAppearance {
  try {
    const saved = (storage ?? localStorage).getItem(THEME_APPEARANCE_STORAGE_KEY)
    return saved === null ? DEFAULT_THEME_APPEARANCE : normalizeThemeAppearance(JSON.parse(saved))
  } catch (error: unknown) {
    console.warn('The saved reading appearance could not be read, so the default is used:', error instanceof Error ? error.message : String(error))
    return DEFAULT_THEME_APPEARANCE
  }
}

/** Saves the appearance for both apps. Storage that cannot be written is said in the console; the choice then lasts only while the page is open. */
export function saveThemeAppearance(appearance: ThemeAppearance, storage?: AppearanceStorage): ThemeAppearance {
  const normalized = normalizeThemeAppearance(appearance)
  try {
    (storage ?? localStorage).setItem(THEME_APPEARANCE_STORAGE_KEY, JSON.stringify(normalized))
  } catch (error: unknown) {
    console.warn('The reading appearance could not be saved, so it lasts only while this page is open:', error instanceof Error ? error.message : String(error))
  }
  return normalized
}

/** Said on window when this page changes the appearance; another tab's change arrives as a storage event. */
const APPEARANCE_CHANGE_EVENT = 'claude-mem-theme-appearance-change'
let current: ThemeAppearance | null = null
/** The page's window as an event target. Typed structurally, since this file is also compiled without the DOM library. */
const pageEvents = (): EventTarget => globalThis as unknown as EventTarget

/** The appearance in use on this page (useSyncExternalStore's snapshot): read once, then kept in step by changes. */
export function themeAppearanceSnapshot(): ThemeAppearance {
  if (current === null) current = readSavedThemeAppearance()
  return current
}

/** Calls `onChange` when this page or another tab changes the appearance (useSyncExternalStore's subscribe). */
export function subscribeThemeAppearance(onChange: () => void): () => void {
  const onStorage = (event: Event) => {
    const { key } = event as Event & { key: string | null }
    if (key === null || key === THEME_APPEARANCE_STORAGE_KEY) { current = null; onChange() }
  }
  // A change made here already set `current`; it holds even when storage is blocked, as the theme keeps its choice.
  pageEvents().addEventListener(APPEARANCE_CHANGE_EVENT, onChange)
  pageEvents().addEventListener('storage', onStorage)
  return () => {
    pageEvents().removeEventListener(APPEARANCE_CHANGE_EVENT, onChange)
    pageEvents().removeEventListener('storage', onStorage)
  }
}

/** Saves a change for both apps and tells this page's subscribers. */
export function updateThemeAppearance(change: Partial<ThemeAppearance>): void {
  current = saveThemeAppearance({ ...themeAppearanceSnapshot(), ...change })
  pageEvents().dispatchEvent(new Event(APPEARANCE_CHANGE_EVENT))
}

/** Puts the appearance on <html> as the attributes both stylesheets read: its palette, face and reading size. */
export function applyThemeAppearance(root: { setAttribute(name: string, value: string): void }, appearance: ThemeAppearance): void {
  root.setAttribute('data-palette', appearance.preset)
  root.setAttribute('data-font', appearance.font)
  root.setAttribute('data-reading', appearance.reading)
}