Skip to content

The agent that grows with you

14.0 KB
Raw Blame
/**
 * PREVIEW TILES — every open preview (a file, a URL, an artifact) rendered as a
 * layout-tree pane, the preview analog of session and route tiles.
 *
 * The rail used to bring its OWN tab strip: a second bar beside the zone's own,
 * at a different height, with its own close menu and its own label casing. It
 * predated the layout tree. Now `$previewTabs` mirrors into pane contributions
 * through the same `paneMirror` the other tiles use, so a preview tab IS a zone
 * tab — same strip, same drag/stack/split, same ⌘W, same right-click verbs, and
 * one bar instead of two.
 */

import { useStore } from '@nanostores/react'

import { findGroup } from '@/components/pane-shell/tree/model'
import {
  $activeTreeGroup,
  $layoutTree,
  closeTabPane,
  revealTreePane,
  treePanesWithPrefix
} from '@/components/pane-shell/tree/store'
import { type MenuKit, renderActionItem } from '@/components/ui/actions-menu'
import { FileTypeIcon } from '@/components/ui/file-type-icon'
import { ToolIcon } from '@/components/ui/tool-icon'
import { translateNow } from '@/i18n'
import { openExternalLink } from '@/lib/external-link'
import { $rightRailActiveTabId, type RightRailTabId, selectRightRailTab } from '@/store/layout'
import {
  $browserPages,
  $dockedVisiblePreviewTabs,
  $poppedBrowserTabIds,
  $previewTabs,
  $visiblePreviewTabs,
  adoptPersistedBrowserTab,
  type BrowserPage,
  closeRightRailTab,
  forgetBrowserPage,
  markBrowserTabPopped,
  newBrowserTab,
  popOutBrowserTab,
  preferredVisibleTabId,
  type PreviewTarget,
  setPreviewTabPinned
} from '@/store/preview'
import { explicitOpenBlocksZone, PREVIEW_TILE_PREFIX } from '@/store/preview-explicit'
import { canOpenBrowserWindow } from '@/store/windows'

import { paneMirror } from './pane-mirror'
import { PreviewTilePane } from './right-rail/preview'
import { forgetPreviewConsole } from './right-rail/preview-console-store'

/** The target behind a tile id, or null once its tab is gone. */
function targetFor(tabId: string): PreviewTarget | null {
  return $previewTabs.get().find(tab => tab.id === tabId)?.target ?? null
}

/** Schemes that are not a page the user's default browser can usefully open
 *  (blank vessel, Chromium internals, injected documents). */
const NON_EXTERNAL_URL = /^(about|blob|chrome|data|devtools|javascript):/i

/** The URL a Browser tab should hand to the OS browser — the page it is
 *  showing now, else the address it was opened with. Null when there isn't
 *  one (`about:blank`, a half-typed address, a file/artifact tab). */
export function browserTabExternalUrl(tabId: string): null | string {
  const target = targetFor(tabId)

  if (target?.kind !== 'url') {
    return null
  }

  const url = $browserPages.get()[tabId]?.url || target.url

  return url && !NON_EXTERNAL_URL.test(url) ? url : null
}

function browserTabMenuPrefix(tabId: string) {
  if (targetFor(tabId)?.kind !== 'url') {
    return undefined
  }

  return (kit: MenuKit) => (
    <>
      {canOpenBrowserWindow()
        ? renderActionItem(kit, {
            icon: 'empty-window',
            key: 'pop-out',
            label: translateNow('preview.popOut'),
            onSelect: () => popOutBrowserTab(tabId)
          })
        : null}
      {renderActionItem(kit, {
        disabled: !browserTabExternalUrl(tabId),
        icon: 'link-external',
        key: 'open-external',
        label: translateNow('preview.openInExternal'),
        onSelect: () => openExternalLink(browserTabExternalUrl(tabId) ?? '')
      })}
    </>
  )
}

/** Pin/unpin for EVERY preview tab's zone menu: pinned tabs are the explicit
 *  cross-session workspace, everything else belongs to the session that
 *  opened it (#73890). URL tabs keep their browser rows below it. */
function previewTabMenuPrefix(tabId: string) {
  const browserRows = browserTabMenuPrefix(tabId)

  // Pin state is read when the menu renders and again on select, never at
  // registration: the mirror keeps a same-title registration (and so this
  // closure) for the tab's whole life, so a captured flag goes stale after the
  // first toggle and the row keeps offering — and re-applying — the same pin.
  const isPinned = () => Boolean($previewTabs.get().find(tab => tab.id === tabId)?.pinned)

  return (kit: MenuKit) => {
    const pinned = isPinned()

    return (
      <>
        {renderActionItem(kit, {
          icon: pinned ? 'pinned' : 'pin',
          key: 'pin',
          label: translateNow(pinned ? 'preview.unpin' : 'preview.pin'),
          onSelect: () => setPreviewTabPinned(tabId, !isPinned())
        })}
        {browserRows?.(kit) ?? null}
      </>
    )
  }
}

