inboxer 0.1.0 → 0.3.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.
package/src/ui/views.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { html, raw } from "hono/html";
2
2
  import type { HtmlEscapedString } from "hono/utils/html";
3
+ import { bareAddress } from "../core/address.ts";
3
4
  import type { MessageView } from "../core/mail.ts";
4
5
  import type { Contact, Context, Message, Session, Thread } from "../types.ts";
5
6
  import { ASSETS } from "./assets.ts";
@@ -7,7 +8,7 @@ import { S } from "./strings.ts";
7
8
 
8
9
  export type View = HtmlEscapedString;
9
10
  export type Theme = "system" | "light" | "dark";
10
- export type Page = "drafts" | "inbox" | "devices" | null;
11
+ export type Page = "drafts" | "inbox" | "devices" | "settings" | null;
11
12
 
12
13
  /** Views never await, so `html` returns a plain escaped string. */
13
14
  function h(strings: TemplateStringsArray, ...values: unknown[]): View {
@@ -22,46 +23,153 @@ function when(condition: unknown, view: () => View): View | "" {
22
23
  return condition ? view() : "";
23
24
  }
24
25
 
25
- function formatDate(iso: string): string {
26
+ const ICONS = {
27
+ back: '<path d="M15 18l-6-6 6-6"/>',
28
+ archive: '<rect x="3" y="4" width="18" height="5" rx="1"/><path d="M5 9v10a1 1 0 001 1h12a1 1 0 001-1V9"/><path d="M10 13h4"/>',
29
+ unarchive: '<rect x="3" y="4" width="18" height="5" rx="1"/><path d="M5 9v10a1 1 0 001 1h12a1 1 0 001-1V9"/><path d="M12 17v-5M9.5 14.5L12 12l2.5 2.5"/>',
30
+ drafts: '<path d="M4 20h4L19 9l-4-4L4 16z"/><path d="M13 7l4 4"/>',
31
+ inbox: '<path d="M3 13l3-8h12l3 8v6a1 1 0 01-1 1H4a1 1 0 01-1-1z"/><path d="M3 13h5l1 3h6l1-3h5"/>',
32
+ plus: '<path d="M12 5v14M5 12h14"/>',
33
+ devices: '<rect x="6" y="2" width="12" height="20" rx="2"/><path d="M11 18h2"/>',
34
+ settings: '<circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M4.2 4.2l2.1 2.1M17.7 17.7l2.1 2.1M2 12h3M19 12h3M4.2 19.8l2.1-2.1M17.7 6.3l2.1-2.1"/>',
35
+ send: '<path d="M5 12h14"/><path d="M13 6l6 6-6 6"/>',
36
+ close: '<path d="M6 6l12 12M18 6L6 18"/>',
37
+ } as const;
38
+
39
+ function icon(name: keyof typeof ICONS): View {
40
+ return raw(`<svg class="icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`) as View;
41
+ }
42
+
43
+ const sameDay = (a: Date, b: Date) => a.toDateString() === b.toDateString();
44
+
45
+ function formatTime(iso: string): string {
46
+ return new Date(iso).toLocaleTimeString("en", { hour: "2-digit", minute: "2-digit", hourCycle: "h23" });
47
+ }
48
+
49
+ function formatDay(iso: string): string {
26
50
  const date = new Date(iso);
27
- const sameDay = date.toDateString() === new Date().toDateString();
28
- return sameDay
29
- ? date.toLocaleTimeString("en", { hour: "2-digit", minute: "2-digit" })
51
+ const now = new Date();
52
+ return date.getFullYear() === now.getFullYear()
53
+ ? date.toLocaleDateString("en", { month: "short", day: "numeric" })
30
54
  : date.toLocaleDateString("en", { month: "short", day: "numeric", year: "numeric" });
31
55
  }
32
56
 
33
- export function layout(title: string, body: View, opts: { theme: Theme; page: Page; draftCount?: number; signedIn: boolean }): View {
34
- const nav = (page: Page, href: string, label: string, extra: View | "" = "") =>
35
- h`<a href="${href}" ${raw(opts.page === page ? 'aria-current="page"' : "")}>${label}${extra}</a>`;
57
+ /** A time today, a day otherwise. */
58
+ function formatDate(iso: string): string {
59
+ return sameDay(new Date(iso), new Date()) ? formatTime(iso) : formatDay(iso);
60
+ }
61
+
62
+ function dayLabel(iso: string): string {
63
+ const date = new Date(iso);
64
+ const yesterday = new Date(Date.now() - 86_400_000);
65
+ if (sameDay(date, new Date())) return S.today;
66
+ if (sameDay(date, yesterday)) return S.yesterday;
67
+ return formatDay(iso);
68
+ }
69
+
70
+ /** Display name from `Name <a@b.com>`, else the bare address. */
71
+ function displayName(address: string): string {
72
+ const match = /^\s*"?([^"<]+?)"?\s*</.exec(address);
73
+ return match ? match[1]! : bareAddress(address);
74
+ }
75
+
76
+ function initials(address: string): string {
77
+ const name = displayName(address);
78
+ const words = (name.includes("@") ? name.split("@")[0]! : name).split(/[\s._-]+/).filter(Boolean);
79
+ return (words.length > 1 ? words[0]![0]! + words[1]![0]! : (words[0] ?? "?").slice(0, 2)).toUpperCase();
80
+ }
81
+
82
+ export interface Mailbox {
83
+ id: string;
84
+ address: string;
85
+ }
86
+
87
+ export interface LayoutOptions {
88
+ theme: Theme;
89
+ page: Page;
90
+ signedIn: boolean;
91
+ draftCount?: number;
92
+ domains?: string[];
93
+ /** Mailboxes the session can send from; New mail shows only when there is one. */
94
+ senders?: Mailbox[];
95
+ /** Thread pages fill the screen on a phone, so the tab bar gives way to the reply box. */
96
+ fullScreen?: boolean;
97
+ }
98
+
99
+ export function layout(title: string, body: View, opts: LayoutOptions): View {
100
+ const link = (page: Page, href: string, label: string, name: keyof typeof ICONS, extra: View | "" = "") =>
101
+ h`<a href="${href}" ${raw(opts.page === page ? 'aria-current="page"' : "")}>${icon(name)}<span>${label}</span>${extra}</a>`;
102
+ const count = opts.draftCount ? h`<span class="count">${opts.draftCount}</span>` : "";
103
+ const canSend = !!opts.senders?.length;
36
104
  return h`<!doctype html>
37
105
  <html lang="en" ${raw(opts.theme === "system" ? "" : `data-theme="${opts.theme}"`)}>
38
106
  <head>
39
107
  <meta charset="utf-8">
40
- <meta name="viewport" content="width=device-width, initial-scale=1">
108
+ <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
41
109
  <title>${title} · ${S.appName}</title>
42
110
  <link rel="stylesheet" href="${ASSETS["style.css"]!.url}">
43
111
  <script src="${ASSETS["htmx.min.js"]!.url}" defer></script>
112
+ <script src="${ASSETS["app.js"]!.url}" defer></script>
44
113
  </head>
45
- <body>
46
- ${when(opts.signedIn, () => h`<header>
47
- <nav>
48
- ${nav("drafts", "/", S.navDrafts, opts.draftCount ? h` <span class="count">${opts.draftCount}</span>` : "")}
49
- ${nav("inbox", "/inbox", S.navInbox)}
50
- ${nav("devices", "/devices", S.navDevices)}
114
+ <body class="${opts.signedIn ? "app" : ""} ${opts.fullScreen ? "full" : ""}">
115
+ ${when(opts.signedIn, () => h`<aside class="sidebar">
116
+ <div class="brand">${S.appName}<span>.</span></div>
117
+ ${when(opts.domains?.length, () => h`<div class="domain">${opts.domains!.join(" · ")}</div>`)}
118
+ ${when(canSend, () => h`<button type="button" class="primary new-mail" data-compose>${icon("plus")}${S.newMail}</button>`)}
119
+ <nav class="side-nav" aria-label="${S.mainNav}">
120
+ ${link("drafts", "/", S.navDrafts, "drafts", count)}
121
+ ${link("inbox", "/inbox", S.navInbox, "inbox")}
122
+ ${link("devices", "/devices", S.navDevices, "devices")}
51
123
  </nav>
52
- <form method="post" action="/theme">
53
- <span class="select"><select name="theme" aria-label="${S.theme}" onchange="this.form.submit()">
54
- ${join((["system", "light", "dark"] as const).map(t =>
55
- h`<option value="${t}" ${raw(t === opts.theme ? "selected" : "")}>${{ system: S.themeSystem, light: S.themeLight, dark: S.themeDark }[t]}</option>`))}
56
- </select></span>
57
- </form>
58
- <form method="post" action="/logout"><button class="link">${S.logout}</button></form>
59
- </header>`)}
124
+ <nav class="side-nav side-end" aria-label="${S.navSettings}">${link("settings", "/settings", S.navSettings, "settings")}</nav>
125
+ </aside>`)}
60
126
  <main>${body}</main>
127
+ ${when(opts.signedIn, () => h`<nav class="tabbar" aria-label="${S.mainNav}">
128
+ ${link("drafts", "/", S.navDrafts, "drafts", count)}
129
+ ${link("inbox", "/inbox", S.navInbox, "inbox")}
130
+ ${when(canSend, () => h`<button type="button" class="tab-compose" data-compose aria-label="${S.newMail}"><span>${icon("plus")}</span></button>`)}
131
+ ${link("devices", "/devices", S.navDevices, "devices")}
132
+ ${link("settings", "/settings", S.navSettings, "settings")}
133
+ </nav>`)}
134
+ ${when(opts.signedIn && canSend, () => composeDialog(opts.senders!))}
61
135
  </body>
62
136
  </html>`;
63
137
  }
64
138
 
139
+ /** New mail, opened from the sidebar or the tab bar; a bottom sheet on a phone. */
140
+ function composeDialog(senders: Mailbox[]): View {
141
+ return h`<dialog class="sheet" id="compose" aria-label="${S.newMail}">
142
+ <form method="post" action="/compose">
143
+ <div class="grabber" aria-hidden="true"></div>
144
+ <button type="button" class="icon-btn sheet-close" data-close aria-label="${S.close}">${icon("close")}</button>
145
+ <div class="field compact">
146
+ <label for="compose-from">${S.from}</label>
147
+ ${senders.length > 1
148
+ ? h`<span class="select"><select id="compose-from" name="mailbox">${join(senders.map(m => h`<option value="${m.id}">${m.address}</option>`))}</select></span>`
149
+ : h`<input type="hidden" name="mailbox" value="${senders[0]!.id}"><output id="compose-from">${senders[0]!.address}</output>`}
150
+ </div>
151
+ <div class="field">
152
+ <label for="compose-to">${S.to}</label>
153
+ <input id="compose-to" name="to" autocomplete="off" required>
154
+ <button type="button" class="link" onclick="this.remove();var r=document.getElementById('compose-cc-row');r.hidden=false;r.querySelector('input').focus()">${S.cc}</button>
155
+ </div>
156
+ <div class="field" id="compose-cc-row" hidden>
157
+ <label for="compose-cc">${S.cc}</label>
158
+ <input id="compose-cc" name="cc" autocomplete="off">
159
+ </div>
160
+ <div class="field">
161
+ <label for="compose-subject">${S.subject}</label>
162
+ <input id="compose-subject" name="subject" required>
163
+ </div>
164
+ <textarea name="text" aria-label="${S.body}" required></textarea>
165
+ <div class="sheet-actions">
166
+ <button type="submit" name="action" value="save">${S.saveDraft}</button>
167
+ <button type="submit" name="action" value="send" class="primary">${S.send}${icon("send")}</button>
168
+ </div>
169
+ </form>
170
+ </dialog>`;
171
+ }
172
+
65
173
  export function message(lines: string[]): View {
66
174
  return h`<div class="centered">${join(lines.map(l => h`<p>${l}</p>`))}</div>`;
67
175
  }
@@ -75,9 +183,50 @@ export function signIn(token: string, valid: boolean): View {
75
183
  }
76
184
 
77
185
  function mailboxBadge(mailbox: string): View {
78
- return h`<span class="badge">${mailbox}</span>`;
186
+ return h`<span class="badge mailbox">${mailbox}</span>`;
187
+ }
188
+
189
+ const PREVIEWABLE = new Set(["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif"]);
190
+
191
+ /** Image types a browser renders without running code; SVG is left out on purpose. */
192
+ export function isPreviewable(contentType: string): boolean {
193
+ return PREVIEWABLE.has(contentType);
79
194
  }
80
195
 
196
+ function formatSize(bytes: number | null): string {
197
+ if (bytes === null) return "";
198
+ if (bytes < 1024) return `${bytes} B`;
199
+ if (bytes < 1048576) return `${Math.round(bytes / 1024)} KB`;
200
+ return `${(bytes / 1048576).toFixed(1)} MB`;
201
+ }
202
+
203
+ /** Images first as thumbnails, inline ones included; the rest as file links. */
204
+ function attachments(m: Pick<MessageView, "id" | "attachments">): View | "" {
205
+ if (!m.attachments.length) return "";
206
+ const href = (id: string) => `/attachments/${encodeURIComponent(m.id)}/${encodeURIComponent(id)}`;
207
+ const images = m.attachments.filter(a => isPreviewable(a.content_type));
208
+ const files = m.attachments.filter(a => !isPreviewable(a.content_type));
209
+ return h`${when(images.length, () => h`<div class="images">${join(images.map(a =>
210
+ h`<a href="${href(a.id)}" target="_blank" rel="noopener"><img src="${href(a.id)}" alt="${a.filename}" loading="lazy"></a>`))}</div>`)}
211
+ ${when(files.length, () => h`<div class="files">${join(files.map(a =>
212
+ h`<a class="file" href="${href(a.id)}" target="_blank" rel="noopener"><span>${a.filename}</span><span class="size">${formatSize(a.size)}</span></a>`))}</div>`)}`;
213
+ }
214
+
215
+ /** Mail clients write "[image: name.png]" in the text part where an image sat; the image shows below instead. */
216
+ function withoutImagePlaceholders(m: MessageView): string {
217
+ let text = m.text;
218
+ for (const a of m.attachments) {
219
+ if (isPreviewable(a.content_type)) text = text.split(`[image: ${a.filename}]`).join("");
220
+ }
221
+ return text.replace(/\n{3,}/g, "\n\n").trim();
222
+ }
223
+
224
+ function recipients(m: MessageView): View | "" {
225
+ if (!m.to.length && !m.cc.length) return "";
226
+ return h`<div class="meta">${when(m.to.length, () => h`<span>${S.to}: ${m.to.join(", ")}</span>`)}${when(m.cc.length, () => h`<span>${S.cc}: ${m.cc.join(", ")}</span>`)}</div>`;
227
+ }
228
+
229
+ /** A message in a draft's history on the Drafts page. */
81
230
  export function messageCard(m: MessageView): View {
82
231
  return h`<article class="card ${m.direction}">
83
232
  <div class="meta">
@@ -85,8 +234,22 @@ export function messageCard(m: MessageView): View {
85
234
  <span>${formatDate(m.date)}</span>
86
235
  ${when(m.direction === "out" && m.status !== "draft", () => h`<span>${m.status}</span>`)}
87
236
  </div>
88
- <div class="meta"><span>${S.to}: ${m.to.join(", ")}</span>${when(m.cc.length, () => h`<span>${S.cc}: ${m.cc.join(", ")}</span>`)}</div>
89
- <pre class="body">${m.text}</pre>
237
+ ${recipients(m)}
238
+ <pre class="body">${withoutImagePlaceholders(m)}</pre>
239
+ ${attachments(m)}
240
+ </article>`;
241
+ }
242
+
243
+ /** A message in a thread: received ones on the left, ours on the right. */
244
+ export function messageBubble(m: MessageView, showSender: boolean): View {
245
+ const mine = m.direction === "out";
246
+ const extra = m.cc.length || m.to.length > (mine ? 1 : 0);
247
+ return h`<article class="msg ${mine ? "mine" : "theirs"}" id="m-${m.id}">
248
+ ${when(showSender && !mine, () => h`<div class="msg-from">${displayName(m.from)}</div>`)}
249
+ ${extra ? recipients(m) : ""}
250
+ <pre class="body">${withoutImagePlaceholders(m)}</pre>
251
+ ${attachments(m)}
252
+ <span class="msg-time">${formatTime(m.date)}${mine ? ` · ${m.status}` : ""}</span>
90
253
  </article>`;
91
254
  }
92
255
 
@@ -95,38 +258,62 @@ export interface ContextView {
95
258
  contacts: Contact[];
96
259
  }
97
260
 
98
- /** Read-only context agents attached to the thread and its participants. */
99
- function contextBlock(ctx: ContextView | null): View | "" {
100
- if (!ctx) return "";
101
- const rows = [
102
- ...ctx.contacts.map(c => ({ who: c.address, meta: c.meta, note: c.note })),
261
+ function contextRows(ctx: ContextView): { who: string; meta: Context["meta"]; note: string | null }[] {
262
+ return [
103
263
  ...(ctx.context ? [{ who: S.thisThread, meta: ctx.context.meta, note: ctx.context.note }] : []),
264
+ ...ctx.contacts.map(c => ({ who: c.address, meta: c.meta, note: c.note })),
104
265
  ].filter(r => r.note || Object.keys(r.meta).length);
105
- if (!rows.length) return "";
106
- return h`<details class="context"><summary>${S.context}</summary>${join(rows.map(r => h`<section>
266
+ }
267
+
268
+ function contextSections(rows: ReturnType<typeof contextRows>): View {
269
+ return join(rows.map(r => h`<section>
107
270
  <div class="who">${r.who}</div>
108
271
  ${when(Object.keys(r.meta).length, () => h`<dl>${join(Object.entries(r.meta).map(([k, v]) => h`<dt>${k}</dt><dd>${String(v)}</dd>`))}</dl>`)}
109
272
  ${when(r.note, () => h`<p>${r.note}</p>`)}
110
- </section>`))}</details>`;
273
+ </section>`));
274
+ }
275
+
276
+ /** Read-only context agents attached to the thread and its participants, folded on a draft card. */
277
+ function contextBlock(ctx: ContextView | null): View | "" {
278
+ if (!ctx) return "";
279
+ const rows = contextRows(ctx);
280
+ if (!rows.length) return "";
281
+ return h`<details class="context"><summary>${S.context}</summary>${contextSections(rows)}</details>`;
111
282
  }
