Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

3.0 KB
Raw Blame
'use client';

import { useTrackAnalytics } from '../analytics/AnalyticsProvider.js';
import { AnalyticsEvents } from '../analytics/events.js';
import { useSessionShareSearch } from '../hooks/useSessionShareSearch.js';
import { Icon } from '../icons/Icon.js';
import { useOptionalAgentSessionsServer } from '../server/ServerContext.js';
import { useOptionalShellMode } from '../server/ShellModeContext.js';
import { isSessionsChromeEnabled } from '../server/serverChrome.js';
import { defaultSessionTimeRange, readSessionShareSearch } from '../utils/sessionShareUrl.js';
import { auiButtonClass, sidebarButtonRadiusClassName, sidebarRailButtonClassName } from './lib/buttonClasses.js';
import { cn } from './lib/cn.js';

export type SessionsBrowserButtonProps = {
  className?: string;
  compact?: boolean;
};

export function SessionsBrowserButton({ className, compact = false }: SessionsBrowserButtonProps) {
  const sessionsServer = useOptionalAgentSessionsServer();
  const shell = useOptionalShellMode();
  const track = useTrackAnalytics();
  const { updateShareSearch } = useSessionShareSearch();
  const sessionsOpen = shell?.sessionsOpen === true;
  const enabled = isSessionsChromeEnabled({ sessions: sessionsServer }) && shell != null;

  if (!enabled) return null;

  return (
    <div className={cn('relative min-w-0', compact ? 'flex justify-center' : 'w-full', className)}>
      <button
        type="button"
        aria-label={compact ? 'Sessions' : undefined}
        title={compact ? 'Sessions' : undefined}
        aria-current={sessionsOpen ? 'page' : undefined}
        className={auiButtonClass({
          variant: 'ghost',
          className: cn(
            sidebarButtonRadiusClassName,
            'font-normal text-sidebar-text shadow-none hover:bg-secondary-button-hover hover:text-ghost-button-text',
            compact ? sidebarRailButtonClassName : 'h-8 w-full !justify-start px-2.5 text-sm',
            sessionsOpen &&
              'bg-primary-button-bg font-medium text-primary-button-text hover:bg-primary-button-hover hover:text-primary-button-text',
          ),
        })}
        onClick={() => {
          if (!sessionsOpen) {
            track(AnalyticsEvents.SessionsBrowser.OPENED);
            const share = readSessionShareSearch(window.location.search);
            updateShareSearch({
              view: 'sessions',
              agentId: null,
              sessionId: null,
              timeRange: share.timeRange ?? defaultSessionTimeRange(),
            });
          }
          shell.setSessionsOpen(true);
        }}
      >
        <Icon name="message-square-text" size={compact ? 14 : undefined} />
        {compact ? (
          <span className="text-center">Sessions</span>
        ) : (
          <>
            <span className="truncate">Sessions</span>
            <Icon name="chevron-right" className="ml-auto size-3.5 shrink-0 opacity-60" />
          </>
        )}
      </button>
    </div>
  );
}

declare module '../theme/SlotsProvider.js' {
  interface AtomSlots {
    SessionsBrowserButton: typeof SessionsBrowserButton;
  }
}