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
task-actions.ts5.8 KB
// The sidebar's actions on tasks and rows (specs/tasks-and-splits/01 and 04):
// the host's task events, the chevron that lists a task's chats, joining a
// task or leaving it by a drop, the row menu, and the keys its entries and the search button show.
import * as Effect from "effect/Effect";
import { App, Host, Kernel, Notify, Plugin } from "divergence/effect";
import type { CommandInfo, HostEvents } from "divergence";
import type { Over, SidebarState } from "./sections.ts";
import { EXPANDED_KEY, forgetTask, noteTask, taskChats } from "./sections.ts";
const describe = (error: unknown): string => (error instanceof Error && error.message ? error.message : "unknown error");
const keysOf = (commands: readonly CommandInfo[]) =>
Object.fromEntries(commands.flatMap((command) => (command.labels?.[0] ? [[command.id, command.labels[0]]] : [])));
export const makeTasks = Effect.fn("Sidebar.tasks")(function* (
state: SidebarState,
redraw: () => void,
dropChat: (id: string, over?: Over) => Effect.Effect<void>,
) {
// A chat that left its task by a drop in the list, and where it was let go: it lands
// there once the host reports it as a row of its own.
let landing: { chatId: string; over: Over } | null = null;
const host = yield* Host;
const kernel = yield* Kernel;
const app = yield* App;
const notify = yield* Notify;
const plugin = yield* Plugin;
// The host's tasks: a row that changed, or one that went.
const taskChanged = Effect.fn("Sidebar.taskChanged")(function* (event: HostEvents["task_changed"]) {
noteTask(state, event.task);
redraw();
const own = landing && state.homes[landing.chatId] === event.task.id && taskChats(state, event.task).length === 1;
if (!own || !landing) return;
const { chatId, over } = landing;
landing = null;
yield* dropChat(chatId, over);
});
const taskRemoved = (event: HostEvents["task_removed"]): Effect.Effect<void> =>
Effect.sync(() => {
forgetTask(state, event.taskId);
redraw();
});
// The chevron among a task row's actions lists its chats under it, or
// hides them; the choice is kept (`expandedTasks`).
// A carried chat let go on the middle third of a row joins that row's task; one let go in
// the list (a gap, a heading) while its task holds others leaves it as a row of its own.
const placeChat = Effect.fn("Sidebar.placeChat")(function* (
chatId: string,
target: { kind: "join"; taskId: string } | { kind: "alone" },
over?: Over,
) {
if (target.kind === "alone" && over) landing = { chatId, over };
state.joining = null;
redraw();
const current = yield* app.state;
const onScreen = current.chatId ? (state.homes[current.chatId] ?? null) : null;
yield* host
.call("host/layouts.place", { tab: { kind: "chat", chatId }, target, onScreen })
.pipe(Effect.catch((error: unknown) => notify.error(`Could not move the chat: ${describe(error)}`)));
});
// The row under a carried chat's pointer, whose middle third takes it.
const markJoining = (row: string | null): Effect.Effect<void> =>
Effect.sync(() => {
if (state.joining === row) return;
state.joining = row;
redraw();
});
// A row's menu opens at once with its own entries; the plugins' entries for it follow, and
// so do the keys its entries' commands are bound to now, which the next opening shows at once.
const openRowMenu = Effect.fn("Sidebar.openRowMenu")(function* (chatId: string, child: boolean, at: { x: number; y: number } | null = null) {
state.rowMenu = { chatId, child, at, entries: [] };
state.popover = null;
redraw();
// The list opens as a press on its trigger does, once the trigger is drawn.
plugin.api.focus("row-menu");
const [{ entries }, { commands }] = yield* Effect.all(
[kernel.call("menu.entries", { place: "row" }), kernel.call("commands", {})],
{ concurrency: "unbounded" },
);
state.keys = keysOf(commands);
if (state.rowMenu?.chatId !== chatId) return;
state.rowMenu = { ...state.rowMenu, entries };
redraw();
});
// The keys the commands are bound to now, for the search button's tooltip
// and the row menu; read at the start and whenever they may have changed.
const loadKeys = Effect.fn("Sidebar.loadKeys")(function* () {
const { commands } = yield* kernel.call("commands", {});
state.keys = keysOf(commands);
redraw();
});
const closeRowMenu = (): Effect.Effect<void> =>
Effect.sync(() => {
if (state.rowMenu === null) return;
state.rowMenu = null;
redraw();
});
// A plugin's entry runs on the row: its task, and the chat for a child row.
const runRowEntry = Effect.fn("Sidebar.runRowEntry")(function* (id: string, chatId: string, child: boolean) {
yield* closeRowMenu();
const taskId = state.homes[chatId] ?? chatId;
yield* kernel.call("run_command", { id, target: child ? { kind: "row", taskId, chatId } : { kind: "row", taskId } });
});
// A child row's Close: the chat leaves its task for Complete, as closing its tab does.
const closeChild = Effect.fn("Sidebar.closeChild")(function* (chatId: string) {
yield* host
.call("host/layouts.close_tab", { tab: { kind: "chat", chatId } })
.pipe(Effect.catch((error: unknown) => notify.error(`Could not close the chat: ${describe(error)}`)));
});
const toggleExpanded = Effect.fn("Sidebar.toggleExpanded")(function* (taskId: string) {
if (state.expanded.has(taskId)) state.expanded.delete(taskId);
else state.expanded.add(taskId);
redraw();
yield* host
.call("host/storage.set", { key: EXPANDED_KEY, value: [...state.expanded] })
.pipe(Effect.catch((error: unknown) => notify.error(`Could not keep the listed tasks: ${describe(error)}`)));
});
return { taskChanged, taskRemoved, toggleExpanded, placeChat, markJoining, openRowMenu, loadKeys, closeRowMenu, runRowEntry, closeChild };
});
No comments yet.