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.5
Permissions in 0.2.5
Files
archive.ts6.7 KB
// Archiving rows and chats, and the notice at the foot of the list that
// offers them back for a few seconds (Undo): the chats come out of the
// archive into the task they were in, checked off and named as they were,
// and the row takes its old place in its section once the host reports it
// back as it was.
import * as Effect from "effect/Effect";
import { App, Host, Notify } from "divergence/effect";
import type { HostError } from "divergence/effect";
import type { Chat } from "divergence";
import type { PlaceTarget } from "divergence/protocol";
import type { ArchivedRow, SidebarState } from "./sections.ts";
import { archivedChild, archivedRow, hasPane, isBack, isTaskRow, nextOpenChat, taskChats, taskOf } from "./sections.ts";
/** How long the notice offers Undo while the pointer is not on it. */
export const UNDO_MS = 5000;
const describe = (error: unknown): string => (error instanceof Error && error.message ? error.message : "unknown error");
export const makeArchive = Effect.fn("Sidebar.archiving")(function* (
state: SidebarState,
redraw: () => void,
savePlaced: () => Effect.Effect<void>,
) {
const host = yield* Host;
const app = yield* App;
const notify = yield* Notify;
// Rows brought back whose place in the list waits for the host to report them as they were.
const returning = new Set<ArchivedRow>();
let timer: ReturnType<typeof setTimeout> | undefined;
yield* Effect.addFinalizer(() => Effect.sync(() => clearTimeout(timer)));
// The notice goes once its time is up, and the pointer on it holds it.
const holdNotice = (held: boolean): Effect.Effect<void> =>
Effect.sync(() => {
clearTimeout(timer);
if (held || !state.undo.length) return;
timer = setTimeout(() => {
state.undo = [];
redraw();
}, UNDO_MS);
});
// Once the host took a row away, the notice offers it back, after the rows it offers already.
const offer =
(row: ArchivedRow) =>
(archived: Effect.Effect<unknown, HostError>): Effect.Effect<void, HostError> =>
archived.pipe(
Effect.andThen(() => {
state.undo = [...state.undo, row];
redraw();
return holdNotice(false);
}),
);
// Archiving the chat on screen moves the screen, as Close Chat does: to
// another chat of its task when the task keeps its row, else to the next
// open row below the archived one (`lead`), or the one above. Chosen
// before the host is asked, while the row is still in the list.
const successor = Effect.fn("Sidebar.successor")(function* (gone: readonly string[], lead: string) {
const current = yield* app.state;
if (!current.chatId || !gone.includes(current.chatId)) return undefined;
const task = taskOf(state, current.chatId);
const sibling = task ? taskChats(state, task).find((chat) => !gone.includes(chat.id)) : undefined;
return sibling ?? nextOpenChat(state, lead);
});
const moveTo = (next: Chat | undefined) => (next ? app.select(next.id, next.workspaceId) : Effect.void);
// An archived chat leaves the list and its transcript stays with the host. A chat
// archived out of a task of several leaves the task, which keeps its row.
const archive = Effect.fn("Sidebar.archive")(function* (chat: Chat) {
const row = isTaskRow(state, chat) ? archivedChild(state, chat) : archivedRow(state, chat);
const next = yield* successor([chat.id], chat.id);
yield* host
.call("host/chats.update", { chatId: chat.id, archived: true })
.pipe(
offer(row),
Effect.andThen(moveTo(next)),
Effect.catch((error: unknown) => notify.error(`Could not archive the chat: ${describe(error)}`)),
);
});
// Archiving a task row archives every chat in it, and the row leaves the list.
const archiveTask = Effect.fn("Sidebar.archiveTask")(function* (taskId: string) {
const chats = taskChats(state, state.tasks[taskId]!);
const row = archivedRow(state, chats[0]!);
const next = yield* successor(
chats.map((chat) => chat.id),
chats[0]!.id,
);
yield* host
.call("host/tasks.archive", { taskId })
.pipe(
offer(row),
Effect.andThen(moveTo(next)),
Effect.catch((error: unknown) => notify.error(`Could not archive the task: ${describe(error)}`)),
);
});
// A row brought back takes its old place in its section once the host reports it as it was.
const land = Effect.fn("Sidebar.land")(function* () {
let landed = false;
for (const row of returning) {
if (!isBack(state, row)) continue;
returning.delete(row);
state.placed[row.chatIds[0]!] = row.placed!;
landed = true;
}
if (!landed) return;
redraw();
yield* savePlaced();
});
// One row out of the archive: its first chat comes back as a row of its own, checked off
// as it was; the others join it, or it goes back to its tab's place in the task it left.
const restore = (row: ArchivedRow): Effect.Effect<void> =>
Effect.gen(function* () {
const [lead, ...others] = row.chatIds as [string, ...string[]];
if (row.placed) returning.add(row);
const back = yield* host.call("host/chats.update", { chatId: lead, archived: false, ...(row.completed ? { completed: true } : {}) });
const current = yield* app.state;
const onScreen = current.chatId ? (state.homes[current.chatId] ?? null) : null;
const place = (chatId: string, target: PlaceTarget) =>
host.call("host/layouts.place", { tab: { kind: "chat", chatId }, target, onScreen });
for (const chatId of others) {
yield* host.call("host/chats.update", { chatId, archived: false });
yield* place(chatId, { kind: "join", taskId: back.task! });
}
const task = row.from ? state.tasks[row.from.taskId] : undefined;
if (row.from && task)
yield* place(lead, hasPane(task.layout.root, row.from.paneId) ? { kind: "strip", paneId: row.from.paneId, index: row.from.index } : { kind: "join", taskId: task.id });
const home = row.from ? task?.id : back.task;
if (row.name && home && state.tasks[home]?.title !== row.name) yield* host.call("host/tasks.update", { taskId: home, title: row.name });
if (row.queued && state.chat) yield* state.chat.submitDraft({ chatId: lead, queue: true });
yield* land();
}).pipe(
Effect.catch((error: unknown) => {
returning.delete(row);
return notify.error(`Could not restore “${row.title}”: ${describe(error)}`);
}),
);
// Undo: every row the notice offers comes back, and the notice goes.
const undoArchive = Effect.fn("Sidebar.undoArchive")(function* () {
const rows = state.undo;
state.undo = [];
clearTimeout(timer);
redraw();
for (const row of rows) yield* restore(row);
});
return { archive, archiveTask, land, holdNotice, undoArchive };
});
No comments yet.