CI / build (push) Successful in 11s
Each account gets an Edit button opening the add form pre-filled with
its settings; the password field is optional ("unchanged") and only a
new value replaces the stored one. Editing no longer rewrites the hosts
or login from the address, so a working setup isn't silently changed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
421 lines
12 KiB
Svelte
421 lines
12 KiB
Svelte
<script lang="ts">
|
||
// The mail accounts Tern syncs, with how each one's sync is going, and
|
||
// the form to connect another. Only the server ever talks to the mail
|
||
// server (server-architecture-spec.md §2); this screen just hands it the
|
||
// settings and shows what it reports back.
|
||
import { createAccount, deleteAccount, listAccounts, updateAccount, type Account } from './api';
|
||
import { formatFullDate } from './format';
|
||
|
||
let {
|
||
reloadKey = 0,
|
||
onChanged,
|
||
}: {
|
||
/** Bumped by the parent when accounts change elsewhere (another tab). */
|
||
reloadKey?: number;
|
||
/** Tells the parent the account list changed (sidebar, compose). */
|
||
onChanged?: () => void;
|
||
} = $props();
|
||
|
||
/** Sync status isn't pushed as an event, so an open screen polls it. */
|
||
const STATUS_POLL_MS = 5_000;
|
||
|
||
let accounts = $state<Account[]>([]);
|
||
let loaded = $state(false);
|
||
let error = $state<string | null>(null);
|
||
let adding = $state(false);
|
||
/** The account being edited; the form is shared with adding. */
|
||
let editing = $state<Account | null>(null);
|
||
let saving = $state(false);
|
||
let formError = $state<string | null>(null);
|
||
|
||
// Form fields. Login and hosts follow the address until edited by hand.
|
||
let email = $state('');
|
||
let displayName = $state('');
|
||
let imapHost = $state('');
|
||
let imapPort = $state(993);
|
||
let smtpHost = $state('');
|
||
let smtpPort = $state(587);
|
||
let login = $state('');
|
||
let password = $state('');
|
||
let loginEdited = $state(false);
|
||
let hostsEdited = $state(false);
|
||
|
||
async function load() {
|
||
try {
|
||
accounts = await listAccounts();
|
||
error = null;
|
||
} catch (e) {
|
||
error = e instanceof Error ? e.message : 'failed to load accounts';
|
||
} finally {
|
||
loaded = true;
|
||
}
|
||
}
|
||
|
||
// Loads on mount and whenever the parent bumps reloadKey; polls while open.
|
||
$effect(() => {
|
||
void reloadKey;
|
||
load();
|
||
});
|
||
$effect(() => {
|
||
const timer = setInterval(load, STATUS_POLL_MS);
|
||
return () => clearInterval(timer);
|
||
});
|
||
|
||
// With no account yet, the form is the whole point of the screen.
|
||
$effect(() => {
|
||
if (loaded && accounts.length === 0) adding = true;
|
||
});
|
||
|
||
function emailChanged() {
|
||
const domain = email.includes('@') ? email.split('@')[1] : '';
|
||
if (!loginEdited) login = email;
|
||
if (!hostsEdited) {
|
||
imapHost = domain ? `mail.${domain}` : '';
|
||
smtpHost = imapHost;
|
||
}
|
||
}
|
||
|
||
function resetForm() {
|
||
email = displayName = imapHost = smtpHost = login = password = '';
|
||
imapPort = 993;
|
||
smtpPort = 587;
|
||
loginEdited = hostsEdited = false;
|
||
formError = null;
|
||
}
|
||
|
||
function startEdit(a: Account) {
|
||
adding = false;
|
||
editing = a;
|
||
email = a.email_address ?? '';
|
||
displayName = a.display_name ?? '';
|
||
imapHost = a.imap_host;
|
||
imapPort = a.imap_port;
|
||
smtpHost = a.smtp_host;
|
||
smtpPort = a.smtp_port;
|
||
login = a.username;
|
||
password = '';
|
||
// Editing the address must not silently rewrite settings that work.
|
||
loginEdited = hostsEdited = true;
|
||
formError = null;
|
||
}
|
||
|
||
function closeForm() {
|
||
adding = false;
|
||
editing = null;
|
||
resetForm();
|
||
}
|
||
|
||
async function save(event: SubmitEvent) {
|
||
event.preventDefault();
|
||
saving = true;
|
||
formError = null;
|
||
const settings = {
|
||
name: editing?.name ?? (email.split('@')[1] || email),
|
||
imap_host: imapHost.trim(),
|
||
imap_port: Number(imapPort),
|
||
smtp_host: smtpHost.trim(),
|
||
smtp_port: Number(smtpPort),
|
||
username: login.trim(),
|
||
password,
|
||
email_address: email.trim() || null,
|
||
display_name: displayName.trim() || null,
|
||
};
|
||
try {
|
||
if (editing) await updateAccount(editing.id, settings);
|
||
else await createAccount(settings);
|
||
closeForm();
|
||
await load();
|
||
onChanged?.();
|
||
} catch (e) {
|
||
formError = e instanceof Error ? e.message : 'failed to save the account';
|
||
} finally {
|
||
saving = false;
|
||
}
|
||
}
|
||
|
||
async function remove(a: Account) {
|
||
const what = a.email_address ?? a.username;
|
||
const count = a.sync?.messages ?? 0;
|
||
if (
|
||
!confirm(
|
||
`Remove ${what}?\n\nTern stops syncing it and deletes the ${count} message${count === 1 ? '' : 's'} it synced from it, ` +
|
||
`including anything you've tagged or filed. Mail on your mail server isn't touched — adding the account again re-syncs the inbox.`,
|
||
)
|
||
)
|
||
return;
|
||
try {
|
||
await deleteAccount(a.id);
|
||
await load();
|
||
onChanged?.();
|
||
} catch (e) {
|
||
error = e instanceof Error ? e.message : 'failed to remove the account';
|
||
}
|
||
}
|
||
|
||
function statusText(a: Account): string {
|
||
const s = a.sync;
|
||
if (!s || s.status === 'pending') return 'Waiting to connect…';
|
||
switch (s.status) {
|
||
case 'syncing':
|
||
return s.caught_up ? 'Connecting…' : 'First sync in progress…';
|
||
case 'idle':
|
||
return 'Connected — new mail arrives as it lands';
|
||
case 'error':
|
||
return 'Can’t sync — retrying';
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<div class="accounts">
|
||
<header>
|
||
<h2>Accounts</h2>
|
||
{#if !adding && !editing}
|
||
<button class="primary" onclick={() => { resetForm(); adding = true; }}>Add account</button>
|
||
{/if}
|
||
</header>
|
||
<p class="hint">
|
||
The mailboxes Tern syncs. Your Tern server connects to each one itself — IMAP over TLS on port 993 to
|
||
read, SMTP on 587 (STARTTLS) or 465 to send — so the mail server has to be reachable from it.
|
||
</p>
|
||
{#if error}<p class="error">{error}</p>{/if}
|
||
|
||
{#if accounts.length > 0}
|
||
<ul>
|
||
{#each accounts as a (a.id)}
|
||
<li>
|
||
<span class="dot state-{a.sync?.status ?? 'pending'}" aria-hidden="true"></span>
|
||
<div class="body">
|
||
<strong>{a.display_name ? `${a.display_name} <${a.email_address ?? a.username}>` : (a.email_address ?? a.username)}</strong>
|
||
<span class="detail">IMAP {a.imap_host}:{a.imap_port} · SMTP {a.smtp_host}:{a.smtp_port} · login {a.username}</span>
|
||
<span class="status state-{a.sync?.status ?? 'pending'}">
|
||
{statusText(a)}{#if a.sync}{` · ${a.sync.messages} message${a.sync.messages === 1 ? '' : 's'}`}{/if}
|
||
</span>
|
||
{#if a.sync?.status === 'error' && a.sync.last_error}
|
||
<span class="last-error">{a.sync.last_error}</span>
|
||
{/if}
|
||
{#if a.sync}<span class="detail">Last update {formatFullDate(a.sync.updated_at)}</span>{/if}
|
||
</div>
|
||
<div class="row-actions">
|
||
<button onclick={() => startEdit(a)} disabled={editing?.id === a.id}>Edit</button>
|
||
<button onclick={() => remove(a)}>Remove</button>
|
||
</div>
|
||
</li>
|
||
{/each}
|
||
</ul>
|
||
{/if}
|
||
|
||
{#if adding || editing}
|
||
<form onsubmit={save}>
|
||
<h3>
|
||
{#if editing}Edit {editing.email_address ?? editing.username}
|
||
{:else if accounts.length === 0}Connect your first mailbox
|
||
{:else}Add an account{/if}
|
||
</h3>
|
||
<label class="wide">Email address
|
||
<input type="email" bind:value={email} oninput={emailChanged} required autocomplete="email" />
|
||
</label>
|
||
<label class="wide">Your name <span class="optional">(optional, shown on mail you send)</span>
|
||
<input bind:value={displayName} autocomplete="name" />
|
||
</label>
|
||
<label>IMAP server
|
||
<input bind:value={imapHost} oninput={() => (hostsEdited = true)} required />
|
||
</label>
|
||
<label class="port">Port
|
||
<input type="number" min="1" max="65535" bind:value={imapPort} required />
|
||
</label>
|
||
<label>SMTP server
|
||
<input bind:value={smtpHost} oninput={() => (hostsEdited = true)} required />
|
||
</label>
|
||
<label class="port">Port
|
||
<input type="number" min="1" max="65535" bind:value={smtpPort} required />
|
||
</label>
|
||
<label class="wide">Login
|
||
<input bind:value={login} oninput={() => (loginEdited = true)} required autocomplete="username" />
|
||
</label>
|
||
<label class="wide">Password
|
||
<input
|
||
type="password"
|
||
bind:value={password}
|
||
required={!editing}
|
||
placeholder={editing ? 'unchanged — type a new one to replace it' : ''}
|
||
autocomplete={editing ? 'new-password' : 'current-password'}
|
||
/>
|
||
</label>
|
||
{#if editing}
|
||
<p class="hint wide">
|
||
Saving reconnects with the new settings. Mail already here stays as long as it's the same mailbox;
|
||
point it at a different one and it's synced afresh.
|
||
</p>
|
||
{/if}
|
||
<p class="hint wide">
|
||
Stored encrypted on your Tern server, never in this browser. Tern only reads your inbox and sends —
|
||
it never changes anything on the mail server.
|
||
</p>
|
||
{#if formError}<p class="error wide">{formError}</p>{/if}
|
||
<div class="actions wide">
|
||
{#if accounts.length > 0}
|
||
<button type="button" onclick={closeForm}>Cancel</button>
|
||
{/if}
|
||
<button type="submit" class="primary" disabled={saving}>
|
||
{saving ? 'Saving…' : editing ? 'Save changes' : 'Add account'}
|
||
</button>
|
||
</div>
|
||
</form>
|
||
{/if}
|
||
</div>
|
||
|
||
<style>
|
||
.accounts {
|
||
overflow-y: auto;
|
||
height: 100%;
|
||
}
|
||
header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
padding: 1.25rem 1.25rem 0;
|
||
}
|
||
h2 {
|
||
margin: 0;
|
||
font-size: 1.1rem;
|
||
}
|
||
h3 {
|
||
grid-column: 1 / -1;
|
||
margin: 0 0 0.25rem;
|
||
font-size: 0.95rem;
|
||
}
|
||
.hint,
|
||
.error {
|
||
padding: 0 1.25rem;
|
||
font-size: 0.82rem;
|
||
color: var(--primary);
|
||
max-width: 48rem;
|
||
}
|
||
.error {
|
||
color: var(--accent);
|
||
}
|
||
ul {
|
||
list-style: none;
|
||
margin: 0;
|
||
padding: 0 1.25rem 1rem;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 0.4rem;
|
||
}
|
||
li {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
gap: 0.75rem;
|
||
padding: 0.7rem 0.75rem;
|
||
border: 1px solid var(--secondary);
|
||
border-radius: 0.45rem;
|
||
}
|
||
.row-actions {
|
||
display: flex;
|
||
gap: 0.4rem;
|
||
}
|
||
.body {
|
||
flex: 1;
|
||
min-width: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 0.15rem;
|
||
}
|
||
.detail {
|
||
font-size: 0.78rem;
|
||
color: var(--primary);
|
||
overflow-wrap: anywhere;
|
||
}
|
||
.status {
|
||
font-size: 0.82rem;
|
||
}
|
||
.status.state-error {
|
||
color: var(--accent);
|
||
}
|
||
.last-error {
|
||
font-family: ui-monospace, monospace;
|
||
font-size: 0.75rem;
|
||
color: var(--accent);
|
||
overflow-wrap: anywhere;
|
||
}
|
||
.dot {
|
||
flex-shrink: 0;
|
||
width: 0.6rem;
|
||
height: 0.6rem;
|
||
margin-top: 0.35rem;
|
||
border-radius: 50%;
|
||
border: 1px solid var(--primary);
|
||
}
|
||
.dot.state-idle {
|
||
background: var(--primary);
|
||
}
|
||
.dot.state-syncing {
|
||
border-style: dashed;
|
||
}
|
||
.dot.state-error {
|
||
background: var(--accent);
|
||
border-color: var(--accent);
|
||
}
|
||
form {
|
||
display: grid;
|
||
grid-template-columns: minmax(0, 1fr) 6rem;
|
||
gap: 0.6rem 0.75rem;
|
||
margin: 0 1.25rem 1.25rem;
|
||
padding: 1rem;
|
||
max-width: 34rem;
|
||
border: 1px solid var(--secondary);
|
||
border-radius: 0.45rem;
|
||
}
|
||
form .hint,
|
||
form .error {
|
||
padding: 0;
|
||
margin: 0;
|
||
}
|
||
.wide {
|
||
grid-column: 1 / -1;
|
||
}
|
||
label {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 0.25rem;
|
||
font-size: 0.8rem;
|
||
color: var(--primary);
|
||
}
|
||
.optional {
|
||
font-size: 0.72rem;
|
||
}
|
||
input {
|
||
font: inherit;
|
||
font-size: 0.88rem;
|
||
color: var(--text);
|
||
background: var(--background);
|
||
border: 1px solid var(--secondary);
|
||
border-radius: 0.3rem;
|
||
padding: 0.35rem 0.5rem;
|
||
}
|
||
.actions {
|
||
display: flex;
|
||
justify-content: flex-end;
|
||
gap: 0.4rem;
|
||
}
|
||
button {
|
||
font: inherit;
|
||
font-size: 0.82rem;
|
||
color: var(--text);
|
||
background: none;
|
||
border: 1px solid var(--secondary);
|
||
border-radius: 0.3rem;
|
||
padding: 0.25rem 0.6rem;
|
||
cursor: pointer;
|
||
}
|
||
button:disabled {
|
||
opacity: 0.4;
|
||
cursor: default;
|
||
}
|
||
.primary {
|
||
background: var(--accent);
|
||
border-color: var(--accent);
|
||
font-weight: 600;
|
||
}
|
||
</style>
|