Skip to content

Commit de94914

Browse files
authored
web: scale the automation canvas as one diagram (#1821)
1 parent fdfc6ee commit de94914

5 files changed

Lines changed: 457 additions & 278 deletions

File tree

‎web/src/pages/automations/build/BuildTab.tsx‎

Lines changed: 128 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
DropdownMenuTrigger,
2424
} from "@/components/ui/dropdown-menu";
2525
import { Input } from "@/components/ui/input";
26+
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
2627
import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
2728
import { useEventCatalog } from "@/hooks/useAutomationEditor";
2829
import { useProfiles } from "@/hooks/useProfiles";
@@ -75,7 +76,8 @@ import { StepDetails } from "../runs/StepDetails";
7576
import { BlockInspector } from "./BlockInspector";
7677
import { AddAgentNode, AgentBus, AgentNode, AgentStub } from "./canvas/AgentNode";
7778
import { Canvas, TRIGGER_ROW_ID } from "./canvas/Canvas";
78-
import { MAX_SCALE, MIN_SCALE } from "./canvas/layout";
79+
import { layoutCanvas } from "./canvas/layout";
80+
import { ZoomFrame } from "./canvas/ZoomFrame";
7981
import { AgentInspector } from "./inspectors/AgentInspector";
8082
import { TestPanel } from "./test/TestPanel";
8183
import { TriggerInspector } from "./TriggerInspector";
@@ -254,9 +256,18 @@ export function BuildTab({
254256
}: BuildTabProps) {
255257
const ways = entrypointIds(definition);
256258
const active = projectEntrypoint(definition, entrypointId);
259+
// One column per way in, as wide as its canvas draws; the agent bus joins
260+
// the columns' centres.
261+
const columns = useMemo(
262+
() =>
263+
entrypointIds(definition).map((way) => {
264+
const projected = projectEntrypoint(definition, way);
265+
return { way, projected, width: layoutCanvas(projected.blocks).width };
266+
}),
267+
[definition],
268+
);
257269
const firstId = active.blocks[0]?.id ?? TRIGGER_ROW_ID;
258270
const [selectedId, setSelectedId] = useState<string>(firstId);
259-
const [zoom, setZoom] = useState<number | "fit">("fit");
260271
const [adding, setAdding] = useState(false);
261272
const [draftWay, setDraftWay] = useState("");
262273
// Read once: `defaultSize` is only consulted on mount, and every later width
@@ -531,162 +542,127 @@ export function BuildTab({
531542
backgroundSize: "20px 20px",
532543
}}
533544
>
534-
<div className="absolute top-3 right-3 z-30 flex items-center gap-1" aria-label="Zoom">
535-
<Button
536-
variant="outline"
537-
size="sm"
538-
className="h-7 w-7 px-0"
539-
aria-label="Zoom out"
540-
onClick={() => setZoom((z) => Math.max(MIN_SCALE, (z === "fit" ? 1 : z) - 0.1))}
541-
>
542-
−
543-
</Button>
544-
<Button
545-
variant="outline"
546-
size="sm"
547-
className="h-7 w-7 px-0"
548-
aria-label="Zoom in"
549-
onClick={() => setZoom((z) => Math.min(MAX_SCALE, (z === "fit" ? 1 : z) + 0.1))}
550-
>
551-
+
552-
</Button>
553-
<Button variant="outline" size="sm" className="h-7" onClick={() => setZoom("fit")}>
554-
Fit
555-
</Button>
556-
</div>
557-
<div className="flex min-h-0 flex-1 gap-6 overflow-auto p-6 pt-12">
558-
{/* Fit: every column shrinks and its canvas scales down, so the
559-
whole row is in view. An explicit zoom keeps each column at
560-
its canvas width and the row scrolls, so a later column is
561-
never painted under the "add" control beside the row.
562-
The row is as tall as its tallest column and the agent sits
563-
right under it: the diagram scrolls as a whole, so a stem
564-
never spans an empty viewport. */}
565-
<div
566-
className={cn(
567-
"flex flex-1 flex-col self-start",
568-
zoom === "fit" ? "min-w-0" : "min-w-max",
569-
)}
570-
>
571-
<div className="flex gap-6">
572-
{ways.map((way) => {
573-
const projected =
574-
way === entrypointId ? active : projectEntrypoint(definition, way);
575-
const carried = wayCarried(way, projected.blocks);
576-
return (
577-
<div
578-
key={way}
579-
className={cn(
580-
"flex min-h-0 flex-1 flex-col",
581-
zoom === "fit" ? "min-w-0" : "min-w-[240px]",
582-
)}
545+
<ZoomFrame
546+
toolbar={
547+
!builtin &&
548+
!replay && (
549+
<Popover open={adding} onOpenChange={setAdding}>
550+
<PopoverTrigger asChild>
551+
<Button
552+
type="button"
553+
variant="outline"
554+
size="sm"
555+
className="h-7 bg-card"
556+
data-testid="add-way-in"
557+
>
558+
<Plus aria-hidden />
559+
Add another way in
560+
</Button>
561+
</PopoverTrigger>
562+
<PopoverContent align="start" className="w-[264px] p-3">
563+
<form
564+
className="flex flex-col gap-2"
565+
onSubmit={(e) => {
566+
e.preventDefault();
567+
confirmAddWay();
568+
}}
583569
>
584-
<Canvas
585-
trigger={projected.trigger}
586-
triggerSummary={triggerSummaryFor(way)}
587-
entrypointId={
588-
ways.length > 1 || way !== MAIN_ENTRYPOINT_ID ? way : undefined
589-
}
590-
blocks={projected.blocks}
591-
selectedId={way === entrypointId ? effectiveId : null}
592-
onSelect={(id) => select(way, id)}
593-
erroredIds={way === entrypointId ? erroredIds : new Set()}
594-
locked={builtin}
595-
onInsert={(at, index, kind) => onInsert(way, at, index, kind)}
596-
onMove={(at, from, to) => onMove(way, at, from, to)}
597-
onRemove={(id) => onRemove(way, id)}
598-
zoom={zoom}
599-
summaryFor={summaryFor}
600-
withAgent={agent !== undefined}
601-
{...(replay ? { replay: { map: replay.map, onPath: carried } } : {})}
570+
<Input
571+
value={draftWay}
572+
onChange={(e) => setDraftWay(e.target.value)}
573+
placeholder="review_feedback"
574+
aria-label="Name for the new way in"
575+
className="font-mono text-xs"
576+
autoFocus
602577
/>
603-
{agent && (
604-
<AgentStub
605-
role={roleOf(definition, way)}
606-
message={messageOf(definition, way)}
607-
dimmed={replay ? !carried : false}
608-
onSelect={() => select(way, TRIGGER_ROW_ID)}
609-
/>
578+
{draftWayError && (
579+
<p className="text-xs text-muted-foreground">{draftWayError}</p>
610580
)}
611-
</div>
612-
);
613-
})}
614-
</div>
615-
{agent ? (
616-
<AgentBus columns={ways.length}>
617-
<AgentNode
618-
summary={agentSummary}
619-
selected={agentSelected}
620-
errored={agentErrors.length > 0}
621-
{...(replayAgent ? { status: replayAgent.status } : {})}
622-
dimmed={replay ? !replayAgent : false}
623-
onSelect={() => setSelectedId(AGENT_NODE_ID)}
624-
/>
625-
</AgentBus>
626-
) : (
627-
!builtin &&
628-
!replay && (
629-
<div className="flex shrink-0 justify-center pt-4">
630-
<AddAgentNode onAdd={addAgent} />
631-
</div>
632-
)
633-
)}
634-
</div>
635-
{!builtin && !replay && (
636-
<div className={cn("flex shrink-0 flex-col", adding ? "w-[264px]" : "w-auto")}>
637-
{adding ? (
638-
<form
639-
className="flex flex-col gap-2 rounded-lg border border-dashed bg-card/60 p-3"
640-
onSubmit={(e) => {
641-
e.preventDefault();
642-
confirmAddWay();
643-
}}
644-
>
645-
<Input
646-
value={draftWay}
647-
onChange={(e) => setDraftWay(e.target.value)}
648-
placeholder="review_feedback"
649-
aria-label="Name for the new way in"
650-
className="font-mono text-xs"
651-
autoFocus
581+
<div className="flex gap-2">
582+
<Button
583+
type="submit"
584+
size="sm"
585+
disabled={draftWay === "" || !!draftWayError}
586+
>
587+
Add
588+
</Button>
589+
<Button
590+
type="button"
591+
size="sm"
592+
variant="ghost"
593+
onClick={() => setAdding(false)}
594+
>
595+
Cancel
596+
</Button>
597+
</div>
598+
</form>
599+
</PopoverContent>
600+
</Popover>
601+
)
602+
}
603+
>
604+
{/* Each column is as wide as its canvas. The row is as tall as
605+
its tallest column and the agent sits right under it, so a
606+
stem never spans an empty viewport. */}
607+
<div className="flex gap-6">
608+
{columns.map(({ way, projected, width }) => {
609+
const carried = wayCarried(way, projected.blocks);
610+
return (
611+
<div key={way} className="flex flex-col" style={{ width }}>
612+
<Canvas
613+
trigger={projected.trigger}
614+
triggerSummary={triggerSummaryFor(way)}
615+
entrypointId={
616+
ways.length > 1 || way !== MAIN_ENTRYPOINT_ID ? way : undefined
617+
}
618+
blocks={projected.blocks}
619+
selectedId={way === entrypointId ? effectiveId : null}
620+
onSelect={(id) => select(way, id)}
621+
erroredIds={way === entrypointId ? erroredIds : new Set()}
622+
locked={builtin}
623+
onInsert={(at, index, kind) => onInsert(way, at, index, kind)}
624+
onMove={(at, from, to) => onMove(way, at, from, to)}
625+
onRemove={(id) => onRemove(way, id)}
626+
summaryFor={summaryFor}
627+
withAgent={agent !== undefined}
628+
{...(replay ? { replay: { map: replay.map, onPath: carried } } : {})}
652629
/>
653-
{draftWayError && (
654-
<p className="text-xs text-muted-foreground">{draftWayError}</p>
630+
{agent && (
631+
<AgentStub
632+
role={roleOf(definition, way)}
633+
message={messageOf(definition, way)}
634+
dimmed={replay ? !carried : false}
635+
onSelect={() => select(way, TRIGGER_ROW_ID)}
636+
/>
655637
)}
656-
<div className="flex gap-2">
657-
<Button
658-
type="submit"
659-
size="sm"
660-
disabled={draftWay === "" || !!draftWayError}
661-
>
662-
Add
663-
</Button>
664-
<Button
665-
type="button"
666-
size="sm"
667-
variant="ghost"
668-
onClick={() => setAdding(false)}
669-
>
670-
Cancel
671-
</Button>
672-
</div>
673-
</form>
674-
) : (
675-
<Button
676-
type="button"
677-
variant="ghost"
678-
size="sm"
679-
className="h-9 justify-start border border-dashed text-muted-foreground"
680-
onClick={() => setAdding(true)}
681-
data-testid="add-way-in"
682-
>
683-
<Plus aria-hidden />
684-
Add another way in
685-
</Button>
686-
)}
687-
</div>
638+
</div>
639+
);
640+
})}
641+
</div>
642+
{agent ? (
643+
<AgentBus
644+
left={columns[0]!.width / 2}
645+
right={columns.at(-1)!.width / 2}
646+
joins={columns.length > 1}
647+
>
648+
<AgentNode
649+
summary={agentSummary}
650+
selected={agentSelected}
651+
errored={agentErrors.length > 0}
652+
{...(replayAgent ? { status: replayAgent.status } : {})}
653+
dimmed={replay ? !replayAgent : false}
654+
onSelect={() => setSelectedId(AGENT_NODE_ID)}
655+
/>
656+
</AgentBus>
657+
) : (
658+
!builtin &&
659+
!replay && (
660+
<div className="flex shrink-0 justify-center pt-4">
661+
<AddAgentNode onAdd={addAgent} />
662+
</div>
663+
)
688664
)}
689-
</div>
665+
</ZoomFrame>
690666
</div>
691667
{!replay && (
692668
<TestDrawer

‎web/src/pages/automations/build/canvas/AgentNode.tsx‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,27 @@ export function AddAgentNode({ onAdd }: { onAdd: () => void }) {
168168
}
169169

170170
/** The bus: a line across the way-in columns' centres, a drop to the node.
171-
* `columns` is how many ways in sit above (each the same width), so the
172-
* line runs from the first column's centre to the last's. */
173-
export function AgentBus({ columns, children }: { columns: number; children: React.ReactNode }) {
174-
const inset = `calc(100% / ${columns * 2})`;
171+
* `left` and `right` are the first and last columns' half widths, so the
172+
* line runs from the first column's centre to the last's. `joins` is false
173+
* for a single column, which drops straight to the node. */
174+
export function AgentBus({
175+
left,
176+
right,
177+
joins,
178+
children,
179+
}: {
180+
left: number;
181+
right: number;
182+
joins: boolean;
183+
children: React.ReactNode;
184+
}) {
175185
return (
176186
<div className="flex shrink-0 flex-col items-center" data-testid="agent-bus">
177187
<div className="relative h-px w-full">
178-
{columns > 1 && (
188+
{joins && (
179189
<div
180190
className="absolute inset-y-0 border-t border-foreground/45"
181-
style={{ left: inset, right: inset }}
191+
style={{ left, right }}
182192
/>
183193
)}
184194
</div>

0 commit comments

Comments
 (0)