Watch
0
0
Fork
You've already forked hyperhive
0

swarm ui: a popover stays on the page, and the ssh popover folds its explanations

A popover hung from its anchor and ran off the foot of the window when the anchor sat low. It now goes over the anchor when it fits neither way better below, and otherwise takes the roomier side with its height cut so it scrolls inside itself; the placement is a pure function with a test, and the box places itself again when its content changes size. The ssh popover showed three long paragraphs above its switch: the explainer and the host-key text now sit in a folded how it works section, and the warning and the switch stay visible. Its sections can no longer give the popover a sideways scroll.
This commit is contained in:
iris 2026-11-20 11:47:08 +01:00 • committed by mara
commit da187f8368
4 changed files with 136 additions and 32 deletions

View file

@ -1,7 +1,8 @@
// <AgentSsh> — one agent's SSH access in an opened agent card: a badge that
// opens an explainer, the switch, a folded "on the target" section with the text
// a machine needs to trust the agent's certificates, and a folded "connections"
// section for the agent's named connections (`AgentSshConnections`).
// opens a warning and the switch, then three folded sections: "how it works"
// (the explainer), "on the target" (the text a machine needs to trust the
// agent's certificates) and "connections" (the agent's named connections, in
// `AgentSshConnections`).
//
// The switch is the `ssh` field of `GET/PUT /api/agents/{name}/permissions`,
// shared with the other grant controls through `useGrant`. The CA key comes
@ -86,22 +87,6 @@ export function AgentSsh({ agent, g }: { agent: string; g: GrantState }) {
sandbox stops it doing, it can do on a machine it can log in to. Only
allow this for machines you would let {agent} control completely.
</p>
<p class="ui-badge-popover-help">
Lets {agent} log in over SSH to machines that trust the swarm's SSH
certificate authority. With this on, {agent} signs its own key for a
certificate that names it, on its first <code>ssh</code> after the
swarm has updated its login, which takes seconds. The certificate
lasts one hour, and the key lives in memory only. Switching off stops
new certificates within seconds; one already signed stays valid until
it expires, an hour after signing. A machine has to trust the
authority before anything works: see "on the target" below.
</p>
<p class="ui-badge-popover-help" role="note">
{agent} accepts the key a machine shows on the first connection and
refuses a changed one afterwards (trust on first use). If a host's key
really changed, check the new key out of band, then forget the old one
with <code>{forgetHostKeyCommand(agent)}</code> on the hive host.
</p>
<CheckboxField
id={`ssh-${agent}`}
label={`allow ${agent} to get SSH certificates`}
@ -121,6 +106,27 @@ export function AgentSsh({ agent, g }: { agent: string; g: GrantState }) {
problem={saveError}
/>
) : null}
<details class="ui-agent-ssh-target">
<summary>how it works</summary>
<p class="ui-badge-popover-help">
Lets {agent} log in over SSH to machines that trust the swarm's SSH
certificate authority. With this on, {agent} signs its own key for a
certificate that names it, on its first <code>ssh</code> after the
swarm has updated its login, which takes seconds. The certificate
lasts one hour, and the key lives in memory only. Switching off
stops new certificates within seconds; one already signed stays
valid until it expires, an hour after signing. A machine has to
trust the authority before anything works: see "on the target"
below.
</p>
<p class="ui-badge-popover-help" role="note">
{agent} accepts the key a machine shows on the first connection and
refuses a changed one afterwards (trust on first use). If a host's
key really changed, check the new key out of band, then forget the
old one with <code>{forgetHostKeyCommand(agent)}</code> on the hive
host.
</p>
</details>
<details class="ui-agent-ssh-target">
<summary>on the target</summary>
{snippet === null ? (

View file

@ -1,6 +1,13 @@
/* The named SSH connections of an agent card: a table that scrolls on its own on
a narrow screen rather than breaking the popover. */
/* The sections of the ssh popover never grow past it: a wide table scrolls in its
own box instead of giving the popover a sideways scroll. */
.ui-agent-ssh-target {
max-width: 100%;
}
.ui-agent-ssh-connections {
min-width: 0;
max-width: 100%;
display: flex;
flex-direction: column;
gap: 0.75em;

View file

@ -28,7 +28,7 @@ import {
import { createPortal } from "preact/compat";
import type { ComponentChildren, RefObject } from "preact";
import { useDismiss } from "@hive/shared/use-dismiss.js";
import { clampLeft } from "../table/popover-position.js";
import { clampLeft, placeVertically } from "../table/popover-position.js";
import { focusInto, restoreFocus, triggerOf } from "./focus.js";
import "./Popover.css";
@ -52,7 +52,11 @@ export function Popover({
children: ComponentChildren;
}) {
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState<{ top: number; left: number } | null>(null);
const [pos, setPos] = useState<{
top: number;
left: number;
maxHeight: number;
} | null>(null);
const id = useId();
const wasOpen = useRef(false);
@ -97,20 +101,42 @@ export function Popover({
if (!anchor) return;
const r = anchor.getBoundingClientRect();
const width = ref.current?.offsetWidth ?? 0;
const top = r.bottom + GAP_PX;
// The content's own height, not the box's: the box is already cut to the
// room it was given, and measuring that would keep it cut.
const content = (ref.current?.scrollHeight ?? 0) + 2;
const { top, maxHeight } = placeVertically(
r.top,
r.bottom,
content,
window.innerHeight,
GAP_PX,
);
const left = clampLeft(r.left, width, window.innerWidth);
// Scrolling anywhere (the streaming terminal included) calls this; keep
// the old object when nothing moved so the subtree does not re-render.
setPos((prev) =>
prev && prev.top === top && prev.left === left ? prev : { top, left },
prev &&
prev.top === top &&
prev.left === left &&
prev.maxHeight === maxHeight
? prev
: { top, left, maxHeight },
);
}
place();
window.addEventListener("scroll", place, true);
window.addEventListener("resize", place);
// The content can change size while the box is open (a folded section
// opens), and the box then needs another place.
const watch =
typeof ResizeObserver === "undefined" || !ref.current
? null
: new ResizeObserver(place);
if (watch && ref.current) watch.observe(ref.current);
return () => {
window.removeEventListener("scroll", place, true);
window.removeEventListener("resize", place);
watch?.disconnect();
};
}, [open, anchorRef]);
@ -129,7 +155,11 @@ export function Popover({
ref={ref}
style={
pos
? { top: `${pos.top}px`, left: `${pos.left}px` }
? {
top: `${pos.top}px`,
left: `${pos.left}px`,
maxHeight: `${pos.maxHeight}px`,
}
: { visibility: "hidden" }
}
>

View file

@ -1,4 +1,4 @@
// Where a table's filter popover sits, without a DOM.
// Where a floating popover sits, without a DOM.
// Space kept between a floating box and the viewport's edge.
const POPOVER_EDGE_PX = 8;
@ -17,3 +17,27 @@ export function clampLeft(
const maxLeft = viewportWidth - width - POPOVER_EDGE_PX;
return Math.max(POPOVER_EDGE_PX, Math.min(left, maxLeft));
}
/**
* The top edge and the tallest height for a popover of `height` that hangs
* from an anchor spanning `anchorTop` to `anchorBottom`: under the anchor when
* it fits there, over it when it does not and there is more room above, and
* otherwise under it again with the height cut to the room left, so it scrolls
* inside itself. A popover under a badge near the foot of the window would
* otherwise run off the page.
*/
export function placeVertically(
anchorTop: number,
anchorBottom: number,
height: number,
viewportHeight: number,
gap: number,
): { top: number; maxHeight: number } {
const below = viewportHeight - anchorBottom - gap - POPOVER_EDGE_PX;
const above = anchorTop - gap - POPOVER_EDGE_PX;
if (height <= below || above <= below) {
return { top: anchorBottom + gap, maxHeight: Math.max(below, 0) };
}
const h = Math.min(height, above);
return { top: anchorTop - gap - h, maxHeight: above };
}