112
283
 
284
+ /** A send or discard in its undo window; reloads itself when the window closes. */
285
+ function pendingCard(d: Message): View {
286
+ const left = Math.max(0, Date.parse(d.pending!.at) - Date.now());
287
+ return h`<article class="card pending" id="draft-${d.id}" hx-get="/drafts/${d.id}/card" hx-trigger="load delay:${left + 400}ms" hx-swap="outerHTML">
288
+ <div class="pending-row">
289
+ <span>${d.pending!.action === "send" ? h`${S.sending} <strong>${d.to.join(", ")}</strong>` : S.discarding}</span>
290
+ <button hx-post="/drafts/${d.id}/undo" hx-target="#draft-${d.id}" hx-swap="outerHTML">${S.undo}</button>
291
+ </div>
292
+ <i class="countdown" style="animation-duration: ${left}ms"></i>
293
+ </article>`;
294
+ }
295
+
296
+ /** A draft for review: a card on the Drafts page, a bubble on our side of a thread. */
113
297
  export function draftCard(d: Message, history: MessageView[], ctx: ContextView | null): View {
114
- return h`<article class="card" id="draft-${d.id}">
298
+ if (d.pending) return pendingCard(d);
299
+ return h`<article class="card draft" id="draft-${d.id}">
115
300
  <div class="meta">
116
- ${mailboxBadge(d.mailbox)}
117
301
  <span class="badge draft">${S.draft}</span>
118
- ${when(d.edited_by_human, () => h`<span>${S.editedByHuman}</span>`)}
119
- <span>${formatDate(d.created_at)}</span>
302
+ ${mailboxBadge(d.mailbox)}
303
+ <span>${d.edited_by_human ? S.editedByHuman : S.byAgent} · ${formatDate(d.created_at)}</span>
304
+ </div>
305
+ <div class="draft-head">
306
+ <div class="meta"><span>${S.to}: <strong>${d.to.join(", ")}</strong></span>${when(d.cc.length, () => h`<span>${S.cc}: ${d.cc.join(", ")}</span>`)}</div>
307
+ <div class="subject">${d.subject}</div>
120
308
  </div>
121
- <div class="meta"><span>${S.to}: <strong>${d.to.join(", ")}</strong></span>${when(d.cc.length, () => h`<span>${S.cc}: ${d.cc.join(", ")}</span>`)}</div>
122
- <div class="subject">${d.subject}</div>
123
309
  ${contextBlock(ctx)}
124
310
  ${when(d.note, () => h`<div class="note">${d.note}</div>`)}
125
311
  <pre class="body">${d.text}</pre>
312
+ ${attachments(d)}
126
313
  <div class="actions">
314
+ <button class="danger" hx-post="/drafts/${d.id}/discard" hx-target="#draft-${d.id}" hx-swap="outerHTML">${S.discard}</button>
127
315
  <button hx-get="/drafts/${d.id}/edit" hx-target="#draft-${d.id}" hx-swap="outerHTML">${S.edit}</button>
128
- <button class="primary" hx-post="/drafts/${d.id}/send" hx-target="#draft-${d.id}" hx-swap="outerHTML" hx-confirm="${S.confirmSend}">${S.send}</button>
129
- <button class="danger" hx-post="/drafts/${d.id}/discard" hx-target="#draft-${d.id}" hx-swap="outerHTML" hx-confirm="${S.confirmDiscard}">${S.discard}</button>
316
+ <button class="primary" hx-post="/drafts/${d.id}/send" hx-target="#draft-${d.id}" hx-swap="outerHTML">${S.send}</button>
130
317
  </div>
131
318
  ${when(history.length, () => h`<details>
132
319
  <summary>${S.thread} (${history.length})</summary>
@@ -144,8 +331,9 @@ export interface DraftForm {
144
331
 
145
332
  export function draftEditor(d: Message, form: DraftForm, conflict: Message | null): View {
146
333
  const version = conflict ? conflict.version : d.version;
147
- return h`<article class="card ${conflict ? "conflict" : ""}" id="draft-${d.id}">
148
- <div class="meta">${mailboxBadge(d.mailbox)}<span class="badge draft">${S.draft}</span></div>
334
+ return h`<article class="card draft editing ${conflict ? "conflict" : ""}" id="draft-${d.id}" data-drop>
335
+ <div class="drop-hint">${S.dropFiles}</div>
336
+ <div class="meta"><span class="badge draft">${S.draft}</span>${mailboxBadge(d.mailbox)}</div>
149
337
  ${when(d.note, () => h`<div class="note">${d.note}</div>`)}
150
338
  ${when(conflict, () => h`<p class="warn">${S.conflictTitle}</p><div class="subject">${S.yourVersion}</div>`)}
151
339
  <form hx-post="/drafts/${d.id}/save" hx-target="#draft-${d.id}" hx-swap="outerHTML">
@@ -164,10 +352,11 @@ export function draftEditor(d: Message, form: DraftForm, conflict: Message | nul
164
352
  <input id="subject-${d.id}" name="subject" value="${form.subject}" required>
165
353
  </div>
166
354
  <textarea id="text-${d.id}" name="text" aria-label="${S.body}" required>${form.text}</textarea>
355
+ ${draftFiles(d, null)}
167
356
  <div class="actions">
168
- <button type="submit">${S.save}</button>
169
- <button type="submit" name="send" value="1" class="primary" hx-confirm="${S.confirmSend}">${S.saveAndSend}</button>
170
357
  <button type="button" class="link" hx-get="/drafts/${d.id}/card" hx-target="#draft-${d.id}" hx-swap="outerHTML">${S.cancel}</button>
358
+ <button type="submit">${S.save}</button>
359
+ <button type="submit" name="send" value="1" class="primary">${S.saveAndSend}</button>
171
360
  </div>
172
361
  </form>
173
362
  ${when(conflict, () => h`<details open>
@@ -181,61 +370,131 @@ export function draftEditor(d: Message, form: DraftForm, conflict: Message | nul
181
370
  </article>`;
182
371
  }
183
372
 
373
+ /** The editor's attachment list with remove buttons, and the picker that drag and drop also feeds. */
374
+ export function draftFiles(d: Message, error: string | null): View {
375
+ return h`<div class="draft-files" id="files-${d.id}">
376
+ ${join(d.attachments.map(a => h`<span class="file"><span>${a.filename}</span><span class="size">${formatSize(a.size)}</span><button type="button" class="link" aria-label="${S.remove} ${a.filename}" hx-post="/drafts/${d.id}/attachments/${a.id}/remove" hx-target="#files-${d.id}" hx-swap="outerHTML">×</button></span>`))}
377
+ <label class="file attach">${S.attach}<input type="file" name="files" multiple hidden hx-post="/drafts/${d.id}/attachments" hx-encoding="multipart/form-data" hx-trigger="change" hx-target="#files-${d.id}" hx-swap="outerHTML" hx-include="this"></label>
378
+ ${when(error, () => h`<p class="warn">${error}</p>`)}
379
+ </div>`;
380
+ }
381
+
184
382
  export function draftDone(id: string, text: string): View {
185
383
  return h`<article class="card notice" id="draft-${id}">${text}</article>`;
186
384
  }
187
385
 
188
386
  export function draftsPage(drafts: { draft: Message; history: MessageView[]; ctx: ContextView }[]): View {
189
- return h`<h1>${S.navDrafts}</h1>
190
- ${drafts.length ? join(drafts.map(d => draftCard(d.draft, d.history, d.ctx))) : h`<p class="empty">${S.draftsEmpty}</p>`}`;
191
- }
192
-
193
- export function inboxPage(threads: Thread[], mailboxes: string[], filter: { mailbox: string; status: string }, own: Set<string>): View {
194
- const tab = (status: string, label: string) => {
195
- const query = new URLSearchParams({ status, ...(filter.mailbox ? { mailbox: filter.mailbox } : {}) });
196
- return h`<a href="/inbox?${query.toString()}" ${raw(filter.status === status ? 'aria-current="page"' : "")}>${label}</a>`;
197
- };
198
- return h`<h1>${S.navInbox}</h1>
199
- <form class="filters" method="get" action="/inbox">
200
- <input type="hidden" name="status" value="${filter.status}">
201
- <span class="select"><select name="mailbox" aria-label="${S.filter}" onchange="this.form.submit()">
202
- <option value="">${S.allMailboxes}</option>
203
- ${join(mailboxes.map(m => h`<option value="${m}" ${raw(m === filter.mailbox ? "selected" : "")}>${m}</option>`))}
204
- </select></span>
205
- <nav class="tabs">${tab("unanswered", S.filterUnanswered)}${tab("all", S.filterAll)}${tab("archived", S.filterArchived)}</nav>
206
- </form>
207
- ${threads.length ? join(threads.map(t => threadRow(t, own))) : h`<p class="empty">${S.inboxEmpty}</p>`}`;
387
+ return h`<div class="page">
388
+ <h1>${S.navDrafts}</h1>
389
+ ${drafts.length ? join(drafts.map(d => draftCard(d.draft, d.history, d.ctx))) : h`<p class="empty">${S.draftsEmpty}</p>`}
390
+ </div>`;
208
391
  }
209
392
 
210
- function threadRow(t: Thread, own: Set<string>): View {
211
- return h`<a class="card thread-row" href="/threads/${t.id}">
212
- <div class="meta">
213
- ${mailboxBadge(t.mailbox)}
214
- ${when(t.unread, () => h`<span class="dot" title="${S.unread}"></span>`)}
215
- ${when(t.has_draft, () => h`<span class="badge draft">${S.hasDraft}</span>`)}
216
- <span>${formatDate(t.last_at)}</span>
217
- </div>
218
- <div class="subject">${t.subject || "—"}</div>
219
- <div class="meta"><span>${t.participants.filter(p => !own.has(p)).join(", ")}</span></div>
393
+ export interface InboxFilter {
394
+ mailbox: string;
395
+ status: string;
396
+ }
397
+
398
+ export function inboxQuery(filter: InboxFilter): string {
399
+ return new URLSearchParams({ status: filter.status, ...(filter.mailbox ? { mailbox: filter.mailbox } : {}) }).toString();
400
+ }
401
+
402
+ /** The thread list; on a wide screen it stays beside the open thread. */
403
+ export function threadList(threads: Thread[], opts: { mailboxes: string[]; domains: string[]; filter: InboxFilter; own: Set<string>; current: string | null }): View {
404
+ const { filter } = opts;
405
+ const tab = (status: string, label: string) =>
406
+ h`<a class="chip" href="/inbox?${inboxQuery({ ...filter, status })}" ${raw(filter.status === status ? 'aria-current="page"' : "")}>${label}</a>`;
407
+ return h`<section class="list">
408
+ <header class="list-head">
409
+ <h1>${opts.domains.length ? opts.domains.join(" · ") : S.navInbox}</h1>
410
+ <form class="filters" method="get" action="/inbox">
411
+ <input type="hidden" name="status" value="${filter.status}">
412
+ <nav class="chips" aria-label="${S.filter}">${tab("unanswered", S.filterPending)}${tab("all", S.filterAll)}${tab("archived", S.filterArchived)}</nav>
413
+ ${when(opts.mailboxes.length > 1, () => h`<span class="select"><select name="mailbox" aria-label="${S.allMailboxes}" onchange="this.form.submit()">
414
+ <option value="">${S.allMailboxes}</option>
415
+ ${join(opts.mailboxes.map(m => h`<option value="${m}" ${raw(m === filter.mailbox ? "selected" : "")}>${m}</option>`))}
416
+ </select></span>`)}
417
+ </form>
418
+ </header>
419
+ ${threads.length ? join(threads.map(t => threadRow(t, opts))) : h`<p class="empty">${S.inboxEmpty}</p>`}
420
+ </section>`;
421
+ }
422
+
423
+ function threadRow(t: Thread, opts: { mailboxes: string[]; filter: InboxFilter; own: Set<string>; current: string | null }): View {
424
+ const people = t.participants.filter(p => !opts.own.has(p));
425
+ return h`<a class="row ${t.unread ? "unread" : ""}" href="/threads/${t.id}?${inboxQuery(opts.filter)}" ${raw(t.id === opts.current ? 'aria-current="page"' : "")}>
426
+ <span class="avatar" aria-hidden="true">${initials(people[0] ?? t.mailbox)}</span>
427
+ <span class="row-main">
428
+ <span class="row-top"><span class="who">${people.join(", ") || t.mailbox}</span><time datetime="${t.last_at}">${formatDate(t.last_at)}</time></span>
429
+ <span class="row-subject">${t.subject || "—"}</span>
430
+ ${when(t.has_draft || opts.mailboxes.length > 1 || t.unread, () => h`<span class="row-tags">
431
+ ${when(t.has_draft, () => h`<span class="badge draft">${S.hasDraft}</span>`)}
432
+ ${when(opts.mailboxes.length > 1, () => mailboxBadge(t.mailbox))}
433
+ ${when(t.unread, () => h`<span class="dot" title="${S.unread}"></span>`)}
434
+ </span>`)}
435
+ </span>
220
436
  </a>`;
221
437
  }
222
438
 
223
- export function threadPage(thread: Thread, items: View[], ctx: ContextView): View {
224
- return h`<h1>${thread.subject || "—"}</h1>
225
- <div class="meta">${mailboxBadge(thread.mailbox)}${archiveButton(thread)}</div>
226
- ${contextBlock(ctx)}
227
- <section class="messages">${join(items)}</section>`;
439
+ /** The thread list and the open thread side by side; a phone shows one of the two. */
440
+ export function inboxPage(list: View, thread: View | null): View {
441
+ return h`<div class="split ${thread ? "has-thread" : ""}">${list}<section class="pane">${thread ?? ""}</section></div>`;
442
+ }
443
+
444
+ export interface ThreadEntry {
445
+ at: string;
446
+ view: View;
447
+ }
448
+
449
+ /** The conversation, oldest first, with day separators, a reply box and the context panel. */
450
+ export function threadView(thread: Thread, entries: ThreadEntry[], ctx: ContextView, opts: { filter: InboxFilter; own: Set<string>; reply: { allowed: boolean; replyAll: boolean } }): View {
451
+ const query = inboxQuery(opts.filter);
452
+ const rows = contextRows(ctx);
453
+ const people = thread.participants.filter(p => !opts.own.has(p));
454
+ const title = h`<span class="thread-subject">${thread.subject || "—"}</span><span class="thread-meta">${[...people, thread.mailbox].join(" · ")}</span>`;
455
+ const items: View[] = [];
456
+ let day = "";
457
+ for (const entry of entries) {
458
+ const label = dayLabel(entry.at);
459
+ if (label !== day) items.push(h`<div class="day">${label}</div>`);
460
+ day = label;
461
+ items.push(entry.view);
462
+ }
463
+ return h`<div class="thread">
464
+ <header class="thread-head">
465
+ <a class="icon-btn back" href="/inbox?${query}" aria-label="${S.back}">${icon("back")}</a>
466
+ ${rows.length
467
+ ? h`<button type="button" class="thread-title" data-context-toggle aria-controls="context">${title}</button>`
468
+ : h`<div class="thread-title">${title}</div>`}
469
+ ${when(rows.length, () => h`<button type="button" class="context-btn" data-context-toggle aria-controls="context">${S.context}</button>`)}
470
+ ${archiveButton(thread)}
471
+ </header>
472
+ <div class="messages" data-scroll-end>${join(items)}</div>
473
+ ${when(opts.reply.allowed, () => h`<form class="composer" method="post" action="/threads/${thread.id}/reply?${query}">
474
+ ${when(opts.reply.replyAll, () => h`<label class="reply-all"><input type="checkbox" name="reply_all" value="1">${S.replyAll}</label>`)}
475
+ <textarea name="text" rows="1" aria-label="${S.reply}" placeholder="${S.reply}…" required></textarea>
476
+ <button type="submit" name="action" value="save" class="icon-btn" aria-label="${S.saveDraft}" title="${S.saveDraft}">${icon("drafts")}</button>
477
+ <button type="submit" name="action" value="send" class="primary send" aria-label="${S.send}" title="${S.send}">${icon("send")}</button>
478
+ </form>`)}
479
+ ${when(rows.length, () => h`<aside class="context-panel" id="context" aria-label="${S.context}">
480
+ <div class="context-head"><h2>${S.context}</h2><button type="button" class="icon-btn" data-context-toggle aria-label="${S.close}">${icon("close")}</button></div>
481
+ <div class="context-body">${contextSections(rows)}</div>
482
+ </aside>
483
+ <div class="context-backdrop" data-context-toggle></div>`)}
484
+ </div>`;
228
485
  }
229
486
 
230
487
  export function archiveButton(thread: Thread): View {
488
+ const label = thread.archived ? S.unarchive : S.archive;
231
489
  return h`<form id="archive" hx-post="/threads/${thread.id}/archive" hx-swap="outerHTML">
232
490
  <input type="hidden" name="archived" value="${thread.archived ? "0" : "1"}">
233
- <button class="link">${thread.archived ? S.unarchive : S.archive}</button>
491
+ <button class="icon-btn" aria-label="${label}" title="${label}">${icon(thread.archived ? "unarchive" : "archive")}</button>
234
492
  </form>`;
235
493
  }
236
494
 
237
495
  export function devicesPage(sessions: Session[], currentId: string): View {
238
- return h`<h1>${S.navDevices}</h1>
496
+ return h`<div class="page">
497
+ <h1>${S.navDevices}</h1>
239
498
  ${sessions.length ? join(sessions.map(s => h`<article class="card" id="session-${s.id}">
240
499
  <div class="meta">
241
500
  <strong>${s.label ?? s.user_agent ?? S.unknownDevice}</strong>
@@ -248,6 +507,21 @@ ${sessions.length ? join(sessions.map(s => h`<article class="card" id="session-$
248
507
  <span>${S.lastSeen}: ${formatDate(s.last_seen)}</span>
249
508
  <span>${S.createdBy}: ${s.created_by}</span>
250
509
  </div>
251
- ${when(s.id !== currentId, () => h`<div class="actions"><button class="danger" hx-post="/devices/${s.id}/revoke" hx-target="#session-${s.id}" hx-swap="delete" hx-confirm="${S.confirmRevoke}">${S.revoke}</button></div>`)}
252
- </article>`)) : h`<p class="empty">${S.devicesEmpty}</p>`}`;
510
+ ${when(s.id !== currentId, () => h`<div class="actions"><button class="danger" hx-post="/devices/${s.id}/revoke" hx-target="#session-${s.id}" hx-swap="delete">${S.revoke}</button></div>`)}
511
+ </article>`)) : h`<p class="empty">${S.devicesEmpty}</p>`}
512
+ </div>`;
513
+ }
514
+
515
+ export function settingsPage(theme: Theme): View {
516
+ return h`<div class="page">
517
+ <h1>${S.navSettings}</h1>
518
+ <form class="setting" method="post" action="/theme">
519
+ <label for="theme">${S.theme}</label>
520
+ <span class="select"><select id="theme" name="theme" onchange="this.form.submit()">
521
+ ${join((["system", "light", "dark"] as const).map(t =>
522
+ h`<option value="${t}" ${raw(t === theme ? "selected" : "")}>${{ system: S.themeSystem, light: S.themeLight, dark: S.themeDark }[t]}</option>`))}
523
+ </select></span>
524
+ </form>
525
+ <form class="setting" method="post" action="/logout"><button class="danger">${S.logout}</button></form>
526
+ </div>`;
253
527
  }