@mccaskie-labs/workbench-nudges 0.3.0 → 0.4.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/CHANGELOG.md +8 -0
- package/README.md +2 -2
- package/dist/confirm.d.ts +60 -0
- package/dist/confirm.js +85 -0
- package/dist/errors.d.ts +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/styles.js +5 -1
- package/dist/ui.d.ts +3 -1
- package/dist/ui.js +28 -4
- package/dist/words.d.ts +1 -1
- package/dist/words.js +28 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @mccaskie-labs/workbench-nudges
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Not needed asks "Are you sure?" first. In `renderNudge` and `renderNudgeList` it now only opens a question that names the follow-up (from `label`, when given), says its draft is deleted, nobody is emailed and it can't be undone, with an optional reason, "Yes, stop this follow-up" and "No, keep it". It is a `<details>`, so it works with no script. I sent it and Remind me tomorrow stay one click.
|
|
8
|
+
- **Breaking:** `nudgeAction`'s `dismiss` refuses a post without `confirm=yes` (the question's button sends it) with the new failure `confirm_needed` (409, "Please confirm first. Nothing has changed."), before reading anything. A page that draws its own Not needed button must ask first and post `confirm=yes`. `dismiss()` on the store is unchanged.
|
|
9
|
+
- `renderConfirm`, `confirmed` and `ConfirmOptions` are exported for an app's own pages. New words `dismiss.question`, `.question_untitled`, `.consequence`, `.reason` and `.yes`, `confirm.no` and `error.confirm_needed` (also in `CATALOGUE_WORDS`). New classes `wb-nudge-confirm`, `-confirm-open`, `-confirm-ask`, `-confirm-keep`, `-confirm-form`, `-confirm-question`, `-confirm-consequence`, `-confirm-field`, `-confirm-label`, `-confirm-input` and `-confirm-yes`, and the variables `--wb-nudge-danger` and `--wb-nudge-on-danger`; `wb-nudge-dismiss` is now on the `<summary>` that opens the question.
|
|
10
|
+
|
|
3
11
|
## 0.3.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -51,10 +51,10 @@ import { renderReplyBox } from '@mccaskie-labs/workbench-ai/reply-ui'
|
|
|
51
51
|
|
|
52
52
|
html = renderNudgeList(await nudges.due(account), { endpoint: '/app/waiting', replyBox: renderReplyBox, label: (n) => titleOf(n.subject) })
|
|
53
53
|
// The buttons post here, behind the owner's sign-in:
|
|
54
|
-
const r = await nudgeAction(nudges, fields, account) // sent, snooze (a day, out of quiet hours) or dismiss
|
|
54
|
+
const r = await nudgeAction(nudges, fields, account) // sent, snooze (a day, out of quiet hours) or dismiss (needs confirm=yes)
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
Every element carries a `wb-nudge-*` class with a default style (`NUDGE_STYLES`, written by `npx workbench-ui styles`); the draft uses the reply drafter's `wb-reply-*` box. Words are in `NUDGE_WORDS` (English, Tagalog, Taglish) under `ui.wbnudge.*`.
|
|
57
|
+
Not needed asks "Are you sure?" first: the button opens a question that names the follow-up and says its draft is deleted, nobody is emailed and it can't be undone, with an optional reason. `nudgeAction` refuses a `dismiss` without `confirm=yes` (`confirm_needed`, 409), so a page that draws its own Not needed button must ask first too. Every element carries a `wb-nudge-*` class with a default style (`NUDGE_STYLES`, written by `npx workbench-ui styles`); the draft uses the reply drafter's `wb-reply-*` box. Words are in `NUDGE_WORDS` (English, Tagalog, Taglish) under `ui.wbnudge.*`.
|
|
58
58
|
|
|
59
59
|
## Data
|
|
60
60
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface ConfirmOptions {
|
|
2
|
+
/** The tool's class prefix, without the trailing dash: "wb-booking". */
|
|
3
|
+
prefix: string;
|
|
4
|
+
/** Where the form posts. */
|
|
5
|
+
endpoint: string;
|
|
6
|
+
/** The hidden fields that say what to do: action, the thing's id or token. */
|
|
7
|
+
fields: Readonly<Record<string, string>>;
|
|
8
|
+
/** The first button's words: "Cancel". */
|
|
9
|
+
ask: string;
|
|
10
|
+
/** The first button's class, so it keeps the look it has in the tool's styles. */
|
|
11
|
+
askClass: string;
|
|
12
|
+
/** The question, naming the thing: "Cancel Sam's booking for Yoga on Monday 7 December at 6:00 pm?" */
|
|
13
|
+
question: string;
|
|
14
|
+
/** What happens next and who is told: "We'll email Sam to say it's cancelled." */
|
|
15
|
+
consequence?: string;
|
|
16
|
+
/** The button that does it, in its own words: "Yes, cancel this booking". */
|
|
17
|
+
yes: string;
|
|
18
|
+
/** The words that close the question again: "No, keep it". */
|
|
19
|
+
no: string;
|
|
20
|
+
/** A reason to send with it, when the action takes one. */
|
|
21
|
+
reason?: {
|
|
22
|
+
label: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
maxlength?: number;
|
|
25
|
+
required?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** For deleting a lot at once: a word the person must type before the button works. */
|
|
28
|
+
type?: {
|
|
29
|
+
label: string;
|
|
30
|
+
word: string;
|
|
31
|
+
};
|
|
32
|
+
/** More fields for the question's form, as HTML the tool has already escaped (a second box, a choice). Drawn above the button. */
|
|
33
|
+
extra?: string;
|
|
34
|
+
/** Draw the question already showing: the page was drawn again to ask it. */
|
|
35
|
+
open?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** The button and its question. */
|
|
38
|
+
export declare function renderConfirm(o: ConfirmOptions): string;
|
|
39
|
+
/** Whether a post answered the question: confirm=yes and, where a word had to be typed, that word. */
|
|
40
|
+
export declare function confirmed(body: Readonly<Record<string, unknown>>, word?: string): boolean;
|
|
41
|
+
interface ConfirmStyle {
|
|
42
|
+
name: string;
|
|
43
|
+
element: string;
|
|
44
|
+
about: string;
|
|
45
|
+
css: string;
|
|
46
|
+
states?: readonly {
|
|
47
|
+
selector: string;
|
|
48
|
+
about: string;
|
|
49
|
+
css: string;
|
|
50
|
+
}[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The question's classes with their default rules, for the tool's styles.
|
|
54
|
+
* `v` reads one of the tool's own variables (text, muted, line, sunk, surface,
|
|
55
|
+
* radius, danger, on-danger, focus).
|
|
56
|
+
*/
|
|
57
|
+
export declare function confirmStyles(prefix: string, v: (name: string) => string, o?: {
|
|
58
|
+
reason?: boolean;
|
|
59
|
+
}): ConfirmStyle[];
|
|
60
|
+
export {};
|
package/dist/confirm.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// "Are you sure?" for the actions that cancel, decline or delete
|
|
2
|
+
// (docs/critical-actions-audit.md, "The standard"). The button a person presses first only opens
|
|
3
|
+
// the question: what is about to happen, named in full, and who will be told.
|
|
4
|
+
// The second button does it. It is a <details>, so it works with no script,
|
|
5
|
+
// and the post carries confirm=yes, which the action helpers insist on: a page
|
|
6
|
+
// that draws its own one-click button is refused with `confirm_needed`.
|
|
7
|
+
//
|
|
8
|
+
// This file is the same in every tool that asks (only the prefix differs), so
|
|
9
|
+
// a project restyles each tool's question by that tool's own class names.
|
|
10
|
+
const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
11
|
+
const pattern = (word) => word.replace(/[.*+?^${}()|[\]\\\/-]/g, "\\$&");
|
|
12
|
+
/** The button and its question. */
|
|
13
|
+
export function renderConfirm(o) {
|
|
14
|
+
const p = o.prefix;
|
|
15
|
+
const hidden = Object.entries({ ...o.fields, confirm: "yes" })
|
|
16
|
+
.map(([k, v]) => `<input type="hidden" name="${esc(k)}" value="${esc(v)}">`)
|
|
17
|
+
.join("");
|
|
18
|
+
const reason = o.reason
|
|
19
|
+
? `<label class="${p}-confirm-field"><span class="${p}-confirm-label">${esc(o.reason.label)}</span><input class="${p}-confirm-input" type="text" name="${esc(o.reason.name ?? "reason")}" maxlength="${o.reason.maxlength ?? 200}"${o.reason.required ? " required" : ""}></label>`
|
|
20
|
+
: "";
|
|
21
|
+
const type = o.type
|
|
22
|
+
? `<label class="${p}-confirm-field"><span class="${p}-confirm-label">${esc(o.type.label)}</span><input class="${p}-confirm-input" type="text" name="confirm_word" required pattern="${esc(pattern(o.type.word))}" autocomplete="off" autocapitalize="off" spellcheck="false"></label>`
|
|
23
|
+
: "";
|
|
24
|
+
return `<details class="${p}-confirm"${o.open ? " open" : ""}><summary class="${p}-confirm-open ${esc(o.askClass)}"><span class="${p}-confirm-ask">${esc(o.ask)}</span><span class="${p}-confirm-keep">${esc(o.no)}</span></summary><form class="${p}-confirm-form" method="post" action="${esc(o.endpoint)}">${hidden}<p class="${p}-confirm-question">${esc(o.question)}</p>${o.consequence ? `<p class="${p}-confirm-consequence">${esc(o.consequence)}</p>` : ""}${reason}${o.extra ?? ""}${type}<button class="${p}-confirm-yes" type="submit">${esc(o.yes)}</button></form></details>`;
|
|
25
|
+
}
|
|
26
|
+
/** Whether a post answered the question: confirm=yes and, where a word had to be typed, that word. */
|
|
27
|
+
export function confirmed(body, word) {
|
|
28
|
+
if (body.confirm !== "yes" && body.confirm !== true)
|
|
29
|
+
return false;
|
|
30
|
+
return word === undefined || (typeof body.confirm_word === "string" && body.confirm_word.trim() === word);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The question's classes with their default rules, for the tool's styles.
|
|
34
|
+
* `v` reads one of the tool's own variables (text, muted, line, sunk, surface,
|
|
35
|
+
* radius, danger, on-danger, focus).
|
|
36
|
+
*/
|
|
37
|
+
export function confirmStyles(prefix, v, o = {}) {
|
|
38
|
+
const focus = { selector: ":focus-visible", about: "Has focus", css: `outline:none;box-shadow:0 0 0 3px ${v("focus")}` };
|
|
39
|
+
return [
|
|
40
|
+
{
|
|
41
|
+
name: `${prefix}-confirm`,
|
|
42
|
+
element: "details",
|
|
43
|
+
about: "A button that asks \"Are you sure?\" before it cancels, declines or deletes",
|
|
44
|
+
css: "display:block",
|
|
45
|
+
states: [
|
|
46
|
+
{ selector: `[open]`, about: "The question is showing", css: `display:flex;flex-direction:column;align-items:flex-start;gap:.75rem;box-sizing:border-box;width:100%;padding:1rem;background:${v("sunk")};border:1px solid ${v("line")};border-radius:${v("radius")}` },
|
|
47
|
+
{ selector: `[open] > summary`, about: "\"No, keep it\" sits under the button that does it", css: "order:2" },
|
|
48
|
+
{ selector: `[open] .${prefix}-confirm-ask`, about: "The first button's words, hidden while the question shows", css: "display:none" },
|
|
49
|
+
{ selector: `[open] .${prefix}-confirm-keep`, about: "\"No, keep it\", shown while the question shows", css: "display:inline" },
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: `${prefix}-confirm-open`,
|
|
54
|
+
element: "summary",
|
|
55
|
+
about: "The first button: it only opens the question",
|
|
56
|
+
css: "list-style:none",
|
|
57
|
+
states: [{ selector: "::-webkit-details-marker", about: "No arrow beside the button", css: "display:none" }],
|
|
58
|
+
},
|
|
59
|
+
{ name: `${prefix}-confirm-ask`, element: "span", about: "The first button's words", css: "display:inline" },
|
|
60
|
+
{ name: `${prefix}-confirm-keep`, element: "span", about: "\"No, keep it\": closes the question", css: `display:none;color:${v("text")}` },
|
|
61
|
+
{ name: `${prefix}-confirm-form`, element: "form", about: "The question and the button that does it", css: "display:grid;gap:.75rem;justify-items:start;width:100%" },
|
|
62
|
+
{ name: `${prefix}-confirm-question`, element: "p", about: "What is about to happen, naming the thing", css: `margin:0;font-weight:600;color:${v("text")}` },
|
|
63
|
+
{ name: `${prefix}-confirm-consequence`, element: "p", about: "What happens next and who is told", css: `margin:0;color:${v("muted")}` },
|
|
64
|
+
...(o.reason
|
|
65
|
+
? [
|
|
66
|
+
{ name: `${prefix}-confirm-field`, element: "label", about: "The reason, or the word to type", css: "display:grid;gap:.25rem;width:100%" },
|
|
67
|
+
{ name: `${prefix}-confirm-label`, element: "span", about: "What to type there", css: `color:${v("muted")}` },
|
|
68
|
+
{
|
|
69
|
+
name: `${prefix}-confirm-input`,
|
|
70
|
+
element: "input",
|
|
71
|
+
about: "Where it is typed",
|
|
72
|
+
css: `box-sizing:border-box;width:100%;min-height:2.75rem;padding:.5rem .75rem;font:inherit;color:${v("text")};background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")}`,
|
|
73
|
+
states: [focus],
|
|
74
|
+
},
|
|
75
|
+
]
|
|
76
|
+
: []),
|
|
77
|
+
{
|
|
78
|
+
name: `${prefix}-confirm-yes`,
|
|
79
|
+
element: "button",
|
|
80
|
+
about: "The button that does it",
|
|
81
|
+
css: `display:inline-flex;align-items:center;justify-content:center;min-height:2.75rem;padding:0 1rem;font:inherit;font-weight:600;border-radius:${v("radius")};cursor:pointer;color:${v("on-danger")};background:${v("danger")};border:1px solid ${v("danger")}`,
|
|
82
|
+
states: [focus],
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
}
|
package/dist/errors.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export type NudgesErrorCode =
|
|
|
9
9
|
| "unknown_nudge"
|
|
10
10
|
/** The account's plan doesn't include nudges (or they are switched off for it). Nothing was written. */
|
|
11
11
|
| "not_allowed"
|
|
12
|
+
/** Not needed arrived without the answer to "Are you sure?" (confirm=yes). Nothing was changed. */
|
|
13
|
+
| "confirm_needed"
|
|
12
14
|
/** The database refused or failed. */
|
|
13
15
|
| "store_failed";
|
|
14
16
|
export declare class NudgesError extends Error {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { checkNudgeConfig, MAX_STEPS, nudgeProblems } from "./config.ts";
|
|
|
3
3
|
export { nudgesMigration } from "./migration.ts";
|
|
4
4
|
export { QUOTE_FOLLOW_UP_GUIDE, STARTER_NUDGES } from "./starters.ts";
|
|
5
5
|
export { nudgeAction, renderNudge, renderNudgeList, type ActionResult, type ListOptions, type Words } from "./ui.ts";
|
|
6
|
+
export { confirmed, renderConfirm, type ConfirmOptions } from "./confirm.ts";
|
|
6
7
|
export { CATALOGUE_WORDS, NUDGE_WORDS, NUDGE_WORD_KEYS, type NudgeWordKey } from "./words.ts";
|
|
7
8
|
export { NUDGE_STYLES } from "./styles.ts";
|
|
8
9
|
export { NudgesError, type NudgesErrorCode } from "./errors.ts";
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { checkNudgeConfig, MAX_STEPS, nudgeProblems } from "./config.js";
|
|
|
9
9
|
export { nudgesMigration } from "./migration.js";
|
|
10
10
|
export { QUOTE_FOLLOW_UP_GUIDE, STARTER_NUDGES } from "./starters.js";
|
|
11
11
|
export { nudgeAction, renderNudge, renderNudgeList } from "./ui.js";
|
|
12
|
+
export { confirmed, renderConfirm } from "./confirm.js";
|
|
12
13
|
export { CATALOGUE_WORDS, NUDGE_WORDS, NUDGE_WORD_KEYS } from "./words.js";
|
|
13
14
|
export { NUDGE_STYLES } from "./styles.js";
|
|
14
15
|
export { NudgesError } from "./errors.js";
|
package/dist/styles.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// workbench-ai) when the app passes renderReplyBox. `npx workbench-ui styles`
|
|
7
7
|
// writes these into the project's public/styles/workbench.css inside
|
|
8
8
|
// `@layer workbench`. The names are public: renaming one is a breaking change.
|
|
9
|
+
import { confirmStyles } from "./confirm.js";
|
|
9
10
|
const v = (name) => `var(--wb-nudge-${name})`;
|
|
10
11
|
const focus = { selector: ":focus-visible", about: "Has focus", css: `outline:none;box-shadow:0 0 0 3px ${v("focus")}` };
|
|
11
12
|
const button = `display:inline-flex;align-items:center;justify-content:center;min-height:2.75rem;padding:0 1rem;font:inherit;font-weight:600;border-radius:${v("radius")};cursor:pointer`;
|
|
@@ -26,6 +27,8 @@ export const NUDGE_STYLES = {
|
|
|
26
27
|
{ name: "--wb-nudge-taken", value: "#c9a227", about: "The edge of a nudge whose date was taken or has no draft" },
|
|
27
28
|
{ name: "--wb-nudge-accent", value: "#4c8dff", about: "The main button: I sent it" },
|
|
28
29
|
{ name: "--wb-nudge-on-accent", value: "#ffffff", about: "Text on the main button" },
|
|
30
|
+
{ name: "--wb-nudge-danger", value: "#e5484d", about: "The button that stops a follow-up after \"Are you sure?\"" },
|
|
31
|
+
{ name: "--wb-nudge-on-danger", value: "#ffffff", about: "Text on that button" },
|
|
29
32
|
{ name: "--wb-nudge-focus", value: "rgba(76,141,255,.4)", about: "The ring round a control with focus" },
|
|
30
33
|
{ name: "--wb-nudge-radius", value: "8px", about: "Corner rounding" },
|
|
31
34
|
],
|
|
@@ -65,6 +68,7 @@ export const NUDGE_STYLES = {
|
|
|
65
68
|
states: [focus],
|
|
66
69
|
},
|
|
67
70
|
{ name: "wb-nudge-snooze", element: "button", about: "Remind me tomorrow", css: `${button};color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
|
|
68
|
-
{ name: "wb-nudge-dismiss", element: "
|
|
71
|
+
{ name: "wb-nudge-dismiss", element: "summary", about: "Not needed: opens \"Are you sure?\"", css: `${button};color:${v("muted")};background:transparent;border:1px solid transparent`, states: [focus] },
|
|
72
|
+
...confirmStyles("wb-nudge", v, { reason: true }),
|
|
69
73
|
],
|
|
70
74
|
};
|
package/dist/ui.d.ts
CHANGED
|
@@ -34,6 +34,8 @@ export interface ActionResult {
|
|
|
34
34
|
* The project's endpoint (behind the owner's sign-in) hands the posted form
|
|
35
35
|
* fields here with the signed-in owner's account: `sent` (the follow-up went:
|
|
36
36
|
* touch we_replied), `snooze` (back in a day, out of the quiet hours) or
|
|
37
|
-
* `dismiss
|
|
37
|
+
* `dismiss` (with an optional reason; needs confirm=yes, or it is refused
|
|
38
|
+
* with `confirm_needed`, 409, and nothing changes). A nudge of another
|
|
39
|
+
* account is answered as unknown.
|
|
38
40
|
*/
|
|
39
41
|
export declare function nudgeAction(nudges: Nudges, body: Record<string, unknown>, account: string, now?: Date): Promise<ActionResult>;
|
package/dist/ui.js
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
// the app passes workbench-ai's renderReplyBox (Copy works with its
|
|
7
7
|
// REPLY_SCRIPT); otherwise a plain text box to copy from. Every element
|
|
8
8
|
// carries a wb-nudge-* class (NUDGE_STYLES), and every button is at least
|
|
9
|
-
// 44 px tall.
|
|
9
|
+
// 44 px tall. Not needed asks "Are you sure?" first (renderConfirm), and
|
|
10
|
+
// nudgeAction refuses a dismiss without confirm=yes.
|
|
11
|
+
import { confirmed, renderConfirm } from "./confirm.js";
|
|
10
12
|
import { NudgesError } from "./errors.js";
|
|
11
13
|
import { addDuration, outOfQuiet } from "./time.js";
|
|
12
14
|
import { NUDGE_WORDS } from "./words.js";
|
|
@@ -38,8 +40,21 @@ export function renderNudge(n, o) {
|
|
|
38
40
|
? o.replyBox(n.draft, { hidden: { nudge: n.id } })
|
|
39
41
|
: `<textarea class="wb-nudge-text" readonly rows="5" lang="${n.draft.language === "en" ? "en" : "fil"}">${esc(n.draft.text)}</textarea>`
|
|
40
42
|
: `<p class="wb-nudge-none">${esc(w("no_draft"))}</p>`;
|
|
43
|
+
// Not needed ends the follow-up and deletes its draft for good, so it asks first.
|
|
44
|
+
const dismiss = renderConfirm({
|
|
45
|
+
prefix: "wb-nudge",
|
|
46
|
+
endpoint: o.endpoint,
|
|
47
|
+
fields: { nudge: n.id, action: "dismiss" },
|
|
48
|
+
ask: w("dismiss"),
|
|
49
|
+
askClass: "wb-nudge-dismiss",
|
|
50
|
+
question: title ? w("dismiss.question", { title }) : w("dismiss.question_untitled"),
|
|
51
|
+
consequence: w("dismiss.consequence"),
|
|
52
|
+
yes: w("dismiss.yes"),
|
|
53
|
+
no: w("confirm.no"),
|
|
54
|
+
reason: { label: w("dismiss.reason") },
|
|
55
|
+
});
|
|
41
56
|
const cls = ["wb-nudge", n.reason_code !== "no_reply" ? "wb-nudge-taken" : ""].filter(Boolean).join(" ");
|
|
42
|
-
return `<article class="${cls}" id="wb-nudge-${esc(n.id)}">${title ? `<h3 class="wb-nudge-title">${esc(title)}</h3>` : ""}<p class="wb-nudge-reason">${esc(reason)}</p><p class="wb-nudge-step">${esc(w("step", { step: n.step, steps: n.steps }))}</p><div class="wb-nudge-draft">${draft}</div><div class="wb-nudge-actions">${form("sent", "wb-nudge-sent", w("sent"))}${form("snooze", "wb-nudge-snooze", w("snooze"))}${
|
|
57
|
+
return `<article class="${cls}" id="wb-nudge-${esc(n.id)}">${title ? `<h3 class="wb-nudge-title">${esc(title)}</h3>` : ""}<p class="wb-nudge-reason">${esc(reason)}</p><p class="wb-nudge-step">${esc(w("step", { step: n.step, steps: n.steps }))}</p><div class="wb-nudge-draft">${draft}</div><div class="wb-nudge-actions">${form("sent", "wb-nudge-sent", w("sent"))}${form("snooze", "wb-nudge-snooze", w("snooze"))}${dismiss}</div></article>`;
|
|
43
58
|
}
|
|
44
59
|
/** The account's Waiting for you list (from due()), with its count. */
|
|
45
60
|
export function renderNudgeList(due, o) {
|
|
@@ -49,17 +64,26 @@ export function renderNudgeList(due, o) {
|
|
|
49
64
|
const items = due.map((n) => `<li class="wb-nudge-item">${renderNudge(n, o)}</li>`).join("");
|
|
50
65
|
return `<section class="wb-nudge-list"><h2 class="wb-nudge-list-title">${esc(w("list.title"))} <span class="wb-nudge-count">${due.length}</span></h2><ul class="wb-nudge-items">${items}</ul></section>`;
|
|
51
66
|
}
|
|
67
|
+
/** Not needed can't be undone, so the post must carry the answer to "Are you sure?" (confirm=yes, which renderConfirm's button sends). */
|
|
68
|
+
function needConfirm(body) {
|
|
69
|
+
if (!confirmed(body))
|
|
70
|
+
throw new NudgesError("confirm_needed", NUDGE_WORDS.en["error.confirm_needed"]);
|
|
71
|
+
}
|
|
52
72
|
/**
|
|
53
73
|
* The project's endpoint (behind the owner's sign-in) hands the posted form
|
|
54
74
|
* fields here with the signed-in owner's account: `sent` (the follow-up went:
|
|
55
75
|
* touch we_replied), `snooze` (back in a day, out of the quiet hours) or
|
|
56
|
-
* `dismiss
|
|
76
|
+
* `dismiss` (with an optional reason; needs confirm=yes, or it is refused
|
|
77
|
+
* with `confirm_needed`, 409, and nothing changes). A nudge of another
|
|
78
|
+
* account is answered as unknown.
|
|
57
79
|
*/
|
|
58
80
|
export async function nudgeAction(nudges, body, account, now = new Date()) {
|
|
59
81
|
try {
|
|
60
82
|
const action = String(body.action ?? "");
|
|
61
83
|
if (!["sent", "snooze", "dismiss"].includes(action))
|
|
62
84
|
throw new NudgesError("bad_input", "action must be sent, snooze or dismiss");
|
|
85
|
+
if (action === "dismiss")
|
|
86
|
+
needConfirm(body);
|
|
63
87
|
const n = await nudges.get(String(body.nudge ?? ""));
|
|
64
88
|
if (!n || n.account !== account || !["waiting", "due", "snoozed"].includes(n.status))
|
|
65
89
|
throw new NudgesError("unknown_nudge", "no such nudge, or it has ended");
|
|
@@ -75,7 +99,7 @@ export async function nudgeAction(nudges, body, account, now = new Date()) {
|
|
|
75
99
|
}
|
|
76
100
|
catch (e) {
|
|
77
101
|
if (e instanceof NudgesError) {
|
|
78
|
-
const status = e.code === "unknown_nudge" ? 404 : e.code === "store_failed" ? 502 : e.code === "bad_config" ? 500 : e.code === "not_allowed" ? 403 : 400;
|
|
102
|
+
const status = e.code === "unknown_nudge" ? 404 : e.code === "store_failed" ? 502 : e.code === "bad_config" ? 500 : e.code === "not_allowed" ? 403 : e.code === "confirm_needed" ? 409 : 400;
|
|
79
103
|
return { status, body: { ok: false, error: e.code, message: e.message } };
|
|
80
104
|
}
|
|
81
105
|
throw e;
|
package/dist/words.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const NUDGE_WORD_KEYS: readonly ["list.title", "list.empty", "step", "reason.no_reply", "reason.date_unavailable", "reason.not_on_plan", "reason.draft_failed", "reason.no_ai", "no_draft", "sent", "snooze", "dismiss", "unit.minute", "unit.minutes", "unit.hour", "unit.hours", "unit.day", "unit.days", "email.digest.subject", "email.digest.subject_one", "email.due.subject", "email.due.subject_one", "email.body", "email.body_one", "email.open", "email.footer"];
|
|
1
|
+
export declare const NUDGE_WORD_KEYS: readonly ["list.title", "list.empty", "step", "reason.no_reply", "reason.date_unavailable", "reason.not_on_plan", "reason.draft_failed", "reason.no_ai", "no_draft", "sent", "snooze", "dismiss", "unit.minute", "unit.minutes", "unit.hour", "unit.hours", "unit.day", "unit.days", "email.digest.subject", "email.digest.subject_one", "email.due.subject", "email.due.subject_one", "email.body", "email.body_one", "email.open", "email.footer", "dismiss.question", "dismiss.question_untitled", "dismiss.consequence", "dismiss.reason", "dismiss.yes", "confirm.no", "error.confirm_needed"];
|
|
2
2
|
export type NudgeWordKey = (typeof NUDGE_WORD_KEYS)[number];
|
|
3
3
|
/** The words by language, as `ui.wbnudge.<key>` would hold them. */
|
|
4
4
|
export declare const NUDGE_WORDS: Readonly<Record<"en" | "tl" | "taglish", Readonly<Record<NudgeWordKey, string>>>>;
|
package/dist/words.js
CHANGED
|
@@ -32,6 +32,13 @@ export const NUDGE_WORD_KEYS = [
|
|
|
32
32
|
"email.body_one",
|
|
33
33
|
"email.open",
|
|
34
34
|
"email.footer",
|
|
35
|
+
"dismiss.question",
|
|
36
|
+
"dismiss.question_untitled",
|
|
37
|
+
"dismiss.consequence",
|
|
38
|
+
"dismiss.reason",
|
|
39
|
+
"dismiss.yes",
|
|
40
|
+
"confirm.no",
|
|
41
|
+
"error.confirm_needed",
|
|
35
42
|
];
|
|
36
43
|
const en = {
|
|
37
44
|
"list.title": "Waiting for you",
|
|
@@ -60,6 +67,13 @@ const en = {
|
|
|
60
67
|
"email.body_one": "You have a follow-up ready to send, already drafted.",
|
|
61
68
|
"email.open": "Open Waiting for you",
|
|
62
69
|
"email.footer": "This is a reminder for you. Nothing was sent to your customers.",
|
|
70
|
+
"dismiss.question": "Stop following up {title}?",
|
|
71
|
+
"dismiss.question_untitled": "Stop this follow-up?",
|
|
72
|
+
"dismiss.consequence": "Its draft is deleted and no more reminders come for it. Nobody is emailed. This can't be undone.",
|
|
73
|
+
"dismiss.reason": "Why (optional, only you see it)",
|
|
74
|
+
"dismiss.yes": "Yes, stop this follow-up",
|
|
75
|
+
"confirm.no": "No, keep it",
|
|
76
|
+
"error.confirm_needed": "Please confirm first. Nothing has changed.",
|
|
63
77
|
};
|
|
64
78
|
const tl = {
|
|
65
79
|
"list.title": "Naghihintay sa iyo",
|
|
@@ -88,6 +102,13 @@ const tl = {
|
|
|
88
102
|
"email.body_one": "May isang follow-up kayong handa nang ipadala, at may draft na.",
|
|
89
103
|
"email.open": "Buksan ang Naghihintay sa iyo",
|
|
90
104
|
"email.footer": "Paalala lang ito para sa inyo. Walang naipadala sa inyong mga customer.",
|
|
105
|
+
"dismiss.question": "Itigil ang pag-follow up sa {title}?",
|
|
106
|
+
"dismiss.question_untitled": "Itigil ang follow-up na ito?",
|
|
107
|
+
"dismiss.consequence": "Mabubura ang draft nito at wala nang paalala para dito. Walang ie-email. Hindi na ito maibabalik.",
|
|
108
|
+
"dismiss.reason": "Bakit (hindi kailangan, kayo lang ang makakakita)",
|
|
109
|
+
"dismiss.yes": "Oo, itigil ang follow-up na ito",
|
|
110
|
+
"confirm.no": "Huwag, panatilihin",
|
|
111
|
+
"error.confirm_needed": "Pakikumpirma muna. Walang nagbago.",
|
|
91
112
|
};
|
|
92
113
|
const taglish = {
|
|
93
114
|
"list.title": "Waiting for you",
|
|
@@ -116,6 +137,13 @@ const taglish = {
|
|
|
116
137
|
"email.body_one": "May isang follow-up kayong ready to send, may draft na.",
|
|
117
138
|
"email.open": "Buksan ang Waiting for you",
|
|
118
139
|
"email.footer": "Reminder lang ito para sa inyo. Walang na-send sa mga customer n'yo.",
|
|
140
|
+
"dismiss.question": "I-stop ang pag-follow up sa {title}?",
|
|
141
|
+
"dismiss.question_untitled": "I-stop ang follow-up na ito?",
|
|
142
|
+
"dismiss.consequence": "Made-delete ang draft nito at wala nang reminders para dito. Walang ie-email. Hindi na ito maibabalik.",
|
|
143
|
+
"dismiss.reason": "Bakit (optional, kayo lang ang makakakita)",
|
|
144
|
+
"dismiss.yes": "Oo, i-stop ang follow-up",
|
|
145
|
+
"confirm.no": "Hindi, i-keep",
|
|
146
|
+
"error.confirm_needed": "Paki-confirm muna. Walang nagbago.",
|
|
119
147
|
};
|
|
120
148
|
/** The words by language, as `ui.wbnudge.<key>` would hold them. */
|
|
121
149
|
export const NUDGE_WORDS = { en, tl, taglish };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mccaskie-labs/workbench-nudges",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Follow-up nudges for Workbench apps: remind the business to follow up when a conversation goes quiet (\"no reply in 3 days\"), with the follow-up already drafted by the reply drafter when it comes due. One scan from the app's cron, quiet hours, at most three steps, and a daily email digest to the owner. Never messages the business's customer.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|