The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { WorkLogBlock } from "./WorkLog"; |
| 2 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 3 | import { useAtomValue } from "@effect/atom-react"; |
| 4 | import { environmentThreadDetails } from "../../state/threads"; |
| 5 | import { |
| 6 | subagentGroupSummary, |
| 7 | summarizeSubagentStatuses, |
| 8 | } from "@t3tools/client-runtime/state/subagent-display"; |
| 9 | import { useGT, useMessages } from "gt-react"; |
| 10 | import { memo, use, useState } from "react"; |
| 11 | import { ChevronDownIcon } from "lucide-react"; |
| 12 | import { cn } from "~/lib/utils"; |
| 13 | import { Collapsible, CollapsibleTrigger, CollapsiblePanel } from "../ui/collapsible"; |
| 14 | import { EnvironmentSubagentAvatar, SubagentElapsed, V2LifecycleRow } from "./V2LifecycleRow"; |
| 15 | import { LinkRequestCard } from "./LinkRequestCard"; |
| 16 | import { SecretRequestCard } from "./SecretRequestCard"; |
| 17 | import { TimelineRowCtx, type TimelineRow } from "./timelineRowContext"; |
| 18 | import { subagentGroupTiming } from "./TimelineV2EventRow.logic"; |
| 19 | |
| 20 | export function V2EventTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "event" }> }) { |
| 21 | const ctx = use(TimelineRowCtx); |
| 22 | const { item, visibility } = row.projectedItem; |
| 23 | if (item.type === "subagent" && (row.subagents?.length ?? 1) > 1) { |
| 24 | return <V2SubagentGroup key={row.id} row={row} />; |
| 25 | } |
| 26 | if (item.type === "secret_request") { |
| 27 | return ( |
| 28 | <SecretRequestCard |
| 29 | environmentId={ctx.activeThreadEnvironmentId} |
| 30 | item={item} |
| 31 | visibility={visibility} |
| 32 | /> |
| 33 | ); |
| 34 | } |
| 35 | if (item.type === "link_request") { |
| 36 | return ( |
| 37 | <LinkRequestCard |
| 38 | environmentId={ctx.activeThreadEnvironmentId} |
| 39 | item={item} |
| 40 | visibility={visibility} |
| 41 | /> |
| 42 | ); |
| 43 | } |
| 44 | return ( |
| 45 | <V2LifecycleRow |
| 46 | environmentId={ctx.activeThreadEnvironmentId} |
| 47 | item={item} |
| 48 | resourceSummary={row.resourceSummary} |
| 49 | runs={ctx.runs} |
| 50 | onOpenThread={ctx.onOpenThread} |
| 51 | /> |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | const V2SubagentGroup = memo(function V2SubagentGroup({ |
| 56 | row, |
| 57 | }: { |
| 58 | row: Extract<TimelineRow, { kind: "event" }>; |
| 59 | }) { |
| 60 | const ctx = use(TimelineRowCtx); |
| 61 | const gt = useGT(); |
| 62 | const m = useMessages(); |
| 63 | const groupId = `subagent-group:${row.id}`; |
| 64 | const [expanded, setExpanded] = useState(() => |
| 65 | ctx.workGroupViewState.expandedEntries.has(groupId), |
| 66 | ); |
| 67 | const members = (row.subagents ?? [row.projectedItem]).flatMap(({ item }) => |
| 68 | item.type === "subagent" ? [item] : [], |
| 69 | ); |
| 70 | const liveAgents = useAtomValue( |
| 71 | environmentThreadDetails.threadAtom( |
| 72 | scopeThreadRef(ctx.activeThreadEnvironmentId, row.projectedItem.item.threadId), |
| 73 | ), |
| 74 | (thread) => thread?.projection.subagents, |
| 75 | ); |
| 76 | const agents = members.map((item) => { |
| 77 | const live = liveAgents?.find((agent) => agent.id === item.subagentId); |
| 78 | return { |
| 79 | item, |
| 80 | status: live?.status ?? item.status, |
| 81 | startedAt: live?.startedAt ?? item.startedAt, |
| 82 | completedAt: live?.completedAt ?? item.completedAt, |
| 83 | }; |
| 84 | }); |
| 85 | const summary = subagentGroupSummary(agents, m); |
| 86 | const label = gt("{count, plural, one {{count} subagent} other {{count} subagents}}", { |
| 87 | count: members.length, |
| 88 | }); |
| 89 | const statusSummary = summarizeSubagentStatuses( |
| 90 | agents.map(({ status }) => status), |
| 91 | m, |
| 92 | ); |
| 93 | const toggleExpanded = (open: boolean) => { |
| 94 | ctx.onToggleWorkEntry(row.id, expanded); |
| 95 | if (open) ctx.workGroupViewState.expandedEntries.add(groupId); |
| 96 | else ctx.workGroupViewState.expandedEntries.delete(groupId); |
| 97 | setExpanded(open); |
| 98 | }; |
| 99 | return ( |
| 100 | <WorkLogBlock continues={row.continuesWorkLog}> |
| 101 | <Collapsible open={expanded} onOpenChange={toggleExpanded} data-subagent-group> |
| 102 | {/* pr-3.25 matches the panel border, padding, and row inset so the times line up. */} |
| 103 | <CollapsibleTrigger |
| 104 | aria-label={label} |
| 105 | aria-description={statusSummary} |
| 106 | className={cn( |
| 107 | "flex w-full min-w-0 items-center gap-2.5 py-2 pr-3.25 text-left transition-opacity hover:opacity-100", |
| 108 | expanded || summary.active |
| 109 | ? "text-foreground opacity-100" |
| 110 | : "text-muted-foreground opacity-55", |
| 111 | )} |
| 112 | > |
| 113 | <span className="flex shrink-0 items-center -space-x-1.5" aria-hidden> |
| 114 | {agents.slice(0, 3).map(({ item, status }) => ( |
| 115 | <EnvironmentSubagentAvatar |
| 116 | key={item.id} |
| 117 | environmentId={ctx.activeThreadEnvironmentId} |
| 118 | providerInstanceId={item.providerInstanceId} |
| 119 | driver={item.driver} |
| 120 | remote={item.remoteChild} |
| 121 | status={agents.length === 1 ? status : undefined} |
| 122 | /> |
| 123 | ))} |
| 124 | {agents.length > 3 ? ( |
| 125 | <span className="relative inline-flex size-6 items-center justify-center rounded-full bg-muted text-3xs font-medium text-muted-foreground ring-2 ring-background"> |
| 126 | +{agents.length - 3} |
| 127 | </span> |
| 128 | ) : null} |
| 129 | </span> |
| 130 | <span className="min-w-0 flex-1"> |
| 131 | <span className="block text-xs font-semibold">{label}</span> |
| 132 | <span |
| 133 | className={cn( |
| 134 | "block truncate text-3xs text-muted-foreground", |
| 135 | summary.active ? "text-info" : summary.failed && "text-destructive", |
| 136 | )} |
| 137 | > |
| 138 | {statusSummary} |
| 139 | </span> |
| 140 | </span> |
| 141 | <span className="shrink-0 text-xs text-muted-foreground tabular-nums"> |
| 142 | <SubagentElapsed agent={subagentGroupTiming(agents)} /> |
| 143 | </span> |
| 144 | <ChevronDownIcon |
| 145 | aria-hidden |
| 146 | className={cn( |
| 147 | "size-3.5 shrink-0 text-muted-foreground transition-transform", |
| 148 | expanded && "rotate-180", |
| 149 | )} |
| 150 | /> |
| 151 | </CollapsibleTrigger> |
| 152 | {/* Virtualized rows must settle before disclosure scroll anchoring resumes. */} |
| 153 | <CollapsiblePanel animate={false}> |
| 154 | {expanded ? ( |
| 155 | <div className="mt-1 mb-1 rounded-lg border border-border/60 bg-card/30 p-1"> |
| 156 | {members.map((item) => ( |
| 157 | <V2LifecycleRow |
| 158 | environmentId={ctx.activeThreadEnvironmentId} |
| 159 | key={item.id} |
| 160 | item={item} |
| 161 | runs={ctx.runs} |
| 162 | onOpenThread={ctx.onOpenThread} |
| 163 | /> |
| 164 | ))} |
| 165 | </div> |
| 166 | ) : null} |
| 167 | </CollapsiblePanel> |
| 168 | </Collapsible> |
| 169 | </WorkLogBlock> |
| 170 | ); |
| 171 | }); |