Watch
0
0
Fork
You've already forked hyperhive
0

swarm ui: the header's tabs can be dragged onto each other to reorder them

Group tabs now show the grip and drag like agent tabs, and every tab in the strip takes the place of the tab another one is dropped on; the order is kept with the open tabs. An agent's tab keeps its other drag, onto a terminal to split it. moveTab is a pure function with tests.
This commit is contained in:
iris 2026-11-20 18:32:23 +01:00
commit f4421b216f
5 changed files with 114 additions and 14 deletions

View file

@ -223,6 +223,10 @@
padding-left: 1.3em; padding-left: 1.3em;
cursor: grab; cursor: grab;
} }
/* The tab a dragged tab would take the place of. */
.shell-tab-drop {
box-shadow: inset 2px 0 0 var(--purple);
}
.shell-tab-grip { .shell-tab-grip {
position: absolute; position: absolute;
left: 0.35em; left: 0.35em;

View file

@ -52,6 +52,7 @@ import {
terminalAgentOf, terminalAgentOf,
} from "../agent/terminal/split-layout.js"; } from "../agent/terminal/split-layout.js";
import { endAgentDrag, startAgentDrag } from "../agent/terminal/termDrag.js"; import { endAgentDrag, startAgentDrag } from "../agent/terminal/termDrag.js";
import { draggedTab, endTabDrag, startTabDrag } from "./tab-order.js";
import { useTermGroups } from "../agent/terminal/useTermGroups.js"; import { useTermGroups } from "../agent/terminal/useTermGroups.js";
import { useSwarmName } from "./useSwarmName.js"; import { useSwarmName } from "./useSwarmName.js";
import "./Shell.css"; import "./Shell.css";
@ -167,6 +168,7 @@ function DynamicTabLink({
onClose, onClose,
agent, agent,
onRename, onRename,
onMove,
}: { }: {
href: string; href: string;
label: string; label: string;
@ -176,29 +178,52 @@ function DynamicTabLink({
agent?: string; agent?: string;
/** Set on a group's tab: a double click renames the group. */ /** Set on a group's tab: a double click renames the group. */
onRename?: (name: string) => void; onRename?: (name: string) => void;
/** Called with the tab dragged onto this one, which takes this tab's place. */
onMove: (from: string) => void;
}) { }) {
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
const [over, setOver] = useState(false);
return ( return (
<span <span
class={ class={
"shell-tab" + "shell-tab" +
(active ? " shell-tab-active" : "") + (active ? " shell-tab-active" : "") +
(agent !== undefined ? " shell-tab-draggable" : "") " shell-tab-draggable" +
(over ? " shell-tab-drop" : "")
} }
title={ title={
agent === undefined ? undefined : "drag onto a terminal to split it" agent === undefined
? "drag onto another tab to move it"
: "drag onto another tab to move it, or onto a terminal to split it"
} }
draggable={agent !== undefined && !editing} draggable={!editing}
onDragStart={ onDragStart={(e) => {
agent === undefined ? undefined : (e) => startAgentDrag(e, agent) startTabDrag(e, href);
} if (agent !== undefined) startAgentDrag(e, agent);
onDragEnd={agent === undefined ? undefined : endAgentDrag} }}
onDragEnd={() => {
endTabDrag();
endAgentDrag();
setOver(false);
}}
onDragOver={(e) => {
const from = draggedTab();
if (from === null || from === href) return;
e.preventDefault();
setOver(true);
}}
onDragLeave={() => setOver(false)}
onDrop={(e) => {
const from = draggedTab();
setOver(false);
if (from === null || from === href) return;
e.preventDefault();
onMove(from);
}}
> >
{agent !== undefined && ( <span class="shell-tab-grip" aria-hidden="true">
<span class="shell-tab-grip" aria-hidden="true"> <GripIcon />
<GripIcon /> </span>
</span>
)}
{editing && onRename !== undefined ? ( {editing && onRename !== undefined ? (
<input <input
class="shell-tab-rename" class="shell-tab-rename"
@ -279,7 +304,7 @@ export function Shell({ children }: { children: ComponentChildren }) {
// why it was reverted). Entirely separate from the fixed `NAV_ITEMS` // why it was reverted). Entirely separate from the fixed `NAV_ITEMS`
// list above, so they get their own small component (`DynamicTabLink`) // list above, so they get their own small component (`DynamicTabLink`)
// rather than reusing `NavLink`. // rather than reusing `NavLink`.
const { tabs, close: closeTab } = useDynamicTabs(); const { tabs, close: closeTab, move: moveTabTo } = useDynamicTabs();
const termGroups = useTermGroups(); const termGroups = useTermGroups();
// The components tab is a developer page: it goes while "hide developer // The components tab is a developer page: it goes while "hide developer
@ -521,6 +546,7 @@ export function Shell({ children }: { children: ComponentChildren }) {
label={group === undefined ? tab.label : groupTitle(group)} label={group === undefined ? tab.label : groupTitle(group)}
active={tab.href === location} active={tab.href === location}
agent={terminalAgentOf(tab.href) ?? undefined} agent={terminalAgentOf(tab.href) ?? undefined}
onMove={(from) => moveTabTo(from, tab.href)}
onRename={ onRename={
groupId === null groupId === null
? undefined ? undefined

View file

@ -0,0 +1,37 @@
// The order of the header's dynamic tabs, and the drag that changes it. A tab
// dropped on another takes that tab's place and the ones between shift by one.
// As in `termDrag`, a drop target reads a drag's payload only on drop, so the
// dragged tab's `href` also sits in a module variable for the hover.
export const TAB_DRAG_TYPE = "application/x-hive-tab";
/** `tabs` with the tab at `from` moved to the place of the tab at `to`. */
export function moveTab<T extends { href: string }>(
tabs: T[],
from: string,
to: string,
): T[] {
const i = tabs.findIndex((t) => t.href === from);
const j = tabs.findIndex((t) => t.href === to);
if (i < 0 || j < 0 || i === j) return tabs;
const next = [...tabs];
const [moved] = next.splice(i, 1);
next.splice(j, 0, moved);
return next;
}
let dragged: string | null = null;
export function startTabDrag(e: DragEvent, href: string) {
dragged = href;
e.dataTransfer?.setData(TAB_DRAG_TYPE, href);
if (e.dataTransfer) e.dataTransfer.effectAllowed = "move";
}
export function endTabDrag() {
dragged = null;
}
/** The tab being dragged, `null` when the drag is something else. */
export function draggedTab(): string | null {
return dragged;
}

View file

@ -20,6 +20,7 @@ import {
readLocalSetting, readLocalSetting,
useLocalSetting, useLocalSetting,
} from "@hive/shared/settings-storage.js"; } from "@hive/shared/settings-storage.js";
import { moveTab } from "./tab-order.js";
export interface DynamicTab { export interface DynamicTab {
href: string; href: string;
@ -34,6 +35,8 @@ export interface UseDynamicTabsResult {
open: (tab: DynamicTab) => void; open: (tab: DynamicTab) => void;
/** Removes the tab. If it was the active route, navigates to `fallbackHref` (default `/agents`, since agent terminal tabs are the only kind today). */ /** Removes the tab. If it was the active route, navigates to `fallbackHref` (default `/agents`, since agent terminal tabs are the only kind today). */
close: (href: string, fallbackHref?: string) => void; close: (href: string, fallbackHref?: string) => void;
/** Moves the tab `from` to the place of the tab `to`, and keeps the order. */
move: (from: string, to: string) => void;
} }
export function useDynamicTabs(): UseDynamicTabsResult { export function useDynamicTabs(): UseDynamicTabsResult {
@ -83,5 +86,9 @@ export function useDynamicTabs(): UseDynamicTabsResult {
if (location === href) navigate(fallbackHref); if (location === href) navigate(fallbackHref);
} }
return { tabs, open, close }; function move(from: string, to: string) {
setTabs(moveTab(tabs, from, to));
}
return { tabs, open, close, move };
} }

View file

@ -0,0 +1,26 @@
// The order of the header's dynamic tabs. Run by `npm test`.
import assert from "node:assert/strict";
import { test } from "node:test";
import { moveTab } from "../src/shell/tab-order.ts";
const tabs = ["a", "b", "c", "d"].map((href) => ({ href }));
const order = (list) => list.map((t) => t.href).join("");
test("a tab moved right takes the place of the tab it lands on", () => {
assert.equal(order(moveTab(tabs, "a", "c")), "bcad");
});
test("a tab moved left lands before the tab it is dropped on", () => {
assert.equal(order(moveTab(tabs, "d", "b")), "adbc");
});
test("dropping a tab on itself, or naming a tab that is gone, changes nothing", () => {
assert.equal(moveTab(tabs, "b", "b"), tabs);
assert.equal(moveTab(tabs, "x", "b"), tabs);
assert.equal(moveTab(tabs, "b", "x"), tabs);
});
test("the list it was given is left as it was", () => {
moveTab(tabs, "a", "d");
assert.equal(order(tabs), "abcd");
});