| 1 |
// The side bar: Channels, Agents, Inbox and Briefs. |
| 2 |
import * as vscode from "vscode"; |
| 3 |
import { Model } from "./model"; |
| 4 |
import { when } from "./pure"; |
| 5 |
|
| 6 |
abstract class View implements vscode.TreeDataProvider<vscode.TreeItem> { |
| 7 |
private readonly emitter = new vscode.EventEmitter<void>(); |
| 8 |
readonly onDidChangeTreeData = this.emitter.event; |
| 9 |
constructor(protected readonly model: Model) { |
| 10 |
model.onDidChange(() => this.emitter.fire()); |
| 11 |
} |
| 12 |
getTreeItem(i: vscode.TreeItem): vscode.TreeItem { |
| 13 |
return i; |
| 14 |
} |
| 15 |
getChildren(): vscode.TreeItem[] { |
| 16 |
if (this.model.error) { |
| 17 |
const i = new vscode.TreeItem("Spectra is not answering"); |
| 18 |
i.description = this.model.error; |
| 19 |
i.tooltip = this.model.error; |
| 20 |
i.iconPath = new vscode.ThemeIcon("warning"); |
| 21 |
i.command = { command: "spectra.refresh", title: "Refresh" }; |
| 22 |
return [i]; |
| 23 |
} |
| 24 |
return this.items(); |
| 25 |
} |
| 26 |
protected abstract items(): vscode.TreeItem[]; |
| 27 |
} |
| 28 |
|
| 29 |
const PRESENCE: Record<string, string> = { online: "charts.green", busy: "charts.purple", idle: "charts.yellow", blocked: "charts.orange", paused: "charts.yellow", offline: "disabledForeground" }; |
| 30 |
|
| 31 |
export class Agents extends View { |
| 32 |
protected items(): vscode.TreeItem[] { |
| 33 |
return this.model.agents |
| 34 |
.filter((a) => !a.disabled) |
| 35 |
.map((a) => { |
| 36 |
const i = new vscode.TreeItem(a.name); |
| 37 |
i.description = `${a.presence} · #${a.project}${a.status ? ` · ${a.status}` : ""}`; |
| 38 |
const expires = a.delegation_expires ? `\nMay act for you until ${new Date(a.delegation_expires).toLocaleString()}` : ""; |
| 39 |
const state = a.state && a.state !== "ok" ? `\n⚠ ${a.state}` : ""; |
| 40 |
i.tooltip = `${a.display}\n${a.kind}${expires}${state}`; |
| 41 |
i.iconPath = new vscode.ThemeIcon(a.state && a.state !== "ok" ? "warning" : "circle-filled", new vscode.ThemeColor(PRESENCE[a.presence] ?? "disabledForeground")); |
| 42 |
i.contextValue = "agent"; |
| 43 |
i.id = `agent:${a.name}`; |
| 44 |
return i; |
| 45 |
}); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
export class Channels extends View { |
| 50 |
protected items(): vscode.TreeItem[] { |
| 51 |
const mine = this.model.folderProject(); |
| 52 |
const rows = [...this.model.channels].filter((c) => c.kind !== "system").sort((a, b) => Number(b.name === mine) - Number(a.name === mine)); |
| 53 |
return rows.map((c) => { |
| 54 |
const i = new vscode.TreeItem(c.display); |
| 55 |
i.description = c.purpose ?? undefined; |
| 56 |
i.tooltip = `${c.display}${c.purpose ? `\n${c.purpose}` : ""}\n${c.messages} message(s)`; |
| 57 |
i.iconPath = new vscode.ThemeIcon(c.kind === "direct" ? "mention" : c.name === mine ? "star-full" : "comment-discussion"); |
| 58 |
i.command = { command: "spectra.openChannel", title: "Open", arguments: [c.name] }; |
| 59 |
i.id = `channel:${c.name}`; |
| 60 |
return i; |
| 61 |
}); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
export class Inbox extends View { |
| 66 |
protected items(): vscode.TreeItem[] { |
| 67 |
if (!this.model.inbox.length) { |
| 68 |
const i = new vscode.TreeItem("Nothing unread"); |
| 69 |
i.iconPath = new vscode.ThemeIcon("check"); |
| 70 |
return [i]; |
| 71 |
} |
| 72 |
return this.model.inbox.map((m) => { |
| 73 |
const i = new vscode.TreeItem(m.summary.replace(/\s+/g, " ").slice(0, 120)); |
| 74 |
i.description = `${m.from_display || m.from} · ${m.channel} · ${when(m.at_ms)}`; |
| 75 |
i.iconPath = new vscode.ThemeIcon(m.kind === "dm" ? "mail" : "mention"); |
| 76 |
i.command = { command: "spectra.openChannel", title: "Open", arguments: [m.channel.replace(/^#/, "")] }; |
| 77 |
return i; |
| 78 |
}); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
export class Briefs extends View { |
| 83 |
protected items(): vscode.TreeItem[] { |
| 84 |
const overall = new vscode.TreeItem("Overall brief"); |
| 85 |
overall.description = this.model.briefs.overall ? firstLine(this.model.briefs.overall) : "not set"; |
| 86 |
overall.iconPath = new vscode.ThemeIcon("globe"); |
| 87 |
overall.command = { command: "spectra.editOverallBrief", title: "Edit" }; |
| 88 |
const items = [overall]; |
| 89 |
for (const p of this.model.projects) { |
| 90 |
const b = this.model.briefs.projects.find((x) => x.project === p.name); |
| 91 |
const i = new vscode.TreeItem(p.name); |
| 92 |
i.description = b ? firstLine(b.text) : "not set"; |
| 93 |
i.iconPath = new vscode.ThemeIcon("note"); |
| 94 |
i.command = { command: "spectra.editProjectBrief", title: "Edit", arguments: [p.name] }; |
| 95 |
items.push(i); |
| 96 |
} |
| 97 |
return items; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
function firstLine(s: string): string { |
| 102 |
return s.trim().split("\n")[0].slice(0, 80); |
| 103 |
} |