@ammduncan/easel 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ (function () {
2
+ "use strict";
3
+
4
+ const listEl = document.getElementById("session-list");
5
+ const emptyEl = document.getElementById("empty-state");
6
+ const countEl = document.getElementById("session-count");
7
+ const themeToggleEl = document.getElementById("theme-toggle");
8
+ const presetBtnEls = Array.from(document.querySelectorAll(".preset-btn"));
9
+ const densityBtnEls = Array.from(document.querySelectorAll(".density-btn"));
10
+
11
+ const CONFIG_KEY = "easel:config";
12
+ const LAST_VISITED_KEY = "easel:last-visited";
13
+ const PRESETS = ["paper", "aurora", "slate"];
14
+ const DENSITIES = ["carded", "flat"];
15
+
16
+ function currentTheme() {
17
+ return document.documentElement.getAttribute("data-theme") || "dark";
18
+ }
19
+ function currentPreset() {
20
+ return document.documentElement.getAttribute("data-preset") || "paper";
21
+ }
22
+ function currentDensity() {
23
+ return document.documentElement.getAttribute("data-density") || "carded";
24
+ }
25
+
26
+ function syncPresetButtons(active) {
27
+ presetBtnEls.forEach((b) => b.classList.toggle("active", b.dataset.preset === active));
28
+ }
29
+ function syncDensityButtons(active) {
30
+ densityBtnEls.forEach((b) => b.classList.toggle("active", b.dataset.density === active));
31
+ }
32
+
33
+ function applyConfig(patch, opts) {
34
+ const theme = patch.theme === "light" || patch.theme === "dark" ? patch.theme : currentTheme();
35
+ const preset = PRESETS.includes(patch.preset) ? patch.preset : currentPreset();
36
+ const density = DENSITIES.includes(patch.density) ? patch.density : currentDensity();
37
+ document.documentElement.setAttribute("data-theme", theme);
38
+ document.documentElement.setAttribute("data-preset", preset);
39
+ document.documentElement.setAttribute("data-density", density);
40
+ syncPresetButtons(preset);
41
+ syncDensityButtons(density);
42
+ try {
43
+ localStorage.setItem(CONFIG_KEY, JSON.stringify({ preset, theme, density }));
44
+ } catch {}
45
+ if (!opts || !opts.skipServer) {
46
+ fetch("/api/config", {
47
+ method: "POST",
48
+ headers: { "content-type": "application/json" },
49
+ body: JSON.stringify({ preset, theme, density }),
50
+ }).catch(() => {});
51
+ }
52
+ }
53
+
54
+ themeToggleEl.addEventListener("click", () => {
55
+ applyConfig({ theme: currentTheme() === "dark" ? "light" : "dark" });
56
+ });
57
+ presetBtnEls.forEach((btn) => {
58
+ btn.addEventListener("click", () => applyConfig({ preset: btn.dataset.preset }));
59
+ });
60
+ densityBtnEls.forEach((btn) => {
61
+ btn.addEventListener("click", () => applyConfig({ density: btn.dataset.density }));
62
+ });
63
+ syncPresetButtons(currentPreset());
64
+ syncDensityButtons(currentDensity());
65
+
66
+ // Hydrate from server on load — in case another tab changed config.
67
+ fetch("/api/config")
68
+ .then((r) => r.json())
69
+ .then((d) => {
70
+ if (d && d.config) applyConfig(d.config, { skipServer: true });
71
+ })
72
+ .catch(() => {});
73
+
74
+ function readLastVisited() {
75
+ try {
76
+ return JSON.parse(localStorage.getItem(LAST_VISITED_KEY) || "{}");
77
+ } catch {
78
+ return {};
79
+ }
80
+ }
81
+
82
+ function basenameOf(path) {
83
+ if (!path || typeof path !== "string") return null;
84
+ const trimmed = path.replace(/\/+$/, "");
85
+ const idx = trimmed.lastIndexOf("/");
86
+ return idx >= 0 ? trimmed.slice(idx + 1) : trimmed;
87
+ }
88
+
89
+ function relTime(ts) {
90
+ if (!ts) return "—";
91
+ const diff = Date.now() - ts;
92
+ if (diff < 0) return "just now";
93
+ const s = Math.floor(diff / 1000);
94
+ if (s < 30) return "just now";
95
+ if (s < 60) return s + "s ago";
96
+ const m = Math.floor(s / 60);
97
+ if (m < 60) return m + "m ago";
98
+ const h = Math.floor(m / 60);
99
+ if (h < 24) return h + "h ago";
100
+ const d = Math.floor(h / 24);
101
+ if (d < 7) return d + "d ago";
102
+ const dt = new Date(ts);
103
+ return dt.toLocaleDateString();
104
+ }
105
+
106
+ function shortId(id) {
107
+ if (!id) return "";
108
+ return id.length > 12 ? id.slice(0, 8) : id;
109
+ }
110
+
111
+ function renderRow(session, lastVisited) {
112
+ const a = document.createElement("a");
113
+ a.className = "session-row";
114
+ a.href = "/s/" + encodeURIComponent(session.id);
115
+
116
+ const left = document.createElement("div");
117
+ left.className = "session-left";
118
+
119
+ const head = document.createElement("div");
120
+ head.className = "session-head";
121
+
122
+ const project = document.createElement("span");
123
+ project.className = "session-project";
124
+ const name = session.label || basenameOf(session.cwd) || "Untitled session";
125
+ project.textContent = name;
126
+ project.title = [session.label, session.cwd, session.id]
127
+ .filter(Boolean)
128
+ .join(" · ");
129
+ head.appendChild(project);
130
+
131
+ const idChip = document.createElement("span");
132
+ idChip.className = "session-id";
133
+ idChip.textContent = shortId(session.id);
134
+ head.appendChild(idChip);
135
+
136
+ const visitedAt = lastVisited[session.id] || 0;
137
+ if (session.lastActivity > visitedAt && session.pushCount > 0) {
138
+ const dot = document.createElement("span");
139
+ dot.className = "session-unread";
140
+ dot.title = "Unread pushes";
141
+ head.appendChild(dot);
142
+ }
143
+ left.appendChild(head);
144
+
145
+ const lastPush = document.createElement("p");
146
+ lastPush.className = "session-last-push";
147
+ if (session.lastPushTitle) {
148
+ lastPush.textContent = session.lastPushTitle;
149
+ } else {
150
+ const span = document.createElement("span");
151
+ span.className = "none";
152
+ span.textContent = "no pushes yet";
153
+ lastPush.appendChild(span);
154
+ }
155
+ left.appendChild(lastPush);
156
+
157
+ const meta = document.createElement("div");
158
+ meta.className = "session-meta";
159
+ const parts = [];
160
+ if (session.cwd) parts.push(session.cwd);
161
+ if (session.lastPushKind) parts.push(session.lastPushKind);
162
+ meta.textContent = parts.join(" · ");
163
+ if (parts.length === 0) meta.hidden = true;
164
+ left.appendChild(meta);
165
+
166
+ a.appendChild(left);
167
+
168
+ const right = document.createElement("div");
169
+ right.className = "session-right";
170
+
171
+ const count = document.createElement("div");
172
+ count.className = "session-pushcount";
173
+ count.textContent =
174
+ session.pushCount === 1 ? "1 push" : session.pushCount + " pushes";
175
+ right.appendChild(count);
176
+
177
+ const when = document.createElement("div");
178
+ when.className = "session-when";
179
+ when.textContent = relTime(session.lastActivity);
180
+ right.appendChild(when);
181
+
182
+ // Hover-revealed delete — placed inside the right column so it sits
183
+ // above the count text in the reserved 22px slot.
184
+ const del = document.createElement("button");
185
+ del.className = "session-del";
186
+ del.type = "button";
187
+ del.title = "Delete this session";
188
+ del.setAttribute("aria-label", "Delete session");
189
+ del.innerHTML =
190
+ '<svg viewBox="0 0 24 24" width="15" height="15" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/></svg>';
191
+ del.addEventListener("click", async (e) => {
192
+ e.preventDefault();
193
+ e.stopPropagation();
194
+ if (!window.confirm(`Delete session "${name}" and all its pushes?`)) return;
195
+ await fetch("/api/sessions/" + encodeURIComponent(session.id), {
196
+ method: "DELETE",
197
+ });
198
+ load();
199
+ });
200
+ right.appendChild(del);
201
+
202
+ a.appendChild(right);
203
+
204
+ return a;
205
+ }
206
+
207
+ async function load() {
208
+ let sessions = [];
209
+ try {
210
+ const r = await fetch("/api/sessions");
211
+ const data = await r.json();
212
+ sessions = data.sessions || [];
213
+ } catch (err) {
214
+ console.error("[easel] failed to load sessions", err);
215
+ }
216
+
217
+ const lastVisited = readLastVisited();
218
+ listEl.innerHTML = "";
219
+ if (sessions.length === 0) {
220
+ emptyEl.hidden = false;
221
+ countEl.textContent = "— sessions";
222
+ return;
223
+ }
224
+ emptyEl.hidden = true;
225
+ countEl.textContent =
226
+ sessions.length === 1 ? "1 session" : sessions.length + " sessions";
227
+
228
+ for (const s of sessions) {
229
+ const row = renderRow(s, lastVisited);
230
+ listEl.appendChild(row);
231
+ }
232
+ }
233
+
234
+ load();
235
+ setInterval(load, 4000);
236
+ })();