@mccaskie-labs/workbench-settings 0.0.0-stage → 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,90 @@
1
+ // The Settings page's samples (docs/look-studio.md §5.1): every part it draws,
2
+ // with made-up settings, for the project's style guide, the look previewer
3
+ // and the visual check. Drawn by the page's own drawer (drawSettingsPage), so
4
+ // a sample can't drift from the real page.
5
+ import { designSettings } from "./design.js";
6
+ import { EMAIL_SETTINGS } from "./email.js";
7
+ import { drawSettingsPage } from "./page.js";
8
+ import { SETTINGS_STYLES } from "./styles.js";
9
+ const [replyTo, fromName] = EMAIL_SETTINGS.settings;
10
+ /** A made-up tool with a choice, so the sample shows a select. */
11
+ const CLASSES = {
12
+ format: 1,
13
+ tool: "classes",
14
+ title: { en: "Classes" },
15
+ about: { en: "How long people can book ahead." },
16
+ settings: [
17
+ {
18
+ key: "book_ahead",
19
+ type: "choice",
20
+ kind: "live",
21
+ label: { en: "Book ahead" },
22
+ help: { en: "How far ahead the timetable opens." },
23
+ choices: [
24
+ { value: "2w", label: { en: "Two weeks" } },
25
+ { value: "4w", label: { en: "Four weeks" } },
26
+ ],
27
+ default: "2w",
28
+ },
29
+ ],
30
+ };
31
+ const bookAhead = CLASSES.settings[0];
32
+ const DESIGN = designSettings({
33
+ looks: [{ id: "clean", title: "Clean" }, { id: "calm", title: "Calm" }],
34
+ schemes: [{ id: "light", title: "Light" }, { id: "dark", title: "Dark" }],
35
+ toolLayouts: { bookings: ["list", "cards"], orders: ["grid", "list"] },
36
+ });
37
+ const [look, scheme, layouts] = DESIGN.settings;
38
+ const designed = [
39
+ { tool: "design", key: "look", def: look, value: "calm", from: "owner", changedBy: "maria@saltbush.example", changedAt: "2026-11-24T08:30:00.000Z", carry: "test_only" },
40
+ { tool: "design", key: "scheme", def: scheme, value: null, from: "default", carry: "carried" },
41
+ { tool: "design", key: "layouts", def: layouts, value: { bookings: "cards", "orders@/shop": "list" }, from: "owner", changedBy: "maria@saltbush.example", changedAt: "2026-11-24T08:31:00.000Z", carry: "test_only" },
42
+ ];
43
+ const page = { endpoint: "/admin/settings", timeZone: "Australia/Sydney" };
44
+ const waiting = [
45
+ { tool: "email", key: "reply_to", def: replyTo, value: "hello@saltbush.example", from: "answers", pending: { value: "bookings@saltbush.example", expiresAt: "2026-11-25T09:00:00.000Z" } },
46
+ { tool: "email", key: "from_name", def: fromName, value: "Saltbush Yoga Studio", from: "owner", changedBy: "maria@saltbush.example", changedAt: "2026-11-24T08:30:00.000Z" },
47
+ ];
48
+ const changes = [
49
+ { tool: "email", key: "from_name", action: "save", before: "Saltbush Yoga", after: "Saltbush Yoga Studio", by: "maria@saltbush.example", at: "2026-11-24T08:30:00.000Z" },
50
+ { tool: "email", key: "from_name", action: "put_back", before: "Saltbush", after: null, by: "sam@saltbush.example", at: "2026-11-20T02:15:00.000Z" },
51
+ ];
52
+ const problems = [
53
+ { tool: "email", key: "reply_to", def: replyTo, value: "hello@saltbush.example", from: "answers" },
54
+ { tool: "email", key: "from_name", def: fromName, value: null, from: "default" },
55
+ { tool: "classes", key: "book_ahead", def: bookAhead, value: "2w", from: "default" },
56
+ ];
57
+ /** Every sample of the Settings page. */
58
+ export const SETTINGS_SAMPLES = {
59
+ format: 1,
60
+ package: SETTINGS_STYLES.package,
61
+ title: "Settings",
62
+ prefix: SETTINGS_STYLES.prefix,
63
+ samples: [
64
+ {
65
+ id: "page",
66
+ title: "Settings, with a new address waiting for its code",
67
+ html: () => drawSettingsPage([EMAIL_SETTINGS], waiting, {
68
+ ...page,
69
+ outcome: { status: 422, tool: "email", key: "reply_to", problem: { word: "code.wrong", values: { left: "4" } } },
70
+ history: changes,
71
+ }),
72
+ },
73
+ {
74
+ id: "saved",
75
+ title: "Just saved, nothing changed yet",
76
+ html: () => drawSettingsPage([EMAIL_SETTINGS, CLASSES], problems, { ...page, outcome: { status: 200, tool: "classes", key: "book_ahead", notice: { word: "saved" } }, history: [] }),
77
+ },
78
+ {
79
+ id: "problems",
80
+ title: "Something to fix, and the store not answering",
81
+ html: () => drawSettingsPage([EMAIL_SETTINGS], problems, { ...page, outcome: { status: 422, tool: "email", key: "reply_to", problem: { word: "problem.email" }, typed: "hello@saltbush" } }) +
82
+ drawSettingsPage([CLASSES], problems, { ...page, outcome: { status: 503, problem: { word: "problem.failed" } } }),
83
+ },
84
+ {
85
+ id: "design",
86
+ title: "Design mode's choices, not yet on the live site",
87
+ html: () => drawSettingsPage([DESIGN], designed, page),
88
+ },
89
+ ],
90
+ };
@@ -0,0 +1,136 @@
1
+ import type { CarriedSetting, SettingChange, SettingsLang, SettingState, SettingValue, ToolSettings } from "./types.ts";
2
+ import { type SettingsStrings } from "./words.ts";
3
+ type DbError = {
4
+ message?: string;
5
+ code?: string;
6
+ details?: string;
7
+ } | null;
8
+ /** The part of a Supabase client settings need. Any supabase-js client fits; use the service client. */
9
+ export interface SettingsClient {
10
+ rpc(fn: string, args: Record<string, unknown>): PromiseLike<{
11
+ data: unknown;
12
+ error: DbError;
13
+ }>;
14
+ }
15
+ /** The part of workbench-email's createMailer(...) settings use, for the code email. */
16
+ export interface SettingsMailer {
17
+ send(mail: {
18
+ to: string | string[];
19
+ subject: string;
20
+ text: string;
21
+ template: string;
22
+ idempotencyKey?: string;
23
+ }): Promise<unknown>;
24
+ }
25
+ export interface SettingsOptions {
26
+ /** The service client. */
27
+ db: SettingsClient;
28
+ /** Every tool's declarations: EMAIL_SETTINGS and the others'. Checked here. */
29
+ tools: readonly ToolSettings[];
30
+ /**
31
+ * The project's answers, by tool: the values from workbench/<tool>.json
32
+ * that a declared setting's `answers` key reads, such as
33
+ * `{ email: { reply_to: "hello@harbour.example" } }`.
34
+ */
35
+ answers?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
36
+ /** createMailer(...), for a checked setting's code. Without it a checked setting can't be changed. */
37
+ mailer?: SettingsMailer;
38
+ /** The app's name, for the code email: "Harbour Physio". */
39
+ appName: string;
40
+ /**
41
+ * How long a read is kept before asking the database again. Default 60
42
+ * seconds: another isolate can use the old value for up to this long after
43
+ * a change.
44
+ */
45
+ cacheMs?: number;
46
+ now?: () => Date;
47
+ /** Six random digits. Only tests pass this. */
48
+ code?: () => string;
49
+ }
50
+ /** What a save did. */
51
+ export type SaveResult = {
52
+ status: "saved";
53
+ value: SettingValue | null;
54
+ } | {
55
+ status: "pending";
56
+ value: SettingValue;
57
+ expiresAt: string;
58
+ } | {
59
+ status: "unchanged";
60
+ };
61
+ /** What a code did. */
62
+ export type ConfirmResult = {
63
+ status: "saved";
64
+ value: SettingValue;
65
+ } | {
66
+ status: "wrong";
67
+ left: number;
68
+ } | {
69
+ status: "expired" | "locked" | "none";
70
+ };
71
+ export interface Settings {
72
+ readonly tools: readonly ToolSettings[];
73
+ /** The value in force: the owner's, else the project's answer, else the tool's default; null when none. */
74
+ get(tool: string, key: string): Promise<SettingValue | null>;
75
+ /** Every declared setting with its value, where it came from and any waiting change, in declaration order. */
76
+ states(): Promise<SettingState[]>;
77
+ /**
78
+ * Saves a value the owner typed. A checked setting emails a code to the new
79
+ * value and waits for confirm. Throws SettingsError bad_value, mailer_needed,
80
+ * too_soon (a code went less than a minute ago), too_many (five in a day) or
81
+ * send_failed (nothing is left waiting).
82
+ */
83
+ save(tool: string, key: string, raw: unknown, by: string, opts?: {
84
+ lang?: SettingsLang;
85
+ t?: SettingsStrings;
86
+ }): Promise<SaveResult>;
87
+ /** Uses a checked setting's waiting value when the code is right. */
88
+ confirm(tool: string, key: string, code: string, by: string): Promise<ConfirmResult>;
89
+ /** Drops a checked change waiting for its code. */
90
+ cancel(tool: string, key: string): Promise<{
91
+ cancelled: boolean;
92
+ }>;
93
+ /** Deletes the owner's value, so the project's answer is used again. */
94
+ putBack(tool: string, key: string, by: string): Promise<{
95
+ changed: boolean;
96
+ }>;
97
+ /** Recent changes, newest first. */
98
+ history(limit?: number): Promise<SettingChange[]>;
99
+ /** Once a day: drops changes older than 90 days and codes past their time. */
100
+ cleanup(): Promise<{
101
+ changes: number;
102
+ pending: number;
103
+ }>;
104
+ /** Forgets this isolate's cached read. */
105
+ forget(): void;
106
+ /**
107
+ * A carried tool's values on this site, to send to the live one: every
108
+ * declared key, with null where the owner hasn't changed it (so the live
109
+ * site puts it back). Run on the test site.
110
+ */
111
+ exportGroup(tool: string): Promise<CarriedSetting[]>;
112
+ /**
113
+ * Sets a carried tool's values from the test site's exportGroup: saves
114
+ * each changed one and puts back each null, recorded as `by`. Every value
115
+ * is checked first, and nothing is written if one fails. Run on the live
116
+ * site, after the owner approved the release.
117
+ */
118
+ importGroup(tool: string, rows: readonly CarriedSetting[], by: string): Promise<{
119
+ changed: number;
120
+ }>;
121
+ /**
122
+ * Records on the test site what the live site now holds (the rows
123
+ * importGroup took), so the page stops saying "On the test site only".
124
+ */
125
+ markCarried(tool: string, rows: readonly CarriedSetting[], by: string): Promise<void>;
126
+ }
127
+ export declare function createSettings(opts: SettingsOptions): Settings;
128
+ /**
129
+ * Gives a tool's `business` callback the owner's reply-to address: the
130
+ * address on the Settings page, else what `base` returns. Bookings, orders,
131
+ * progress, tracker and agreements take this shape.
132
+ */
133
+ export declare function withReplyTo<B extends object, A extends unknown[]>(settings: Settings, base: (...args: A) => B | Promise<B>): (...args: A) => Promise<B & {
134
+ replyTo?: string;
135
+ }>;
136
+ export {};
@@ -0,0 +1,286 @@
1
+ // createSettings: the values an app's owner set on the Settings page, worked
2
+ // out over the project's answers and each tool's default (docs/project-settings.md §2),
3
+ // saved and put back with a record of who did it, and a checked change proved
4
+ // with an emailed code before it is used (§5).
5
+ import { checkValue, sameValue, settingsProblems } from "./check.js";
6
+ import { SettingsError } from "./errors.js";
7
+ import { phrase, settingsWord } from "./words.js";
8
+ const ACCOUNT = "";
9
+ const id = (tool, key) => `${tool}\u0000${key}`;
10
+ function sixDigits() {
11
+ const a = new Uint32Array(1);
12
+ // Rejection sampling keeps every code equally likely.
13
+ for (;;) {
14
+ crypto.getRandomValues(a);
15
+ if (a[0] < 4_294_000_000)
16
+ return String(a[0] % 1_000_000).padStart(6, "0");
17
+ }
18
+ }
19
+ export function createSettings(opts) {
20
+ const problems = settingsProblems(opts.tools);
21
+ if (problems.length)
22
+ throw new SettingsError("bad_declaration", problems.join("; "));
23
+ if (!opts.appName?.trim())
24
+ throw new SettingsError("bad_declaration", "appName is required, for the code email");
25
+ const now = opts.now ?? (() => new Date());
26
+ const cacheMs = opts.cacheMs ?? 60_000;
27
+ const makeCode = opts.code ?? sixDigits;
28
+ let snap = null;
29
+ // Bumped on every write, so a read that started before it can't put old values back.
30
+ let generation = 0;
31
+ const fresh = () => {
32
+ snap = null;
33
+ generation++;
34
+ };
35
+ /** The admin's name or email as stored: trimmed, 1 to 200 characters. */
36
+ const who = (by) => {
37
+ const b = String(by ?? "").trim();
38
+ if (!b || [...b].length > 200)
39
+ throw new SettingsError("bad_input", "by must be the admin's name or email, 1 to 200 characters");
40
+ return b;
41
+ };
42
+ const defOf = (tool, key) => {
43
+ const def = opts.tools.find((t) => t.tool === tool)?.settings.find((s) => s.key === key);
44
+ if (!def)
45
+ throw new SettingsError("unknown_setting", `No setting ${tool}.${key} is declared`);
46
+ return def;
47
+ };
48
+ async function call(fn, args) {
49
+ const { data, error } = await opts.db.rpc(fn, args);
50
+ if (error)
51
+ throw new SettingsError("store_failed", `${fn} failed: ${error.message ?? error.code ?? "unknown error"}`);
52
+ return data;
53
+ }
54
+ async function load() {
55
+ if (snap && now().getTime() - snap.at < cacheMs)
56
+ return snap;
57
+ const started = generation;
58
+ const data = await call("workbench_settings_read", { p_account: ACCOUNT });
59
+ const read = {
60
+ at: now().getTime(),
61
+ values: new Map(data.values.map((v) => [id(v.tool, v.key), v])),
62
+ pending: new Map(data.pending.map((p) => [id(p.tool, p.key), p])),
63
+ carried: new Map((data.carried ?? []).map((c) => [id(c.tool, c.key), c])),
64
+ };
65
+ if (started === generation)
66
+ snap = read;
67
+ return read;
68
+ }
69
+ /** The project's answer for a declared setting, when it passes the setting's own check. */
70
+ function answerOf(tool, def) {
71
+ if (!def.answers)
72
+ return null;
73
+ const raw = opts.answers?.[tool]?.[def.answers];
74
+ if (raw === undefined || raw === null)
75
+ return null;
76
+ const c = checkValue(def, raw);
77
+ return c.ok ? c.value : null;
78
+ }
79
+ const toolOf = (tool) => opts.tools.find((t) => t.tool === tool);
80
+ function stateOf(tool, def, s) {
81
+ const stored = s.values.get(id(tool, def.key));
82
+ const waiting = s.pending.get(id(tool, def.key));
83
+ const pending = waiting && new Date(waiting.expires_at).getTime() > now().getTime() ? { value: waiting.value, expiresAt: waiting.expires_at } : undefined;
84
+ const extra = { ...(pending ? { pending } : {}) };
85
+ // A stored value that no longer passes its check (a declaration tightened in an update) is ignored.
86
+ const owned = stored && checkValue(def, stored.value).ok ? stored : undefined;
87
+ if (toolOf(tool)?.carries) {
88
+ // What the live site holds is the owner's value as last carried; nothing carried yet means "not changed".
89
+ const carried = s.carried.get(id(tool, def.key))?.value ?? null;
90
+ Object.assign(extra, { carry: sameValue(owned?.value ?? null, carried) ? "carried" : "test_only" });
91
+ }
92
+ if (owned)
93
+ return { tool, key: def.key, def, value: owned.value, from: "owner", changedBy: owned.changed_by, changedAt: owned.changed_at, ...extra };
94
+ const answer = answerOf(tool, def);
95
+ if (answer !== null)
96
+ return { tool, key: def.key, def, value: answer, from: "answers", ...extra };
97
+ return { tool, key: def.key, def, value: def.default ?? null, from: "default", ...extra };
98
+ }
99
+ const carriedTool = (tool) => {
100
+ const t = toolOf(tool);
101
+ if (!t)
102
+ throw new SettingsError("unknown_setting", `No tool ${tool} is declared`);
103
+ if (!t.carries)
104
+ throw new SettingsError("bad_input", `${tool} isn't marked carries`);
105
+ return t;
106
+ };
107
+ return {
108
+ tools: opts.tools,
109
+ async get(tool, key) {
110
+ const def = defOf(tool, key);
111
+ return stateOf(tool, def, await load()).value;
112
+ },
113
+ async states() {
114
+ const s = await load();
115
+ return opts.tools.flatMap((t) => t.settings.map((def) => stateOf(t.tool, def, s)));
116
+ },
117
+ async save(tool, key, raw, by, o = {}) {
118
+ const def = defOf(tool, key);
119
+ const c = checkValue(def, raw);
120
+ if (!c.ok)
121
+ throw new SettingsError("bad_value", `${tool}.${key}: ${c.problem}`, { word: c.problem, ...(c.values ? { values: c.values } : {}), ...(c.words ? { words: c.words } : {}) });
122
+ by = who(by);
123
+ const current = stateOf(tool, def, await load());
124
+ if (sameValue(current.value, c.value)) {
125
+ // Typing the value already in force drops a change waiting for its code.
126
+ if (current.pending)
127
+ await this.cancel(tool, key);
128
+ return { status: "unchanged" };
129
+ }
130
+ if (c.value === null) {
131
+ // An empty field is a put-back for a setting the owner changed; otherwise nothing to do.
132
+ if (current.from !== "owner")
133
+ return { status: "unchanged" };
134
+ await this.putBack(tool, key, by);
135
+ return { status: "saved", value: null };
136
+ }
137
+ if (def.kind === "checked") {
138
+ if (!opts.mailer)
139
+ throw new SettingsError("mailer_needed", `${tool}.${key} is checked by an emailed code, and createSettings was given no mailer`);
140
+ const code = makeCode();
141
+ const r = await call("workbench_settings_request", {
142
+ p_tool: tool,
143
+ p_key: key,
144
+ p_account: ACCOUNT,
145
+ p_value: c.value,
146
+ p_code: code,
147
+ p_by: by,
148
+ p_now: now().toISOString(),
149
+ });
150
+ fresh();
151
+ if (r.status === "too_soon")
152
+ throw new SettingsError("too_soon", `${tool}.${key}: a code was sent less than a minute ago`);
153
+ if (r.status === "too_many")
154
+ throw new SettingsError("too_many", `${tool}.${key}: five codes were sent in the last day`);
155
+ const lang = o.lang ?? "en";
156
+ const values = { app: opts.appName, code, setting: phrase(def.label, lang) };
157
+ try {
158
+ await opts.mailer.send({
159
+ to: String(c.value),
160
+ subject: settingsWord("email.subject", lang, values, o.t),
161
+ text: settingsWord("email.body", lang, values, o.t),
162
+ template: "settings_code",
163
+ });
164
+ }
165
+ catch (e) {
166
+ // Nothing waits for a code that never went.
167
+ await call("workbench_settings_cancel", { p_tool: tool, p_key: key, p_account: ACCOUNT }).catch(() => { });
168
+ fresh();
169
+ throw new SettingsError("send_failed", `${tool}.${key}: the code email couldn't be sent (${e instanceof Error ? e.name : "error"})`);
170
+ }
171
+ return { status: "pending", value: c.value, expiresAt: r.expires_at };
172
+ }
173
+ await call("workbench_settings_save", { p_tool: tool, p_key: key, p_account: ACCOUNT, p_value: c.value, p_by: by, p_now: now().toISOString() });
174
+ fresh();
175
+ return { status: "saved", value: c.value };
176
+ },
177
+ async cancel(tool, key) {
178
+ defOf(tool, key);
179
+ const r = await call("workbench_settings_cancel", { p_tool: tool, p_key: key, p_account: ACCOUNT });
180
+ fresh();
181
+ return r;
182
+ },
183
+ async confirm(tool, key, code, by) {
184
+ defOf(tool, key);
185
+ const r = await call("workbench_settings_confirm", {
186
+ p_tool: tool,
187
+ p_key: key,
188
+ p_account: ACCOUNT,
189
+ p_code: String(code).trim(),
190
+ p_by: who(by),
191
+ p_now: now().toISOString(),
192
+ });
193
+ fresh();
194
+ if (r.status === "saved")
195
+ return { status: "saved", value: r.value };
196
+ if (r.status === "wrong")
197
+ return { status: "wrong", left: r.left ?? 0 };
198
+ return { status: r.status };
199
+ },
200
+ async putBack(tool, key, by) {
201
+ defOf(tool, key);
202
+ const r = await call("workbench_settings_put_back", { p_tool: tool, p_key: key, p_account: ACCOUNT, p_by: who(by), p_now: now().toISOString() });
203
+ fresh();
204
+ return { changed: r.changed };
205
+ },
206
+ async history(limit = 50) {
207
+ const rows = await call("workbench_settings_history", { p_account: ACCOUNT, p_limit: limit });
208
+ return rows.map((r) => ({ tool: r.tool, key: r.key, action: r.action, before: r.before, after: r.after, by: r.changed_by, at: r.changed_at }));
209
+ },
210
+ async cleanup() {
211
+ return call("workbench_settings_cleanup", { p_now: now().toISOString() });
212
+ },
213
+ forget() {
214
+ fresh();
215
+ },
216
+ async exportGroup(tool) {
217
+ const t = carriedTool(tool);
218
+ const s = await load();
219
+ return t.settings.map((def) => {
220
+ const st = stateOf(tool, def, s);
221
+ return { key: def.key, value: st.from === "owner" ? st.value : null };
222
+ });
223
+ },
224
+ async importGroup(tool, rows, by) {
225
+ const t = carriedTool(tool);
226
+ by = who(by);
227
+ const byKey = new Map(rows.map((r) => [r.key, r.value]));
228
+ const checked = t.settings.map((def) => {
229
+ const c = checkValue(def, byKey.get(def.key) ?? null);
230
+ if (!c.ok)
231
+ throw new SettingsError("bad_value", `${tool}.${def.key}: ${c.problem}`, { word: c.problem });
232
+ return { def, value: c.value };
233
+ });
234
+ for (const r of rows)
235
+ if (!t.settings.some((d) => d.key === r.key))
236
+ throw new SettingsError("unknown_setting", `No setting ${tool}.${r.key} is declared`);
237
+ const s = await load();
238
+ let changed = 0;
239
+ for (const { def, value } of checked) {
240
+ const st = stateOf(tool, def, s);
241
+ if (value === null) {
242
+ if (st.from === "owner") {
243
+ await call("workbench_settings_put_back", { p_tool: tool, p_key: def.key, p_account: ACCOUNT, p_by: by, p_now: now().toISOString() });
244
+ changed++;
245
+ }
246
+ }
247
+ else if (!(st.from === "owner" && sameValue(st.value, value))) {
248
+ await call("workbench_settings_save", { p_tool: tool, p_key: def.key, p_account: ACCOUNT, p_value: value, p_by: by, p_now: now().toISOString() });
249
+ changed++;
250
+ }
251
+ }
252
+ fresh();
253
+ return { changed };
254
+ },
255
+ async markCarried(tool, rows, by) {
256
+ const t = carriedTool(tool);
257
+ for (const r of rows) {
258
+ const def = t.settings.find((d) => d.key === r.key);
259
+ if (!def)
260
+ throw new SettingsError("unknown_setting", `No setting ${tool}.${r.key} is declared`);
261
+ if (!checkValue(def, r.value).ok)
262
+ throw new SettingsError("bad_value", `${tool}.${r.key}: doesn't pass its check`);
263
+ }
264
+ await call("workbench_settings_carried", {
265
+ p_tool: tool,
266
+ p_account: ACCOUNT,
267
+ p_rows: rows.map((r) => ({ key: r.key, value: r.value })),
268
+ p_by: who(by),
269
+ p_now: now().toISOString(),
270
+ });
271
+ fresh();
272
+ },
273
+ };
274
+ }
275
+ /**
276
+ * Gives a tool's `business` callback the owner's reply-to address: the
277
+ * address on the Settings page, else what `base` returns. Bookings, orders,
278
+ * progress, tracker and agreements take this shape.
279
+ */
280
+ export function withReplyTo(settings, base) {
281
+ return async (...args) => {
282
+ const b = await base(...args);
283
+ const replyTo = await settings.get("email", "reply_to");
284
+ return typeof replyTo === "string" ? { ...b, replyTo } : b;
285
+ };
286
+ }
@@ -0,0 +1,34 @@
1
+ /** One CSS custom property the rules read, with its default. */
2
+ interface StyleVariable {
3
+ /** `--wb-<tool>-<what>`. */
4
+ name: string;
5
+ value: string;
6
+ about: string;
7
+ }
8
+ /** A rule for a class in another state, or for an element inside it. */
9
+ interface StyleState {
10
+ selector: string;
11
+ about: string;
12
+ css: string;
13
+ }
14
+ /** One class the tool puts on an element, with its default rule. */
15
+ interface StyleClass {
16
+ name: string;
17
+ element: string;
18
+ about: string;
19
+ css: string;
20
+ states?: readonly StyleState[];
21
+ }
22
+ /** Every class and variable the tool draws with. */
23
+ export interface ToolStyles {
24
+ format: 1;
25
+ tool: string;
26
+ package: string;
27
+ title: string;
28
+ prefix: string;
29
+ variables: readonly StyleVariable[];
30
+ classes: readonly StyleClass[];
31
+ }
32
+ /** The style hooks of the Settings page (renderSettingsPage). */
33
+ export declare const SETTINGS_STYLES: ToolStyles;
34
+ export {};
package/dist/styles.js ADDED
@@ -0,0 +1,83 @@
1
+ // The look the Settings page starts with (docs/capabilities-manifest.md §10).
2
+ // Every element renderSettingsPage draws carries one of these classes, and
3
+ // every class has a default rule here, so the page looks finished out of the
4
+ // box and a project restyles it from its own CSS by the same names. `npx
5
+ // workbench-ui styles` writes these into the project's
6
+ // public/styles/workbench.css. The names are public: renaming one is a
7
+ // breaking change.
8
+ //
9
+ // Colours, fonts and corners default to the shared theme tokens
10
+ // (`--wb-accent`, `--wb-text`: workbench-ui's THEME_TOKENS, docs/theming.md),
11
+ // so the project's theme reaches the page; each has a plain fallback for a
12
+ // project whose workbench.css predates the theme layer.
13
+ const v = (name) => `var(--wb-settings-${name})`;
14
+ const focus = { selector: ":focus-visible", about: "Has focus", css: `outline:none;box-shadow:0 0 0 3px ${v("focus")}` };
15
+ const control = `box-sizing:border-box;width:100%;max-width:28rem;min-height:2.75rem;padding:.5rem .75rem;font:inherit;color:${v("text")};background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius-small")}`;
16
+ const button = `display:inline-flex;align-items:center;justify-content:center;min-height:2.75rem;padding:0 1.25rem;font:inherit;font-weight:600;border-radius:${v("radius-small")};cursor:pointer`;
17
+ const invalid = { selector: '[aria-invalid="true"]', about: "What was typed has a problem", css: `border-color:${v("danger")}` };
18
+ /** The style hooks of the Settings page (renderSettingsPage). */
19
+ export const SETTINGS_STYLES = {
20
+ format: 1,
21
+ tool: "settings",
22
+ package: "@mccaskie-labs/workbench-settings",
23
+ title: "Settings",
24
+ prefix: "wb-settings-",
25
+ variables: [
26
+ { name: "--wb-settings-text", value: "var(--wb-text, #1d1d22)", about: "Text" },
27
+ { name: "--wb-settings-muted", value: "var(--wb-muted, #5d5d68)", about: "Quieter text: help, where a value came from, the history" },
28
+ { name: "--wb-settings-surface", value: "var(--wb-surface, #ffffff)", about: "Background of each section and the fields" },
29
+ { name: "--wb-settings-line", value: "var(--wb-line, #dcdce2)", about: "Borders and rules" },
30
+ { name: "--wb-settings-accent", value: "var(--wb-accent, #1b66d6)", about: "The Save button" },
31
+ { name: "--wb-settings-accent-hover", value: "var(--wb-accent-hover, #1554b3)", about: "The Save button under the pointer" },
32
+ { name: "--wb-settings-on-accent", value: "var(--wb-on-accent, #ffffff)", about: "Text on the Save button" },
33
+ { name: "--wb-settings-success", value: "var(--wb-success, #1f7a3d)", about: "The saved notice" },
34
+ { name: "--wb-settings-warning", value: "var(--wb-warning, #8a5a00)", about: "The waiting-for-a-code note" },
35
+ { name: "--wb-settings-danger", value: "var(--wb-danger, #b3261e)", about: "A problem with what was typed" },
36
+ { name: "--wb-settings-focus", value: "var(--wb-focus, rgba(27,102,214,.35))", about: "The ring round a control with focus" },
37
+ { name: "--wb-settings-font", value: "var(--wb-font, system-ui, sans-serif)", about: "The page's text" },
38
+ { name: "--wb-settings-font-heading", value: "var(--wb-font-heading, var(--wb-settings-font))", about: "The headings" },
39
+ { name: "--wb-settings-radius", value: "var(--wb-radius, 8px)", about: "Corner rounding of each section" },
40
+ { name: "--wb-settings-radius-small", value: "var(--wb-radius-small, 6px)", about: "Corner rounding of fields and buttons" },
41
+ { name: "--wb-settings-width", value: "48rem", about: "The page's widest" },
42
+ ],
43
+ classes: [
44
+ { name: "wb-settings", element: "div", about: "The whole page body", css: `display:grid;gap:1.25rem;box-sizing:border-box;max-width:${v("width")};margin:0 auto;color:${v("text")};font-family:${v("font")}` },
45
+ { name: "wb-settings-title", element: "h1", about: "\"Settings\"", css: `margin:0;font-family:${v("font-heading")};font-size:1.75rem;line-height:1.2` },
46
+ { name: "wb-settings-intro", element: "p", about: "The line under the title", css: `margin:0;color:${v("muted")}` },
47
+ { name: "wb-settings-notice", element: "p", about: "What the last save did: \"Saved\"", css: `margin:0;padding:.75rem 1rem;border-left:4px solid ${v("success")};background:${v("surface")}` },
48
+ { name: "wb-settings-problem", element: "p", about: "The settings couldn't be saved", css: `margin:0;padding:.75rem 1rem;border-left:4px solid ${v("danger")};background:${v("surface")}` },
49
+ { name: "wb-settings-section", element: "section", about: "One tool's settings: \"Emails\"", css: `display:grid;gap:1rem;padding:1.25rem;background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")}` },
50
+ { name: "wb-settings-heading", element: "h2", about: "A section's heading", css: `margin:0;font-family:${v("font-heading")};font-size:1.25rem;line-height:1.3` },
51
+ { name: "wb-settings-about", element: "p", about: "The line under a section's heading", css: `margin:0;color:${v("muted")}` },
52
+ { name: "wb-settings-setting", element: "div", about: "One setting, with its code form while one waits", css: `display:grid;gap:.75rem;padding-top:1rem;border-top:1px solid ${v("line")}` },
53
+ { name: "wb-settings-item", element: "form", about: "One setting's form", css: "display:grid;gap:.5rem;margin:0" },
54
+ { name: "wb-settings-label", element: "label", about: "A setting's name", css: "font-weight:600" },
55
+ { name: "wb-settings-help", element: "p", about: "What a setting does", css: `margin:0;color:${v("muted")};font-size:.9375rem` },
56
+ { name: "wb-settings-input", element: "input", about: "A text, email, number or code field", css: control, states: [focus, invalid] },
57
+ { name: "wb-settings-select", element: "select", about: "A choice", css: control, states: [focus, invalid] },
58
+ { name: "wb-settings-code", element: "input", about: "The code field (also wb-settings-input)", css: "max-width:10rem;letter-spacing:.2em;font-variant-numeric:tabular-nums" },
59
+ { name: "wb-settings-field-problem", element: "p", about: "What's wrong with what was typed", css: `margin:0;color:${v("danger")};font-weight:600` },
60
+ { name: "wb-settings-source", element: "p", about: "Where the value came from: \"Set when the app was built\"", css: `margin:0;color:${v("muted")};font-size:.875rem` },
61
+ { name: "wb-settings-carry", element: "p", about: "\"On the test site only\": a design choice the live site hasn't been given yet", css: `margin:0;padding-left:.75rem;border-left:4px solid ${v("warning")};font-size:.875rem` },
62
+ { name: "wb-settings-map", element: "ul", about: "A list of choices made elsewhere (design mode's layouts)", css: "margin:0;padding-left:1.25rem;display:grid;gap:.25rem" },
63
+ { name: "wb-settings-map-item", element: "li", about: "One choice in the list: \"bookings: list\"", css: "overflow-wrap:anywhere" },
64
+ { name: "wb-settings-actions", element: "div", about: "A form's buttons", css: "display:flex;flex-wrap:wrap;gap:.5rem" },
65
+ {
66
+ name: "wb-settings-save",
67
+ element: "button",
68
+ about: "Save, or Use this address",
69
+ css: `${button};color:${v("on-accent")};background:${v("accent")};border:1px solid ${v("accent")}`,
70
+ states: [{ selector: ":hover", about: "Under the pointer", css: `background:${v("accent-hover")};border-color:${v("accent-hover")}` }, focus],
71
+ },
72
+ { name: "wb-settings-put-back", element: "button", about: "Put back to how the app was built", css: `${button};color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
73
+ { name: "wb-settings-cancel", element: "button", about: "Cancel a change waiting for its code", css: `${button};color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
74
+ { name: "wb-settings-pending", element: "form", about: "The code form while a new address waits", css: `display:grid;gap:.5rem;margin:0;padding:1rem;border-left:4px solid ${v("warning")};background:${v("surface")}` },
75
+ { name: "wb-settings-pending-note", element: "p", about: "\"We've emailed a code to …\"", css: "margin:0" },
76
+ { name: "wb-settings-history", element: "section", about: "Recent changes", css: "display:grid;gap:.75rem;overflow-x:auto" },
77
+ { name: "wb-settings-empty", element: "p", about: "\"Nothing has been changed yet\"", css: `margin:0;color:${v("muted")}` },
78
+ { name: "wb-settings-table", element: "table", about: "The changes", css: "width:100%;border-collapse:collapse;font-size:.9375rem" },
79
+ { name: "wb-settings-row", element: "tr", about: "One change", css: "vertical-align:top" },
80
+ { name: "wb-settings-th", element: "th", about: "A column's heading", css: `text-align:left;padding:.5rem .25rem;border-bottom:2px solid ${v("line")};color:${v("muted")};font-weight:600` },
81
+ { name: "wb-settings-td", element: "td", about: "A cell", css: `padding:.5rem .25rem;border-bottom:1px solid ${v("line")};vertical-align:top` },
82
+ ],
83
+ };