Keep settings with the account, not the browser

Every setting lived in localStorage, so none of them travelled between
devices. The sharpest edge is the default identity: with none set the
address that sorts first wins, so mail goes out from an address the
recipient may not recognise -- and someone who sets it at work finds it
unset at home, with nothing to say so. Reported in #54.

They now live in a settings.json in the account's own JMAP Files, beside
the signature images already kept there. ihasmail itself stays stateless:
no volume, no database, nothing to back up separately, and the settings
are covered by whatever backs up the mail store.

localStorage stays as a cache rather than the source of truth, so the
first frame is painted from it and the file corrects it a moment later.
A private window has no cache and shows defaults for that one frame,
which is the trade for not gating the whole app on a network round trip.

Not everything should follow the account. A list-pane width picked on a
27" monitor is wrong on a laptop, and the notification toggles track a
permission the browser grants per-device, so claiming it elsewhere would
be a lie. Those stay local, written as a list of exceptions so that a
setting added later syncs by default -- which is what adding one almost
always means.

Writes are coalesced: update() fires on every frame of a splitter drag,
so a change waits 3s and the newest value wins. A tab going away flushes
first, as does signing out, so a setting changed seconds before either
is not lost.

The ihasmail folder is now hidden from the Files view, contents and all.
Hiding the folder alone would have been worse than showing it: the tree
attaches a node whose parent is missing to the root, so the signature
images would have spilled into the top level as if the user had put them
there. Those images have been visible since signatures shipped.

Requires 0.16 -- FileNode/query cannot see directories before that. On
0.15 settings stay local exactly as they were.

Verified against the mock end to end: folder create, blob upload, node
create, read back, update, re-read. Not yet exercised against the live
0.16.19.
This commit is contained in:
2026-08-26 08:19:57 -07:00
parent f9f442072b
commit 0a9218f622
10 changed files with 520 additions and 55 deletions
+36 -5
View File
@@ -1,6 +1,7 @@
import { create } from "zustand";
import { CAP, JmapMethodError, client, setErrorMessage } from "@/jmap/client";
import { directoryCreate, fileCreate, fileNodeProps, normalizeFileNodes, queryOmitsDirectories } from "@/lib/filenode";
import { isAppFolder } from "@/lib/appFolder";
import type { FileNode, GetResponse, Id, QueryResponse, SetResponse } from "@/jmap/types";
import { useSession } from "./session";
@@ -30,6 +31,31 @@ let filtersSupported = true;
const byName = (a: FileNode, b: FileNode) => (a.nodeType === b.nodeType ? a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: "base" }) : a.nodeType === "directory" ? -1 : 1);
/**
* Drop the client's own `ihasmail` folder, and everything inside it, from a
* listing. It holds signature images and the synced settings file — real nodes
* in the account, but housekeeping rather than anything the user filed.
*
* The contents have to go too: the tree attaches a node whose parent is missing
* to the root, so hiding the folder alone would spill its files into the top
* level, which is worse than showing the folder.
*/
export function withoutAppFolder(nodes: FileNode[]): FileNode[] {
const hidden = new Set<Id>();
for (const n of nodes) if (isAppFolder(n)) hidden.add(n.id);
if (!hidden.size) return nodes;
for (let grew = true; grew; ) {
grew = false;
for (const n of nodes) {
if (!hidden.has(n.id) && n.parentId && hidden.has(n.parentId)) {
hidden.add(n.id);
grew = true;
}
}
}
return nodes.filter((n) => !hidden.has(n.id));
}
/** Fetch all nodes (paged, no filter) and rebuild the full children map. */
async function loadAllNodes(accountId: Id, set: (fn: (s: FilesState) => Partial<FilesState>) => void): Promise<void> {
const all: FileNode[] = [];
@@ -52,14 +78,17 @@ async function loadAllNodes(accountId: Id, set: (fn: (s: FilesState) => Partial<
if (!q.ids.length || (q.total != null && position >= q.total)) break;
}
}
// After the whole collection, not per page: the folder and its contents can
// land in different pages, and a half-filtered pass would spill the rest.
const visible = withoutAppFolder(all);
const nodes: Record<Id, FileNode> = {};
const children: Record<string, Id[]> = { root: [] };
for (const n of all) nodes[n.id] = n;
for (const n of all.sort(byName)) {
for (const n of visible) nodes[n.id] = n;
for (const n of visible.sort(byName)) {
const key = n.parentId && nodes[n.parentId] ? n.parentId : "root";
(children[key] ??= []).push(n.id);
}
for (const n of all) children[n.id] ??= [];
for (const n of visible) children[n.id] ??= [];
set(() => ({ nodes, children, loading: false, error: null }));
}
@@ -95,10 +124,12 @@ export const useFiles = create<FilesState>((set, get) => ({
]);
const q = res.get("q")?.[0] as unknown as QueryResponse;
const g = res.get("g")?.[0] as unknown as GetResponse<FileNode>;
const listed = withoutAppFolder(normalizeFileNodes(g.list));
const keep = new Set(listed.map((n) => n.id));
set((s) => {
const nodes = { ...s.nodes };
for (const n of normalizeFileNodes(g.list)) nodes[n.id] = n;
return { nodes, children: { ...s.children, [parentId ?? "root"]: q.ids }, loading: false, error: null };
for (const n of listed) nodes[n.id] = n;
return { nodes, children: { ...s.children, [parentId ?? "root"]: q.ids.filter((id) => keep.has(id)) }, loading: false, error: null };
});
} catch (err) {
// Older Stalwart releases don't support parentId / isTopLevel filters: fall back to