swarm ui: select several agents on the agents page and broadcast to them
Select mode picks agents from the list: the select button in the filter card, a press on an agent's icon, or a long press on a card starts it. In it a card toggles a tick instead of opening its details, and the card's own controls stop answering. A bar at the foot of the list has select all shown, clear, broadcast and done; Escape leaves the mode. Broadcast opens a dialog with the picked agents and a message box, sending through the existing broadcast endpoint; the composer's send and result parts are now shared with the swarm page's broadcast card. A full send shows a toast and ends select mode; a partial one keeps the result with a retry for the agents that failed.
This commit is contained in:
parent
7bdaea7954
commit
56ed2e07da
4 changed files with 793 additions and 88 deletions
|
|
@ -62,3 +62,68 @@
|
|||
outline: 2px solid var(--purple);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
/* The icon is where a press starts select mode, so it shows the pointer; the tick
|
||||
sits over its corner while picking. */
|
||||
.ui-agent-card-icon {
|
||||
position: relative;
|
||||
flex: none;
|
||||
display: block;
|
||||
cursor: pointer;
|
||||
}
|
||||
.ui-agent-card-row[role="checkbox"] {
|
||||
cursor: pointer;
|
||||
}
|
||||
.ui-agent-card-row[role="checkbox"] .ui-agent-card-icon {
|
||||
cursor: inherit;
|
||||
}
|
||||
.ui-agent-card-tick {
|
||||
position: absolute;
|
||||
top: 0.3em;
|
||||
right: 0.3em;
|
||||
width: 1.4em;
|
||||
height: 1.4em;
|
||||
box-sizing: border-box;
|
||||
border: 2px solid var(--muted);
|
||||
border-radius: 50%;
|
||||
background: color-mix(in srgb, var(--bg) 70%, transparent);
|
||||
transition:
|
||||
background-color 0.15s ease,
|
||||
border-color 0.15s ease,
|
||||
transform 0.15s ease;
|
||||
}
|
||||
.ui-agent-card-tick-on {
|
||||
border-color: var(--purple);
|
||||
background: var(--purple);
|
||||
transform: scale(1.1);
|
||||
}
|
||||
/* The check mark inside, drawn with two borders, in the card's own background. */
|
||||
.ui-agent-card-tick-on::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
left: 0.38em;
|
||||
top: 0.14em;
|
||||
width: 0.28em;
|
||||
height: 0.58em;
|
||||
border: solid var(--bg);
|
||||
border-width: 0 2px 2px 0;
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
/* Not a control while picking: the card takes the click. */
|
||||
.ui-agent-card-row[role="checkbox"] [inert] {
|
||||
opacity: 0.55;
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.ui-agent-card-tick {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
:root[data-motion="reduce"] .ui-agent-card-tick {
|
||||
transition: none;
|
||||
}
|
||||
/* A press and hold starts select mode, so it must not also select the text. */
|
||||
.ui-agent-card-row {
|
||||
-webkit-user-select: none;
|
||||
user-select: none;
|
||||
-webkit-touch-callout: none;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,12 @@
|
|||
// which the page turns into `/agents/<name>`); the card of the open agent is
|
||||
// marked `selected`.
|
||||
//
|
||||
// In select mode (`select` is set) a choice toggles the card's tick instead, to
|
||||
// pick several agents for something to do to all of them. A tick on the icon
|
||||
// shows which are picked; the card's own controls (terminal, wanted menu, login)
|
||||
// stop answering so a stray tap can't start or stop an agent. Outside select
|
||||
// mode, a press on the icon, or a long press on the card, starts it (`onStartSelect`).
|
||||
//
|
||||
// The terminal badge beside the wanted menu opens the agent's terminal tab, the
|
||||
// most common action, without opening the card.
|
||||
//
|
||||
|
|
@ -17,6 +23,7 @@ import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
|||
import { Badge } from "@hive/shared/badge.js";
|
||||
import { TerminalIcon } from "@hive/shared/icons.js";
|
||||
import { Card } from "../../ui/card/Card.js";
|
||||
import { useLongPress } from "./useLongPress.js";
|
||||
import { useDynamicTabs } from "../../shell/useDynamicTabs.js";
|
||||
import { AgentIcon } from "../AgentIcon.js";
|
||||
import { ClaudeLoginDialog } from "../claude-login/ClaudeLoginDialog.js";
|
||||
|
|
@ -41,6 +48,8 @@ export function AgentCard({
|
|||
actions,
|
||||
selected = false,
|
||||
onOpen,
|
||||
select,
|
||||
onStartSelect,
|
||||
}: {
|
||||
row: AgentRow;
|
||||
actions: AgentActions;
|
||||
|
|
@ -48,29 +57,63 @@ export function AgentCard({
|
|||
selected?: boolean;
|
||||
/** Called when the card is chosen, to open the agent's details. */
|
||||
onOpen: () => void;
|
||||
/** Set in select mode: whether this agent is picked, and the way to flip it. */
|
||||
select?: { picked: boolean; onToggle: () => void };
|
||||
/** Called to enter select mode with this agent picked. */
|
||||
onStartSelect: () => void;
|
||||
}) {
|
||||
const [loggingIn, setLoggingIn] = useState(false);
|
||||
const error = actions.actionErrors.get(row.name);
|
||||
const { open: openTab } = useDynamicTabs();
|
||||
const term = termPaths(row.name);
|
||||
const selecting = select !== undefined;
|
||||
const choose = selecting ? select.onToggle : onOpen;
|
||||
const press = useLongPress(onStartSelect, !selecting);
|
||||
// The controls inside the card do nothing while picking.
|
||||
const inert = selecting ? { inert: true } : {};
|
||||
return (
|
||||
<Card selected={selected}>
|
||||
<Card selected={selecting ? select.picked : selected}>
|
||||
<AgentCardLayout
|
||||
rowProps={{
|
||||
role: "button",
|
||||
role: selecting ? "checkbox" : "button",
|
||||
tabIndex: 0,
|
||||
"data-agent": row.name,
|
||||
"aria-current": selected ? "true" : undefined,
|
||||
onClick: onOpen,
|
||||
"aria-checked": selecting ? select.picked : undefined,
|
||||
"aria-current": !selecting && selected ? "true" : undefined,
|
||||
onClick: () => {
|
||||
if (!press.shouldIgnoreClick()) choose();
|
||||
},
|
||||
onKeyDown: (e) => {
|
||||
if (e.target !== e.currentTarget) return;
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault();
|
||||
onOpen();
|
||||
choose();
|
||||
}
|
||||
},
|
||||
...press.handlers,
|
||||
}}
|
||||
icon={<AgentIcon name={row.name} />}
|
||||
icon={
|
||||
<span
|
||||
class="ui-agent-card-icon"
|
||||
title={selecting ? undefined : `select ${row.name}`}
|
||||
onClick={(e) => {
|
||||
if (selecting) return;
|
||||
e.stopPropagation();
|
||||
onStartSelect();
|
||||
}}
|
||||
>
|
||||
<AgentIcon name={row.name} />
|
||||
{selecting ? (
|
||||
<span
|
||||
class={
|
||||
"ui-agent-card-tick" +
|
||||
(select.picked ? " ui-agent-card-tick-on" : "")
|
||||
}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
) : null}
|
||||
</span>
|
||||
}
|
||||
name={<span class="ui-agent-card-name">{row.name}</span>}
|
||||
status={
|
||||
<>
|
||||
|
|
@ -116,6 +159,7 @@ export function AgentCard({
|
|||
<span
|
||||
class="ui-agent-card-login"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
{...inert}
|
||||
>
|
||||
<Badge
|
||||
tone="warning"
|
||||
|
|
@ -142,6 +186,7 @@ export function AgentCard({
|
|||
<span
|
||||
class="ui-agent-card-wanted"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
{...inert}
|
||||
>
|
||||
<Badge
|
||||
variant="quiet"
|
||||
|
|
|
|||
44
frontend/packages/swarm-ui/src/agent/roster/long-press.ts
Normal file
44
frontend/packages/swarm-ui/src/agent/roster/long-press.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// A long press: the pointer held down on one spot for `LONG_PRESS_MS`. Plain
|
||||
// functions so `npm test` runs them; `useLongPress` wires them to a component.
|
||||
|
||||
/** How long a press must last to count as a long one. */
|
||||
export const LONG_PRESS_MS = 500;
|
||||
|
||||
/** How far (px) the pointer may stray before the press is a drag or a scroll. */
|
||||
export const LONG_PRESS_SLOP = 8;
|
||||
|
||||
/** Whether the pointer has left the spot where the press began. */
|
||||
export function strayed(
|
||||
from: { x: number; y: number },
|
||||
to: { x: number; y: number },
|
||||
): boolean {
|
||||
return Math.hypot(to.x - from.x, to.y - from.y) > LONG_PRESS_SLOP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a long press has just started select mode and its closing click is
|
||||
* still to come. Every press starts by clearing it (`pressed`), so a long press
|
||||
* that ends with no click (the browser's own menu on a touch screen, a release
|
||||
* off the card) cannot swallow the next real tap.
|
||||
*/
|
||||
export function createLongPressFlag() {
|
||||
let fired = false;
|
||||
return {
|
||||
/** A new press begins. */
|
||||
pressed: () => {
|
||||
fired = false;
|
||||
},
|
||||
/** The press ran long enough. */
|
||||
fire: () => {
|
||||
fired = true;
|
||||
},
|
||||
/** The click after a press: true once if that press was a long one. */
|
||||
consumeClick: () => {
|
||||
const was = fired;
|
||||
fired = false;
|
||||
return was;
|
||||
},
|
||||
/** Whether the context menu that a long press opens should be blocked. */
|
||||
blocksContextMenu: () => fired,
|
||||
};
|
||||
}
|
||||
57
frontend/packages/swarm-ui/src/agent/roster/useLongPress.ts
Normal file
57
frontend/packages/swarm-ui/src/agent/roster/useLongPress.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// useLongPress — handlers to spread on an element so that holding a pointer
|
||||
// (finger or mouse) on it for `LONG_PRESS_MS` calls `onLongPress`. A press that
|
||||
// moves, lifts early or is cancelled (a scroll starting) does nothing. The click
|
||||
// that follows a long press is the same gesture ending, so `shouldIgnoreClick`
|
||||
// tells the click handler to skip it, once (`createLongPressFlag`).
|
||||
import { useRef } from "preact/hooks";
|
||||
import { LONG_PRESS_MS, createLongPressFlag, strayed } from "./long-press.js";
|
||||
|
||||
type PointerHandler = (e: PointerEvent) => void;
|
||||
|
||||
export function useLongPress(onLongPress: () => void, enabled = true) {
|
||||
const timer = useRef<number | undefined>(undefined);
|
||||
const origin = useRef({ x: 0, y: 0 });
|
||||
const flag = useRef(createLongPressFlag());
|
||||
|
||||
const cancel = () => {
|
||||
window.clearTimeout(timer.current);
|
||||
timer.current = undefined;
|
||||
};
|
||||
|
||||
// Always attached, even while `enabled` is off: every press has to clear the
|
||||
// flag, or a long press that began select mode would leave it set.
|
||||
const onPointerDown: PointerHandler = (e) => {
|
||||
flag.current.pressed();
|
||||
cancel();
|
||||
// A press on a control inside (the terminal button, a menu) is that
|
||||
// control's own.
|
||||
if (!enabled) return;
|
||||
if ((e.target as HTMLElement).closest("button, a, input") !== null) return;
|
||||
origin.current = { x: e.clientX, y: e.clientY };
|
||||
timer.current = window.setTimeout(() => {
|
||||
timer.current = undefined;
|
||||
flag.current.fire();
|
||||
onLongPress();
|
||||
}, LONG_PRESS_MS);
|
||||
};
|
||||
const onPointerMove: PointerHandler = (e) => {
|
||||
if (timer.current === undefined) return;
|
||||
if (strayed(origin.current, { x: e.clientX, y: e.clientY })) cancel();
|
||||
};
|
||||
|
||||
return {
|
||||
handlers: {
|
||||
onPointerDown,
|
||||
onPointerMove,
|
||||
onPointerUp: cancel,
|
||||
onPointerCancel: cancel,
|
||||
onPointerLeave: cancel,
|
||||
// A long press on a touch screen also opens the browser's own menu.
|
||||
onContextMenu: (e: Event) => {
|
||||
if (flag.current.blocksContextMenu()) e.preventDefault();
|
||||
},
|
||||
},
|
||||
/** True once after a long press, so its closing click is not a plain one. */
|
||||
shouldIgnoreClick: () => flag.current.consumeClick(),
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue