Official
sidebar
Built-in sidebar UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/sidebar@0.2.6
Permissions in 0.2.6
Files
top-row.test.ts4.5 KB
// The sidebar's top row: New chat, the workspace picker and a search button
// that opens the chat search (Cmd+P). The row sits in the title strip, after
// the sidebar toggle, while the room there holds it, and at the top of the
// sidebar otherwise.
import assert from "node:assert/strict";
import { test } from "node:test";
import type { EffectViewContext } from "divergence/effect";
import type { Sidebar } from "./controller.ts";
import { initialState } from "./sections.ts";
import { renderSidebar, renderTitleLead } from "./views.tsx";
type Node = Record<string, unknown> & { type?: string; id?: string; children?: Node[] };
function sidebarTree() {
const state = initialState();
state.loaded = true;
let updates = 0;
const ran: unknown[] = [];
const ctx = { app: { route: "chat" }, state: {}, plugin: "sidebar", update() {}, run: (effect: unknown) => ran.push(effect) } as unknown as EffectViewContext<{ cmd?: boolean }>;
const commands: string[] = [];
const sidebar = {
state,
views: [{ update: () => updates++ }],
runCommand: (id: string) => {
commands.push(id);
return id;
},
} as unknown as Sidebar["Service"];
return {
state,
commands,
ran,
updates: () => updates,
tree: () => renderSidebar(ctx, sidebar) as unknown as Node,
title: () => renderTitleLead(ctx, sidebar) as unknown as Node,
};
}
// The chain of nodes from the root down to the one with `id`.
function pathTo(node: Node, id: string): Node[] | null {
if (node.id === id) return [node];
for (const child of node.children ?? []) {
const path = child && typeof child === "object" ? pathTo(child, id) : null;
if (path) return [node, ...path];
}
return null;
}
function find(node: Node, test: (node: Node) => boolean): Node | null {
if (test(node)) return node;
for (const child of node.children ?? []) {
const found = child && typeof child === "object" ? find(child, test) : null;
if (found) return found;
}
return null;
}
const call = (node: Node | null | undefined, handler: string, event: unknown = {}) => (node![handler] as (event: unknown) => void)(event);
test("the search button in the top row opens the chat search instead of a field of its own", () => {
const { tree, commands, ran } = sidebarTree();
for (const id of ["new-chat", "workspace-filter", "chat-search-open"]) assert.ok(pathTo(tree(), id), `the row holds ${id}`);
call(pathTo(tree(), "chat-search-open")!.at(-1), "onClick");
assert.deepEqual(commands, ["palette.chats"], "it runs Cmd+P's command");
assert.deepEqual(ran, ["palette.chats"]);
assert.equal(pathTo(tree(), "chat-search"), null, "the sidebar has no search field");
assert.ok(pathTo(tree(), "new-chat"), "the row stays as it was");
});
test("the search button's tooltip names the key the chat search is bound to now", () => {
const { state, tree } = sidebarTree();
const tooltip = () => pathTo(tree(), "chat-search-open")!.at(-1)!.tooltip;
assert.equal(tooltip(), "Search chats", "no key named before the keys are read");
state.keys = { "palette.chats": "⌘P" };
assert.equal(tooltip(), "Search chats (⌘P)");
state.keys = { "palette.chats": "⌃⇧F" };
assert.equal(tooltip(), "Search chats (⌃⇧F)", "a key the user bound instead");
});
test("the top row moves into the title strip while the room after the sidebar toggle holds it", () => {
const { tree, title, updates } = sidebarTree();
const lead = () => find(title(), (node) => node.alignTo === "title-lead");
const measure = (width: number) => call(find(lead()!, (node) => node.type === "canvas"), "onResize", { width, height: 1 });
assert.ok(lead(), "the title view measures the room after the toggle");
assert.equal(pathTo(lead()!, "new-chat"), null, "nothing there before it is measured");
measure(60);
assert.equal(pathTo(lead()!, "new-chat"), null, "a narrow sidebar keeps the row");
assert.ok(pathTo(tree(), "new-chat"));
measure(150);
assert.equal(updates(), 1, "both views draw again when the row moves");
for (const id of ["new-chat", "workspace-filter", "chat-search-open"]) {
assert.ok(pathTo(lead()!, id), `the strip holds ${id}`);
assert.equal(pathTo(tree(), id), null, `and the sidebar does not hold ${id}`);
}
assert.equal(pathTo(lead()!, "new-chat")!.at(-1)!.type, "button", "New chat is a title control there");
measure(160);
assert.equal(updates(), 1, "a wider room that changes nothing draws nothing");
// A hidden or peeked sidebar leaves no room: the row is back in the sidebar.
measure(0);
assert.equal(pathTo(lead()!, "new-chat"), null);
assert.ok(pathTo(tree(), "new-chat"));
});
No comments yet.