/** Tab title. A URL tab is titled by the CONTRIBUTION as the surface — see
 *  `BrowserTabLabel` for the live page name the strip actually renders — so
 *  navigating doesn't re-register the pane on every hop. A file names the
 *  file; an artifact is titled rather than located, so its label is the whole
 *  name. */
function previewTitle(tabId: string): string {
  const target = targetFor(tabId)

  if (!target) {
    return 'Preview'
  }

  if (target.kind === 'url') {
    return 'Browser'
  }

  if (target.kind === 'artifact') {
    return target.label || 'Preview'
  }

  const value = target.label || target.path || target.source || target.url
  const tail = value.split(/[\\/]/).filter(Boolean).at(-1)

  return tail || value || 'Preview'
}

/**
 * What to call a Browser tab. Its page title, else the host it is on, else
 * the surface — the ladder every browser walks, and the reason more than one
 * Browser is usable at all: three tabs reading "Browser" name nothing.
 *
 * A page that never set a title reports its own address as one, which is a
 * worse label than the host it came from, so an address-shaped title falls
 * through.
 */
export function browserTabLabel(target: PreviewTarget, page?: BrowserPage): string {
  const url = page?.url || target.url
  const title = page?.title.trim()

  if (title && title !== url) {
    return title
  }

  try {
    return new URL(url).hostname.replace(/^www\./, '') || 'Browser'
  } catch {
    // `about:blank` and half-typed addresses have no host to fall back to.
    return 'Browser'
  }
}

/** Live tab label for a Browser: it renames itself as the page navigates,
 *  without the contribution re-registering (see PaneChrome.tabTitle). */
function BrowserTabLabel({ tabId }: { tabId: string }) {
  const pages = useStore($browserPages)
  const target = targetFor(tabId)

  return target ? browserTabLabel(target, pages[tabId]) : null
}

/** The tab's lead glyph — the same file/tool icon family the file tree and code
 *  fences resolve through, so a `.tsx` peek and its sidebar row agree. */
function PreviewTabLead({ tabId }: { tabId: string }) {
  const target = targetFor(tabId)

  if (!target) {
    return null
  }

  if (target.kind === 'artifact') {
    return <ToolIcon className="opacity-70" name="sparkle" size="0.6875rem" />
  }

  if (target.kind === 'url') {
    return <ToolIcon className="opacity-70" name="globe" size="0.6875rem" />
  }

  return <FileTypeIcon className="opacity-70" path={target.path || target.url} size="0.6875rem" />
}

const previewPaneId = (tabId: string) => `${PREVIEW_TILE_PREFIX}:${tabId}`

/** The pane a NEW preview tile should stack into: another preview tile already
 *  in the tree, else — when several tabs arrive in one pass (a reload, or a
 *  session switch bringing its tabs back) — the FIRST of them, which itself
 *  gets no anchor and opens the zone. One shared head keeps the anchors
 *  acyclic; adoption places a pane after an anchor arriving with it (store.ts).
 *  `undefined` means this is the first preview — it opens its own zone. */
function existingPreviewAnchor(tabId: string): string | undefined {
  const own = previewPaneId(tabId)
  const inTree = treePanesWithPrefix(`${PREVIEW_TILE_PREFIX}:`).find(id => id !== own)

  if (inTree) {
    return inTree
  }

  const head = $dockedVisiblePreviewTabs.get()[0]

  return head && head.id !== tabId ? previewPaneId(head.id) : undefined
}

/** Keep pane contributions mirroring `$previewTabs`, keep the store's selection
 *  and the tree's active pane agreeing, and front a tile when its tab is
 *  selected. Call once from the root. */
