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.

8.6 KB
Raw Blame
import { useState } from 'react'
import { AccountChip, useAccountChip } from './AccountChip'
import { IconBellFilled, IconCircleCheck, IconClose } from './icons'
import { NodeIconView } from './NodeIcon'
import { ProjectGlyph } from './ProjectGlyph'
import type { SessionRowVM } from '../lib/sessionList'
import { sessionStateAgeTitle } from '../lib/sessionList'
import { useContextWindow } from '../state/contextWindow'
import { useSessionNaming } from '../state/sessionNaming'
import { useSettings } from '../state/settings'
import { contextFillColor, contextPillText, percentText } from '../lib/usageFormat'

export interface SessionRowProps {
  row: SessionRowVM
  onClick(): void
  onClose(): void
  onRename(title: string): void
  onAiName(): void | Promise<void>
  onContextMenu(e: React.MouseEvent): void
  onDragStart(): void
  onDragEnd(): void
  /** Status-group mode only: elapsed time since the current state began. */
  stateAgeLabel?: string
}

function dirName(p?: string): string {
  if (!p) return ''
  const parts = p.replace(/\/+$/, '').split('/')
  return parts[parts.length - 1] || p
}

export function SessionRow({
  row,
  onClick,
  onClose,
  onRename,
  onAiName,
  onContextMenu,
  onDragStart,
  onDragEnd,
  stateAgeLabel
}: SessionRowProps): JSX.Element {
  const [editing, setEditing] = useState(false)
  const [draft, setDraft] = useState(row.title)
  // Naming progress lives in a store keyed by node id, so the spinner persists across the row
  // unmounting (sidebar close / hover-peek collapse) while the name is still generating.
  const naming = useSessionNaming((s) => !!s.byId[row.id])
  const usage = useContextWindow((s) => (row.sessionId ? s.bySessionId[row.sessionId] : undefined))
  const percentMode = useSettings((s) => s.settings.usagePercentMode)
  // The sidebar is one more view of the same nodes, so it gets the canvas header's account chip
  // under the same visibility rule — two rows on two Claude logins are otherwise indistinguishable.
  const accountChip = useAccountChip(row.accountId, row.account, undefined, row.sshHostKey)

  const commit = (): void => {
    const t = draft.trim()
    if (t && t !== row.title) onRename(t)
    setEditing(false)
  }

  const aiName = (e: React.MouseEvent): void => {
    e.stopPropagation()
    if (naming) return
    void onAiName()
  }

  return (
    <div
      className="ss-row"
      draggable={!editing}
      onClick={onClick}
      onContextMenu={onContextMenu}
      onMouseDown={(e) => {
        // Middle-click closes the session, same as the × button — but goes through
        // onClose's confirm dialog rather than skipping it (killing a real tmux
        // session isn't the same low-stakes action as closing a browser tab).
        if (e.button === 1) {
          e.preventDefault()
          e.stopPropagation()
          onClose()
        }
      }}
      onDragStart={(e) => {
        e.dataTransfer.effectAllowed = 'move'
        // Some browsers require data to be set for a drag to start.
        e.dataTransfer.setData('text/plain', row.id)
        onDragStart()
      }}
      onDragEnd={onDragEnd}
    >
      {row.statusKind === 'attention' ? (
        // Needs-you rings a bell — louder than one more colored dot.
        <span className="ss-bell" title={row.stateLabel}>
          <IconBellFilled />
        </span>
      ) : row.statusKind !== 'working' && row.unread ? (
        // Finished before its live state became unknown while the user wasn't looking: the check,
        // but accent-blue and pulsing until they visit the node. Working/attention win —
        // a new turn or a permission prompt is more urgent than an old unread mark.
        <span className="ss-check ss-check--unread" title="Finished — new for you">
          <IconCircleCheck />
        </span>
      ) : (
        <span className={`ss-dot ss-dot--${row.statusKind}`} title={row.stateLabel} />
      )}
      <div className="ss-row__body">
        <div className="ss-row__titleline">
          {/* Ahead of the project monogram: the icon identifies the SESSION, and a row that is
              already carrying a status dot, a monogram and a context pill needs its most specific
              mark first. `projectId` is passed because status mode flattens rows across projects,
              so the active project is not necessarily this row's. */}
          <NodeIconView icon={row.icon} size={13} className="ss-row__icon" projectId={row.projectId} />
          {row.projectColor ? (
            // Status mode: rows are flattened across projects, so each row shows its project's
            // icon (or, absent one, the monogram — colored circle with the project initial)
            // instead of the plain color mark.
            <ProjectGlyph
              icon={row.projectIcon}
              color={row.projectColor}
              name={row.projectName ?? ''}
              variant="monogram"
              className="ss-mark ss-mark--project"
              title={row.projectName}
            />
          ) : (
            <ProjectGlyph color={row.color} name="" variant="dot" className="ss-mark" />
          )}
          {editing ? (
            <input
              className="ss-title-input"
              autoFocus
              value={draft}
              onClick={(e) => e.stopPropagation()}
              onChange={(e) => setDraft(e.target.value)}
              onBlur={commit}
              onKeyDown={(e) => {
                if (e.key === 'Enter') commit()
                if (e.key === 'Escape') setEditing(false)
              }}
            />
          ) : (
            <span
              className={`ss-title${row.unread ? ' is-unread' : ''}`}
              onDoubleClick={(e) => {
                e.stopPropagation()
                setDraft(row.title)
                setEditing(true)
              }}
            >
              {row.title}
            </span>
          )}
          {/* `title`, because the chip now yields to the name and can be ellipsised: a truncated
              chip with no tooltip is the one state where the session name is unrecoverable. */}
          {row.session && (
            <span className="ss-chip" title={row.session}>
              {row.session}
            </span>
          )}
          <AccountChip chip={accountChip} className="ss-account" />
          {row.loop && (
            <span className="ss-loop">
              {row.loop.kind} · {row.loop.count}
            </span>
          )}
          {row.usesContext && usage && (
            <span
              className="ss-ctx"
              title={`${usage.windowSource === 'estimate' ? 'Estimated context window' : 'Context window'} — ${percentText(usage.usedPercent, percentMode)}`}
              style={{ background: contextFillColor(usage.usedPercent) }}
            >
              {usage.windowSource === 'estimate' ? '~' : ''}{contextPillText(usage.usedTokens, usage.windowTokens, usage.usedPercent, percentMode)}
            </span>
          )}
          {/* Both buttons are invisible until the row is hovered, yet they used to hold 46px of a
              253px line — a quarter of it — away from the NAME. The cluster is taken out of flow
              and floated over the line's tail on hover instead: the tail is what an ellipsis was
              already eating, and the name gets those pixels back at every other moment. */}
          <span className="ss-row__actions">
            <button
              className="ss-row__ai"
              title="Name with AI (from terminal output)"
              disabled={naming}
              onClick={aiName}
            >
              {naming ? '…' : '✦'}
            </button>
            <button
              className="ss-row__close"
              title="End session"
              onClick={(e) => {
                e.stopPropagation()
                onClose()
              }}
            >
              <IconClose />
            </button>
          </span>
        </div>
        {(row.projectName || row.cwd || row.sshHost || stateAgeLabel) && (
          <div className="ss-meta">
            {row.projectName && <span className="ss-meta__project">{row.projectName}</span>}
            {row.sshHost && <span className="ss-meta__ssh">⇅ {row.sshHost}</span>}
            {row.cwd && <span className="ss-meta__cwd">{dirName(row.cwd)}</span>}
            {stateAgeLabel && (
              <span
                className={`ss-meta__state-age${row.statusClock === 'restored' ? ' ss-meta__state-age--restored' : ''}`}
                title={sessionStateAgeTitle(stateAgeLabel, row.statusClock, row.lastSeenState)}
              >
                {stateAgeLabel}
              </span>
            )}
          </div>
        )}
      </div>
    </div>
  )
}