ui / src / components / agents-nav.tsxmain at d68d7e1
// The Agents app's sidebar body (the frame around it is AppShell): "New agent" and the mind's
// agents at the top (sub-agents are not listed), most recently active first. Each shows its name or
// the first thing a person said to it, with what it is doing beneath while it works or waits (else
// its path), an icon for what runs it and a dot for its status (lib/agent-summary.ts). Pinned agents sit in their own list above, pins kept
// per mind in this browser. It imports no router: a row is a link to `agentHref(path)`, and a
// plain left click on it is `onOpen`'s when the page has one.
//
// IN THE ICON RAIL (the sidebar collapsed) a row is its icon and its status dot alone, centred as
// the other rows' icons are, and its tooltip (`AgentTooltip`) says its title, its line and its
// path. The words hide by the sidebar's own `group-data-[collapsible=icon]` classes.
import { useCallback, useEffect, useMemo, useState, type ComponentProps } from "react";
import { PinIcon, SquarePenIcon } from "lucide-react";
import { cn } from "cn";
import { z } from "zod";
import { kindIcon, STATUS_DOT } from "./agent-look.ts";
import {
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
} from "#/components/ui/sidebar.tsx";
import {
agentDoing,
agentTitle,
orderAgents,
personAt,
topLevelAgents,
type AgentRow,
type AgentStatus,
} from "#/lib/agent-summary.ts";
import { ContextPathText } from "#/components/context-path-text.tsx";
import { plainLeftClick } from "#/lib/plain-left-click.ts";
/** Where an agent's chat is, and the home page for `undefined`. */
export type AgentHref = (path: string | undefined) => string;
/** Opens an agent's chat, or the home page for `undefined`, without leaving the page. */
export type OpenAgent = (path: string | undefined) => void;
/** A link to an agent's chat or the home page. A plain left click is `onOpen`'s; any other click
* is the browser's (a new tab). */
export function AgentLink({
path,
agentHref,
onOpen,
...props
}: Omit<ComponentProps<"a">, "href" | "onClick"> & {
path: string | undefined;
agentHref: AgentHref;
onOpen: OpenAgent | undefined;
}) {
return (
<a
{...props}
href={agentHref(path)}
onClick={(event) => {
if (!onOpen || !plainLeftClick(event)) return;
event.preventDefault();
onOpen(path);
}}
/>
);
}
export function AgentsNav({
mind,
agents,
agent,
person,
installed = true,
agentHref,
onOpen,
}: {
/** the mind's address: pins are kept under it */
mind: string;
/** whether the mind has `itx.agents`: without it there is nothing to create an agent with */
installed?: boolean;
agents: AgentRow[];
/** the open agent; none on the home page */
agent: string | undefined;
/** The folder of the person signed in (lib/agent-summary.ts `personOf`): their agents come
* first, and another person's agent says whose it is. */
person?: string;
agentHref: AgentHref;
/** A page with a client router navigates here; without it a row is a plain link. */
onOpen?: OpenAgent;
}) {
const { pinned, togglePinned } = usePinnedAgents(mind);
const ordered = useMemo(
() => orderAgents(topLevelAgents(agents), pinned, person),
[agents, pinned, person],
);
const renderRow = (row: AgentRow) => (
<AgentsNavRow
key={row.path}
row={row}
other={personAt(row.path) === person ? undefined : personAt(row.path)}
active={row.path === agent}
pinned={pinned.has(row.path)}
onTogglePinned={() => togglePinned(row.path)}
agentHref={agentHref}
onOpen={onOpen}
/>
);
return (
<>
{installed ? (
<SidebarGroup>
<SidebarGroupContent>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton
isActive={!agent}
tooltip="New agent"
render={<AgentLink path={undefined} agentHref={agentHref} onOpen={onOpen} />}
>
<SquarePenIcon />
<span>New agent</span>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
) : null}
{ordered.pinned.length > 0 ? (
<SidebarGroup>
<SidebarGroupLabel>Pinned</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>{ordered.pinned.map(renderRow)}</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
) : null}
<SidebarGroup>
<SidebarGroupLabel>Agents</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{agents.length === 0 ? (
<p className="font-readme px-2 py-1 text-xs text-muted-foreground group-data-[collapsible=icon]:hidden">
No agents yet.
</p>
) : null}
{ordered.recent.map(renderRow)}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</>
);
}
function AgentsNavRow({
row,
other,
active,
pinned,
onTogglePinned,
agentHref,
onOpen,
}: {
row: AgentRow;
/** The person whose agent this is, when it is another person's. */
other: string | undefined;
active: boolean;
pinned: boolean;
onTogglePinned: () => void;
agentHref: AgentHref;
onOpen: OpenAgent | undefined;
}) {
const KindIcon = kindIcon(row.kind);
const doing = agentDoing(row);
return (
<SidebarMenuItem data-agent-path={row.path}>
<SidebarMenuButton
size="lg"
// a phone gives what the agent is doing two lines, so the row grows; in the icon rail the
// large row is the same square as the other rows, its icon centred
className="max-md:h-auto max-md:min-h-12 group-data-[collapsible=icon]:p-2!"
isActive={active}
tooltip={{
children: <AgentTooltip title={agentTitle(row)} line={doing} path={row.path} />,
}}
// the link's name, also in the rail, where the row's words are not drawn
render={
<AgentLink
aria-label={agentTitle(row)}
path={row.path}
agentHref={agentHref}
onOpen={onOpen}
/>
}
>
<span className="relative">
<KindIcon data-agent-kind={row.kind || undefined} />
{row.status ? <StatusDot status={row.status} /> : null}
</span>
{/* an agent with no title is its path, once, on line one */}
<span className="flex min-w-0 flex-col group-data-[collapsible=icon]:hidden">
<span className="flex min-w-0 items-baseline gap-1 text-sm">
{other ? (
<span className="shrink-0 text-muted-foreground" data-agent-person>
{other}
</span>
) : null}
{row.title ? (
<span className="truncate">{agentTitle(row)}</span>
) : (
<ContextPathText path={row.path} truncate className="text-xs" />
)}
</span>
{doing ? (
<span
className="line-clamp-2 text-2xs text-muted-foreground md:line-clamp-1"
data-agent-doing
>
{doing}
</span>
) : row.title ? (
<ContextPathText path={row.path} truncate className="text-2xs text-muted-foreground" />
) : null}
</span>
</SidebarMenuButton>
<button
type="button"
data-sidebar="menu-action"
aria-label={pinned ? "Unpin agent" : "Pin agent"}
aria-pressed={pinned}
onClick={onTogglePinned}
className={cn(
"absolute top-1/2 right-1 flex size-6 -translate-y-1/2 items-center justify-center rounded-md text-muted-foreground outline-hidden ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 group-data-[collapsible=icon]:hidden [&>svg]:size-3.5",
// touch has no hover: the action shows there; a pointer reveals it on the row
!pinned &&
"md:opacity-0 md:group-hover/menu-item:opacity-100 md:focus-visible:opacity-100",
)}
>
<PinIcon className={cn(pinned && "fill-current")} />
</button>
</SidebarMenuItem>
);
}
/** An agent row's tooltip, which shows in the icon rail: its title, its one line, and its path. */
export function AgentTooltip({
title,
line,
path,
}: {
title: string;
line?: string | null;
path: string;
}) {
return (
<span className="flex max-w-72 min-w-0 flex-col gap-0.5" data-agent-tooltip>
<span className="line-clamp-2">{title}</span>
{line ? <span className="truncate opacity-70">{line}</span> : null}
<ContextPathText path={path} truncate className="opacity-70" />
</span>
);
}
/** What the agent is doing, as a dot on the corner of its icon. */
export function StatusDot({ status }: { status: AgentStatus }) {
return (
<span
data-agent-status={status}
title={status}
className={cn(
"absolute -right-0.5 -bottom-0.5 size-1.5 rounded-full ring-1 ring-sidebar",
STATUS_DOT[status],
)}
/>
);
}
// ── pins ──
// The agents this person pinned, per mind, in this browser's localStorage: a viewer's
// convenience, never the mind's data. Read after mount (a server render has no storage); storage
// that throws (a private window, blocked site data) leaves pins in memory only.
const Pins = z.array(z.string());
const NONE: ReadonlySet<string> = new Set();
function storageKey(mind: string) {
return `iterate-agents:pinned:${mind}`;
}
function readPins(mind: string): Set<string> {
try {
const parsed = Pins.safeParse(JSON.parse(localStorage.getItem(storageKey(mind)) ?? "[]"));
return new Set(parsed.success ? parsed.data : []);
} catch {
return new Set();
}
}
function usePinnedAgents(mind: string): {
pinned: ReadonlySet<string>;
togglePinned: (path: string) => void;
} {
const [held, setHeld] = useState<{ mind: string; pinned: ReadonlySet<string> }>();
useEffect(() => setHeld({ mind, pinned: readPins(mind) }), [mind]);
const togglePinned = useCallback(
(path: string) => {
const next = new Set(held?.mind === mind ? held.pinned : readPins(mind));
if (!next.delete(path)) next.add(path);
try {
localStorage.setItem(storageKey(mind), JSON.stringify([...next]));
} catch {
// storage refused: the pin holds for this page's life
}
setHeld({ mind, pinned: next });
},
[held, mind],
);
return {
pinned: held?.mind === mind ? held.pinned : NONE,
togglePinned,
};
}