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

1.7 KB
Raw Blame
import { useState, useEffect } from 'react';
import { readSavedThemeChoice, resolveTheme, saveThemeChoice, type ThemeChoice } from '../../theme-choice';

/**
 * The theme choices, in the order the "…" menu offers them, from their one definition (src/ui/theme-choice.ts), which
 * My Work uses too: a choice saved in either app is the other's on its next load.
 */
export { THEME_CHOICES as THEME_PREFERENCES, type ThemeChoice as ThemePreference } from '../../theme-choice';

const SYSTEM_DARK_QUERY = '(prefers-color-scheme: dark)';

/**
 * The saved choice on <html> as `data-theme`, following the operating system while the choice is "system". The inline
 * script in viewer-template.html's <head> sets the same attribute from the same saved choice before the first paint.
 */
export function useTheme() {
  const [preference, setPreference] = useState<ThemeChoice>(() => readSavedThemeChoice() ?? 'system');

  useEffect(() => {
    document.documentElement.setAttribute('data-theme', resolveTheme(preference, window.matchMedia(SYSTEM_DARK_QUERY).matches));
  }, [preference]);

  useEffect(() => {
    if (preference !== 'system') return;

    const mediaQuery = window.matchMedia(SYSTEM_DARK_QUERY);
    const handleChange = (e: MediaQueryListEvent) => {
      document.documentElement.setAttribute('data-theme', resolveTheme(preference, e.matches));
    };

    mediaQuery.addEventListener('change', handleChange);
    return () => mediaQuery.removeEventListener('change', handleChange);
  }, [preference]);

  const setThemePreference = (newPreference: ThemeChoice) => {
    saveThemeChoice(newPreference);
    setPreference(newPreference);
  };

  return {
    preference,
    setThemePreference
  };
}