export function watchPreviewTiles(): void {
  watchPreviewTileMirror()

  window.hermesDesktop?.onBrowserPopoutClosed?.(tabId => {
    adoptPersistedBrowserTab(tabId)
    markBrowserTabPopped(tabId, false)
  })

  // The reveal analog of session tiles (session-states calls revealTreePane on
  // open): `openPreview` selects the tab, and the TREE must front its pane —
  // un-minimize, un-hide, activate in its zone. Both stores, because re-opening
  // the already-active tab changes only `$previewTabs` (fresh tab object), while
  // switching tabs changes only the active id.
  const reveal = () => {
    const tabId = $rightRailActiveTabId.get()

    if (tabId && targetFor(tabId)) {
      revealTreePane(`${PREVIEW_TILE_PREFIX}:${tabId}`)
    }
  }

  $rightRailActiveTabId.listen(reveal)
  // One listener for the visible list: re-home the selection FIRST (a session
  // switch or an unpin can leave the active tab outside the visible set, and
  // a stale reveal of a just-hidden tab is a no-op — its pane left the tree),
  // THEN reveal the tab the re-home left active. Registered after the mirror,
  // so the pane set is already synced when this runs.
  $visiblePreviewTabs.listen(() => {
    rehome()
    reveal()
  })

  // A session switch (or an unpin) can leave the active tab outside the
  // visible set — re-home the selection onto the tab that session last had in
  // front (else its first) so the strip and the pane never point at a hidden
  // preview.
  const rehome = () => {
    const preferred = preferredVisibleTabId()

    if (preferred !== $rightRailActiveTabId.get()) {
      selectRightRailTab(preferred)
    }
  }

  // And the reverse: clicking a preview TAB activates its pane in the TREE
  // only, so the store's selection must follow or `$previewTarget` (⌘L quote
  // labels, the titlebar's has-preview state) keeps reporting the previous
  // tab. Same derivation `$focusedStoredSessionId` uses: the interacted zone's
  // active pane names the tab. Converges with `reveal` — re-selecting the id
  // the tree already fronts is a no-op in both directions.
  const follow = () => {
    const tree = $layoutTree.get()
    const groupId = $activeTreeGroup.get()

    // Do not copy this zone over an explicit open that lives in a different
    // group. A focus change after that open lifts the guard.
    if (
      explicitOpenBlocksZone(
        groupId,
        $previewTabs.get().map(tab => tab.id)
      )
    ) {
      return
    }

    const active = groupId && tree ? findGroup(tree, groupId)?.active : undefined

    if (!active?.startsWith(`${PREVIEW_TILE_PREFIX}:`)) {
      return
    }

    const tabId = active.slice(PREVIEW_TILE_PREFIX.length + 1) as RightRailTabId

    if (targetFor(tabId) && $rightRailActiveTabId.get() !== tabId) {
      selectRightRailTab(tabId)
    }
  }

  $layoutTree.listen(follow)
  $activeTreeGroup.listen(follow)
}

/** Hidden sessions' live pages kept mounted at once. WHY a cap: every kept
 *  Browser is a whole renderer process (plus its page's memory) that keeps
 *  running while its session is off screen. Eight covers flipping between
 *  the handful of chats a user is actually working in; past that the
 *  longest-hidden one is unmounted and reloads from its saved address when
 *  its session returns (the tab itself is never closed). */
const HIDDEN_LIVE_PREVIEW_LIMIT = 8

/** A page (live Browser guest or rendered HTML) whose body must survive
 *  being out of view; a text peek is cheap to re-render. */
function keepsLiveBody(tabId: string): boolean {
  const target = targetFor(tabId)

  return target?.kind === 'url' || target?.previewKind === 'html'
}

/** A tab whose session left the screen keeps its live body when it is a page
 *  still open in this profile's rail (`targetFor` reads it) and not popped
 *  out (the pop-out window owns that guest). */
const retainHiddenPreview = (tabId: string) => keepsLiveBody(tabId) && !$poppedBrowserTabIds.get().has(tabId)

const watchPreviewTileMirror = paneMirror<{ id: string }>({
  // Only the FOCUSED session's tabs (plus pins) are in the tree — switching
  // sessions swaps the drawer without closing any tab. A hidden session's
  // live page leaves the tree but stays mounted (hidden, inert) so returning
  // to that session shows the same guest, not a reload; a popped-out
  // Browser's pane leaves the docked tree for good.
  source: $dockedVisiblePreviewTabs,
  retain: retainHiddenPreview,
  retainLimit: HIDDEN_LIVE_PREVIEW_LIMIT,
  key: tab => tab.id,
  prefix: PREVIEW_TILE_PREFIX,
  // The FIRST preview still opens its own zone docked beside main (identical
  // to route tiles — NOT anchored to the file tree, so ⌘J can't take it
  // along). Every SUBSEQUENT preview stacks into that zone as a center tab:
  // without the anchor each opened file split a new zone off the right edge
  // (#93610), turning three file opens into three ever-narrower columns.
  dir: tab => (existingPreviewAnchor(tab.id) ? 'center' : 'right'),
  anchor: tab => existingPreviewAnchor(tab.id),
  minWidth: '22rem',
  title: previewTitle,
  tabLead: tabId => <PreviewTabLead tabId={tabId} />,
  tabTitle: tabId => (targetFor(tabId)?.kind === 'url' ? <BrowserTabLabel tabId={tabId} /> : undefined),
  // A Browser is a vessel, so there can be more of it — a file peek is one of
  // a kind and leaves the strip's "+" to whatever else the zone holds.
  newTab: tabId => (targetFor(tabId)?.kind === 'url' ? newBrowserTab : undefined),
  // Pin/unpin rides the zone tab menu for every preview tab: pinned tabs are
  // the explicit cross-session workspace, everything else belongs to the
  // session that opened it (#73890).
  tabMenuPrefix: previewTabMenuPrefix,
  lifecycleKeepAlive: keepsLiveBody,
  // The body's own Close (an error state's way out) is the tab's ✕, verbatim.
  render: tabId => <PreviewTilePane onClose={() => closeTabPane(previewPaneId(tabId))} tabId={tabId} />,
  close: tabId => {
    forgetBrowserPage(tabId)
    forgetPreviewConsole(tabId)
    closeRightRailTab(tabId)
  }
})