import { useEffect, useRef, useState, type ReactNode } from 'react' import { Check, Copy } from 'lucide-react' import { getEntityDisplayTitle } from '../lib/entityDisplay' import { Icon as UiIcon, IconButton, Text, Tooltip } from '../ui' import type { BadgeTone } from '../ui' import { MainHeader } from './MainHeader' import { InlineStatusBadge } from './InlineStatusBadge' import { EntityRuntimeBadges } from './EntityRuntimeBadges' import { ShareEntityDialog } from './ShareEntityDialog' import { useEntityPermission } from '../hooks/useEntityPermission' import { listViews, type ViewId } from '../lib/workspace/viewRegistry' import styles from './EntityHeader.module.css' import { useElectricAgents, type ElectricEntity, } from '../lib/ElectricAgentsProvider' const STATUS_TONE: Record = { active: `info`, running: `info`, idle: `success`, spawning: `warning`, paused: `warning`, stopping: `warning`, stopped: `neutral`, killed: `danger`, } type EntityHeaderProps = { entity: ElectricEntity /** ID of the currently-rendered view for this entity. */ currentViewId?: ViewId /** Switch the rendered view in-place (no layout change). */ onSetView?: (viewId: ViewId) => void /** * Optional slot for the tile menu (the `…` button at the right edge). * The workspace passes its `` here. Kept generic so the * header doesn't need to know about tiles / groups / splits. */ menu?: ReactNode /** Optional leading affordance before the title, e.g. a tile drag handle. */ leading?: ReactNode /** True for the leftmost top tile that must clear fixed titlebar controls. */ chromeInsetTarget?: boolean /** Optional banner of error messages displayed below the strip. */ errors?: Array } /** * Top of an entity tile. A flat strip with the session name + id on * the left and an actions cluster on the right (view-toggle icons + * caller-supplied menu), plus a thin error strip below when actions * surface errors. * * No border-bottom — the strip shares the chat background so the * header reads as part of the same surface as the conversation below. */ export function EntityHeader({ entity, currentViewId, onSetView, menu, leading, chromeInsetTarget = false, errors, }: EntityHeaderProps): React.ReactElement | null { return ( <> } chromeInsetTarget={chromeInsetTarget} actions={ } /> {errors && errors.length > 0 && (
{errors.map((msg, i) => ( {msg} ))}
)} ) } function EntityTitle({ entity, }: { entity: ElectricEntity }): React.ReactElement { const { setEntityTitle } = useElectricAgents() const { title } = getEntityDisplayTitle(entity) // The session id is the URL minus the leading slash (e.g. // `horton/gpt5-verify-1777802612`). The type is encoded in the path // so a separate type pill would be redundant. const sessionId = entity.url.replace(/^\//, ``) const decoded = decodeURIComponent(entity.url) const [copied, setCopied] = useState(false) const [editing, setEditing] = useState(false) const [draftTitle, setDraftTitle] = useState(title) const copiedTimerRef = useRef | null>(null) const inputRef = useRef(null) useEffect(() => { return () => { if (copiedTimerRef.current) clearTimeout(copiedTimerRef.current) } }, []) useEffect(() => { if (!editing) setDraftTitle(title) }, [editing, title]) useEffect(() => { if (editing) inputRef.current?.focus() }, [editing]) const copy = () => { void navigator.clipboard.writeText(sessionId) setCopied(true) if (copiedTimerRef.current) clearTimeout(copiedTimerRef.current) copiedTimerRef.current = setTimeout(() => setCopied(false), 1200) } const cancelEdit = () => { setDraftTitle(title) setEditing(false) } const saveEdit = () => { const nextTitle = draftTitle.trim() if (!nextTitle) { cancelEdit() return } setEditing(false) setDraftTitle(nextTitle) if (nextTitle === title || !setEntityTitle) return const tx = setEntityTitle({ entityUrl: entity.url, title: nextTitle }) void tx.isPersisted.promise.catch(() => { setDraftTitle(title) }) } return ( {editing ? ( setDraftTitle(event.currentTarget.value)} onBlur={saveEdit} onKeyDown={(event) => { if (event.key === `Enter`) { event.preventDefault() inputRef.current?.blur() } else if (event.key === `Escape`) { event.preventDefault() cancelEdit() } }} /> ) : ( )} ) } function EntityActions({ entity, currentViewId, onSetView, menu, }: Pick< EntityHeaderProps, `entity` | `currentViewId` | `onSetView` | `menu` >): React.ReactElement { // The view registry is the source of truth for which view buttons // appear. `defaultViewId` is the first registered view (`chat`) and // is treated as implicit when no current view is set. const availableViews = onSetView ? listViews(entity) : [] const defaultViewId = availableViews[0]?.id const activeViewId = currentViewId ?? defaultViewId const canManage = useEntityPermission(entity, `manage`) // Only show the inline view-switcher buttons when there's more than // one view available — otherwise the strip is just visual noise. const showViewStrip = onSetView && availableViews.length > 1 return ( {entity.status} {showViewStrip && availableViews.map((view) => { const Icon = view.icon const active = view.id === activeViewId return ( onSetView!(view.id)} aria-label={view.label} aria-pressed={active} className={active ? styles.activeBg : undefined} > ) })} {canManage && } {menu} ) }