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

5.0 KB
Raw Blame
import React, { useId, useMemo } from 'react';
import { useI18n, interpolate } from '../../i18n';
import type { SettingsDraft } from '../hooks/useSettingsDraft';
import { useContextPreview } from '../hooks/useContextPreview';
import { previewFacts } from '../utils/settings-preview';
import { formatCount } from '../utils/formatters';
import { settingsHash, timelineHash } from '../utils/memory-route';
import { LeaveQuestion } from './ContextSettingsModal';
import { TerminalPreview } from './TerminalPreview';
import { ChevronLeftIcon } from './icons';

interface ContextPreviewPageProps {
  /** The settings draft (App's useSettingsDraft): the preview's default source follows its "Include all sources". */
  draft: SettingsDraft;
  /** Opens the timeline on a project: where a shortened preview's left-out observations can all be read. */
  onShowInTimeline: (project: string) => void;
}

/**
 * The saved context preview as its own page (#/context-preview; design pass Phase 11, Codex D19), one link from every
 * settings category at every width. Its breadcrumb goes back to the category the person came from; the draft and its
 * save state wait in App meanwhile, so going back finds them as they were.
 */
export function ContextPreviewPage({ draft, onShowInTimeline }: ContextPreviewPageProps) {
  const { t } = useI18n();
  const idPrefix = useId();
  const {
    preview, isLoading, error, projects, sources, selectedSource, setSelectedSource, selectedProject, setSelectedProject,
  } = useContextPreview(draft.values);
  const facts = useMemo(() => previewFacts(preview), [preview]);
  const sourceId = `${idPrefix}-source`;
  const projectId = `${idPrefix}-project`;
  // '' is the "All sources" choice, as null is before the first choice: both read "All sources".
  const previewLabel = [selectedSource || t('All sources'), selectedProject].filter(Boolean).join(' · ');
  // Only for a preview that arrived: before the first read (300 ms after a choice) it would read "0 characters".
  const showPreviewSize = !isLoading && !error && !!selectedProject && preview !== '';
  const previewSize = showPreviewSize ? [
    interpolate(facts.shortened
      ? (facts.observations === 1 ? t('{count} observation shown') : t('{count} observations shown'))
      : (facts.observations === 1 ? t('{count} observation') : t('{count} observations')),
    { count: formatCount(facts.observations) }),
    interpolate(t('{count} characters'), { count: formatCount(facts.characters) }),
  ].join(' · ') : null;
  // Asks first when the draft has unsaved changes: the timeline is outside the settings pages.
  const showAllInTimeline = selectedProject
    ? () => draft.requestLeave(timelineHash(), () => onShowInTimeline(selectedProject))
    : undefined;

  return (
    <main className="mem-scroll settings-page context-preview-page" aria-labelledby={`${idPrefix}-title`}>
      <div className="mem-page mem-page--reading">
        <nav className="mem-crumbs" aria-label={t('Breadcrumb')}>
          <a href={settingsHash(draft.lastSection)}><ChevronLeftIcon size={14} />{t('Memory settings')}</a>
        </nav>
        <header className="mem-pagehead">
          <h1 className="mem-title" id={`${idPrefix}-title`} tabIndex={-1}>{t('Saved context preview')}</h1>
          {/* The size of the preview shown, once it is in; until then what the page shows. */}
          <p className="mem-leadtext context-preview-size">
            {previewSize ?? t('Preview the saved configuration for the selected source and project.')}
          </p>
        </header>

        {/* The two choices scope the preview below them, not the settings. */}
        <div className="context-preview-choices">
          <div className="mem-formrow">
            <label htmlFor={sourceId}>{t('Source')}</label>
            <select id={sourceId} className="mem-select context-preview-source" value={selectedSource || ''}
              onChange={event => setSelectedSource(event.target.value)} disabled={sources.length === 0}>
              <option value="">{t('All sources')}</option>
              {sources.map(source => <option key={source} value={source}>{source}</option>)}
            </select>
          </div>
          <div className="mem-formrow">
            <label htmlFor={projectId}>{t('Project')}</label>
            <select id={projectId} className="mem-select context-preview-project" value={selectedProject || ''}
              onChange={event => setSelectedProject(event.target.value)} disabled={projects.length === 0}>
              {projects.map(project => <option key={project} value={project}>{project}</option>)}
            </select>
          </div>
        </div>

        {error ? (
          <div className="context-preview-error" role="alert">{interpolate(t('Error loading preview: {error}'), { error })}</div>
        ) : (
          <TerminalPreview content={preview} isLoading={isLoading} label={previewLabel} onShowAll={showAllInTimeline} />
        )}

        {draft.leaveQuestion && (
          <footer className="settings-footer">
            <LeaveQuestion draft={draft} />
          </footer>
        )}
      </div>
    </main>
  );
}