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
rename.ts2.3 KB
// Renaming a row in place: the pencil opens a field with the keyboard in it,
// Enter or leaving the field saves what was typed, Escape keeps the title,
// and a field open on another row closes first, saving what was typed there
// as Enter would.
import * as Effect from "effect/Effect";
import { Host, Notify, Plugin } from "divergence/effect";
import type { HostError } from "divergence/effect";
import type { Editing, SidebarState } from "./sections.ts";
const describe = (error: unknown): string => (error instanceof Error && error.message ? error.message : "unknown error");
export const makeRename = Effect.fn("Sidebar.renaming")(function* (state: SidebarState, redraw: () => void) {
const host = yield* Host;
const notify = yield* Notify;
const plugin = yield* Plugin;
const startRename = Effect.fn("Sidebar.startRename")(function* (editing: Omit<Editing, "seed">) {
if (state.editing?.id === editing.id) return;
yield* commitRename();
state.editing = { ...editing, seed: editing.title };
redraw();
plugin.api.focus(`rename-${editing.id}`);
});
const cancelRename = (): Effect.Effect<void> =>
Effect.sync(() => {
if (!state.editing) return;
state.editing = null;
redraw();
});
// `id` names the field that asks: a field that loses the keyboard after
// Escape, or after another row's field opened, has nothing left to save.
const commitRename = Effect.fn("Sidebar.commitRename")(function* (id?: string) {
const editing = state.editing;
if (!editing || (id !== undefined && editing.id !== id)) return;
state.editing = null;
redraw();
// Unchanged, the title stays the host's: a title the agent gave the chat
// meanwhile survives, and a task row keeps following its first chat.
if (editing.title.trim() === editing.seed.trim()) return;
// A task row's name is the task's; clearing it goes back to its first
// chat's title (document 1).
const renamed: Effect.Effect<unknown, HostError> = editing.task
? host.call("host/tasks.update", { taskId: editing.task, title: editing.title.trim() || null })
: host.call("host/chats.update", { chatId: editing.id, title: editing.title.trim() || "New Chat" });
yield* renamed.pipe(Effect.catch((error: unknown) => notify.error(`Rename failed: ${describe(error)}`)));
});
return { startRename, cancelRename, commitRename };
});
No comments yet.