@connextar/house 0.24.0 → 0.25.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/README.md +49 -12
- package/dist/account/data-contract.d.ts +20 -0
- package/dist/account/data-contract.d.ts.map +1 -1
- package/dist/account/data-contract.js +17 -3
- package/dist/account/data-contract.js.map +1 -1
- package/dist/account/index.d.ts +1 -1
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/index.js +1 -1
- package/dist/account/index.js.map +1 -1
- package/dist/admin/access.d.ts +22 -2
- package/dist/admin/access.d.ts.map +1 -1
- package/dist/admin/access.js +35 -7
- package/dist/admin/access.js.map +1 -1
- package/dist/admin/index.d.ts +1 -1
- package/dist/admin/index.d.ts.map +1 -1
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.js.map +1 -1
- package/dist/admin/second-factor-index.d.ts +1 -1
- package/dist/admin/second-factor-index.d.ts.map +1 -1
- package/dist/admin/second-factor-index.js.map +1 -1
- package/dist/admin/second-factor.d.ts +17 -6
- package/dist/admin/second-factor.d.ts.map +1 -1
- package/dist/admin/second-factor.js +5 -3
- package/dist/admin/second-factor.js.map +1 -1
- package/dist/admin/view-as.d.ts +12 -3
- package/dist/admin/view-as.d.ts.map +1 -1
- package/dist/admin/view-as.js +6 -2
- package/dist/admin/view-as.js.map +1 -1
- package/dist/auth/step-up-index.d.ts +1 -1
- package/dist/auth/step-up-index.d.ts.map +1 -1
- package/dist/auth/step-up-index.js.map +1 -1
- package/dist/auth/step-up.d.ts +22 -0
- package/dist/auth/step-up.d.ts.map +1 -1
- package/dist/auth/step-up.js +8 -6
- package/dist/auth/step-up.js.map +1 -1
- package/dist/whats-new/index.d.ts +1 -1
- package/dist/whats-new/index.d.ts.map +1 -1
- package/dist/whats-new/index.js +1 -1
- package/dist/whats-new/index.js.map +1 -1
- package/dist/whats-new/use-whats-new.d.ts +7 -1
- package/dist/whats-new/use-whats-new.d.ts.map +1 -1
- package/dist/whats-new/use-whats-new.js +10 -6
- package/dist/whats-new/use-whats-new.js.map +1 -1
- package/dist/words.d.ts +12 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +17 -0
- package/dist/words.js.map +1 -0
- package/package.json +1 -1
- package/src/account/data-contract.ts +34 -4
- package/src/account/index.ts +3 -0
- package/src/admin/access.ts +50 -9
- package/src/admin/index.ts +2 -0
- package/src/admin/second-factor-index.ts +1 -0
- package/src/admin/second-factor.ts +19 -5
- package/src/admin/view-as.ts +20 -4
- package/src/auth/step-up-index.ts +1 -1
- package/src/auth/step-up.ts +28 -6
- package/src/whats-new/index.ts +1 -1
- package/src/whats-new/use-whats-new.ts +13 -6
- package/src/words.ts +16 -0
package/src/admin/access.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { fillWords } from "../words.js";
|
|
1
2
|
import { DEFAULT_ADMIN_PROOF_PREFIX, sessionProofReader, type AuthWithProofs } from "./proof.js";
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -61,25 +62,49 @@ export interface AdminRefusalWording {
|
|
|
61
62
|
fixHref?: string | null;
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
/**
|
|
65
|
-
|
|
65
|
+
/**
|
|
66
|
+
* The places only the app knows, which the house's sentences and links name through placeholders. Set them once
|
|
67
|
+
* rather than rewriting whole sentences; an app's own `message` or `fixHref` may use the same placeholders.
|
|
68
|
+
*/
|
|
69
|
+
export interface AdminPlaces {
|
|
70
|
+
/** Where two-factor is turned on, as the app's screens name it: "Settings › Account & security". `{securitySettings}`. */
|
|
71
|
+
securitySettings?: string;
|
|
72
|
+
/** That screen's address: "/settings?tab=security#two-factor". `{securitySettingsHref}`. */
|
|
73
|
+
securitySettingsHref?: string;
|
|
74
|
+
/** The sign-in page: "/login". `{signInHref}`. */
|
|
75
|
+
signInHref?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const DEFAULT_ADMIN_PLACES: Required<AdminPlaces> = {
|
|
79
|
+
securitySettings: "your account’s security settings",
|
|
80
|
+
securitySettingsHref: "/settings#two-factor",
|
|
81
|
+
signInHref: "/sign-in",
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** Per reason, the words and the link an app wants instead of the house's, and the places its sentences name. */
|
|
85
|
+
export type AdminRefusalWords = Partial<Record<AdminRefusalReason, AdminRefusalWording>> & { places?: AdminPlaces };
|
|
66
86
|
|
|
87
|
+
/**
|
|
88
|
+
* The house's sentences and links, as templates. `{securitySettings}`, `{securitySettingsHref}` and `{signInHref}`
|
|
89
|
+
* are filled from `words.places`. The two links to the security settings also carry `?admin=<reason>`, so the page
|
|
90
|
+
* can say why the person was sent there (`adminRefusalFromParam`).
|
|
91
|
+
*/
|
|
67
92
|
export const DEFAULT_ADMIN_REFUSAL_WORDS: Record<AdminRefusalReason, Required<AdminRefusalWording>> = {
|
|
68
|
-
"signed-out": { message: "Sign in to use administration.", fixHref: "
|
|
93
|
+
"signed-out": { message: "Sign in to use administration.", fixHref: "{signInHref}" },
|
|
69
94
|
"session-ended": {
|
|
70
95
|
message: "This sign-in has ended. Sign in again to use administration.",
|
|
71
|
-
fixHref: "
|
|
96
|
+
fixHref: "{signInHref}",
|
|
72
97
|
},
|
|
73
98
|
"not-admin": { message: "Only an administrator can do that.", fixHref: null },
|
|
74
99
|
"two-factor-off": {
|
|
75
100
|
message:
|
|
76
|
-
"Administration needs two-factor authentication on your account. Turn it on in
|
|
77
|
-
fixHref: "
|
|
101
|
+
"Administration needs two-factor authentication on your account. Turn it on in {securitySettings}, then come back.",
|
|
102
|
+
fixHref: "{securitySettingsHref}",
|
|
78
103
|
},
|
|
79
104
|
"sign-in-without-second-factor": {
|
|
80
105
|
message:
|
|
81
106
|
"Administration needs a sign-in that used your authenticator code or a passkey, and this one didn’t. Sign out, then sign in with your email, password and code — or with a passkey.",
|
|
82
|
-
fixHref: "
|
|
107
|
+
fixHref: "{securitySettingsHref}",
|
|
83
108
|
},
|
|
84
109
|
"viewing-as": {
|
|
85
110
|
message: "You’re viewing as somebody else. Stop viewing as them to use administration.",
|
|
@@ -87,6 +112,16 @@ export const DEFAULT_ADMIN_REFUSAL_WORDS: Record<AdminRefusalReason, Required<Ad
|
|
|
87
112
|
},
|
|
88
113
|
};
|
|
89
114
|
|
|
115
|
+
const SENT_TO_SETTINGS = new Set<AdminRefusalReason>(["two-factor-off", "sign-in-without-second-factor"]);
|
|
116
|
+
|
|
117
|
+
/** `href` with `admin=<reason>` added to its query, before any `#fragment`. */
|
|
118
|
+
function withAdminParam(href: string, reason: AdminRefusalReason): string {
|
|
119
|
+
const hash = href.indexOf("#");
|
|
120
|
+
const path = hash === -1 ? href : href.slice(0, hash);
|
|
121
|
+
const fragment = hash === -1 ? "" : href.slice(hash);
|
|
122
|
+
return `${path}${path.includes("?") ? "&" : "?"}admin=${encodeURIComponent(reason)}${fragment}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
90
125
|
const STATUS: Record<AdminRefusalReason, 401 | 403> = {
|
|
91
126
|
"signed-out": 401,
|
|
92
127
|
"session-ended": 401,
|
|
@@ -100,11 +135,17 @@ const STATUS: Record<AdminRefusalReason, 401 | 403> = {
|
|
|
100
135
|
export function adminRefusal(reason: AdminRefusalReason, words: AdminRefusalWords = {}): AdminRefusal {
|
|
101
136
|
const own = words[reason];
|
|
102
137
|
const fallback = DEFAULT_ADMIN_REFUSAL_WORDS[reason];
|
|
138
|
+
const places: Record<string, string> = { ...DEFAULT_ADMIN_PLACES };
|
|
139
|
+
for (const [key, value] of Object.entries(words.places ?? {})) if (value?.trim()) places[key] = value.trim();
|
|
140
|
+
const ownHref = own && "fixHref" in own && own.fixHref !== undefined;
|
|
141
|
+
const href = ownHref ? own.fixHref! : fallback.fixHref;
|
|
142
|
+
let fixHref = href === null ? null : fillWords(href, places);
|
|
143
|
+
if (fixHref !== null && !ownHref && SENT_TO_SETTINGS.has(reason)) fixHref = withAdminParam(fixHref, reason);
|
|
103
144
|
return {
|
|
104
145
|
ok: false,
|
|
105
146
|
reason,
|
|
106
|
-
message: own?.message ?? fallback.message,
|
|
107
|
-
fixHref
|
|
147
|
+
message: fillWords(own?.message ?? fallback.message, places),
|
|
148
|
+
fixHref,
|
|
108
149
|
status: STATUS[reason],
|
|
109
150
|
};
|
|
110
151
|
}
|
package/src/admin/index.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
export {
|
|
9
9
|
ADMIN_REFUSAL_REASONS,
|
|
10
10
|
AdminRefusalError,
|
|
11
|
+
DEFAULT_ADMIN_PLACES,
|
|
11
12
|
DEFAULT_ADMIN_REFUSAL_WORDS,
|
|
12
13
|
adminAccess,
|
|
13
14
|
adminRefusal,
|
|
@@ -19,6 +20,7 @@ export {
|
|
|
19
20
|
type AdminAccessFacts,
|
|
20
21
|
type AdminAccessLookups,
|
|
21
22
|
type AdminAccessOptions,
|
|
23
|
+
type AdminPlaces,
|
|
22
24
|
type AdminRefusal,
|
|
23
25
|
type AdminRefusalReason,
|
|
24
26
|
type AdminRefusalWording,
|
|
@@ -80,8 +80,20 @@ export interface AdminSecondFactorSessionsOptions {
|
|
|
80
80
|
standing?: string;
|
|
81
81
|
/** The sign-in event's scope. Default platform. */
|
|
82
82
|
scope?: AdminAuditScope;
|
|
83
|
-
/**
|
|
84
|
-
|
|
83
|
+
/**
|
|
84
|
+
* The refusals' words. Each is a sentence, or a function of the refused route for an app that says which change
|
|
85
|
+
* was refused — FB Web re-threw the plugin's error in its own words until it could.
|
|
86
|
+
*/
|
|
87
|
+
messages?: {
|
|
88
|
+
factorChange?: string | ((refused: AdminFactorChange) => string);
|
|
89
|
+
passkeyNeedsTwoFactor?: string | ((refused: AdminFactorChange) => string);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The route a factor-change refusal is for: adding a passkey, or reading or changing two-factor. */
|
|
94
|
+
export interface AdminFactorChange {
|
|
95
|
+
path: string;
|
|
96
|
+
factor: "passkey" | "two-factor";
|
|
85
97
|
}
|
|
86
98
|
|
|
87
99
|
export interface AdminSignIn {
|
|
@@ -289,10 +301,12 @@ export function adminSecondFactorSessions(options: AdminSecondFactorSessionsOpti
|
|
|
289
301
|
addsPasskey && !twoFactorOn
|
|
290
302
|
? ADMIN_SECOND_FACTOR_ERROR_CODES.ADMIN_PASSKEY_NEEDS_TWO_FACTOR
|
|
291
303
|
: ADMIN_SECOND_FACTOR_ERROR_CODES.ADMIN_SECOND_FACTOR_REQUIRED;
|
|
292
|
-
const
|
|
304
|
+
const refused: AdminFactorChange = { path: ctx.path, factor: addsPasskey ? "passkey" : "two-factor" };
|
|
305
|
+
const words =
|
|
293
306
|
refusal.code === "ADMIN_PASSKEY_NEEDS_TWO_FACTOR"
|
|
294
|
-
?
|
|
295
|
-
:
|
|
307
|
+
? options.messages?.passkeyNeedsTwoFactor
|
|
308
|
+
: options.messages?.factorChange;
|
|
309
|
+
const message = (typeof words === "function" ? words(refused) : words)?.trim() || refusal.message;
|
|
296
310
|
throw new APIError("FORBIDDEN", { code: refusal.code, message });
|
|
297
311
|
}),
|
|
298
312
|
};
|
package/src/admin/view-as.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
2
2
|
|
|
3
|
+
import { fillWords } from "../words.js";
|
|
3
4
|
import { AdminRefusalError, adminRefusal, type AdminAccess, type AdminRefusal } from "./access.js";
|
|
4
5
|
import {
|
|
5
6
|
emitSafely,
|
|
@@ -336,8 +337,17 @@ export interface MaybeViewing {
|
|
|
336
337
|
*/
|
|
337
338
|
export interface ViewingAsWords {
|
|
338
339
|
appName?: string;
|
|
339
|
-
/**
|
|
340
|
-
|
|
340
|
+
/**
|
|
341
|
+
* The whole sentence: a template with `{app}`, `{name}` and `{what}` — "You are viewing {app} as {name}. Stop
|
|
342
|
+
* viewing as them before {what}." — or a function for anything a template can't say. `name` is "somebody else"
|
|
343
|
+
* in a template, and null to a function, when the view's person has no name to give.
|
|
344
|
+
*/
|
|
345
|
+
message?: string | ((refusal: { name: string | null; what: string; appName: string | null }) => string);
|
|
346
|
+
/**
|
|
347
|
+
* When whether the request is a view can't be told (the lookup threw), the request is refused. Template with
|
|
348
|
+
* `{what}`; default "Could not tell whether this request is a “view as”, so {what} was refused."
|
|
349
|
+
*/
|
|
350
|
+
undetermined?: string;
|
|
341
351
|
}
|
|
342
352
|
|
|
343
353
|
export interface RefuseWhileViewingAsOptions extends ViewingAsWords {
|
|
@@ -348,7 +358,10 @@ export interface RefuseWhileViewingAsOptions extends ViewingAsWords {
|
|
|
348
358
|
/** The sentence a refusal under "view as" says — the house's, with the app's name, or the app's own. */
|
|
349
359
|
export function viewingAsMessage(name: string | null | undefined, what: string, words: ViewingAsWords = {}): string {
|
|
350
360
|
const appName = words.appName?.trim() || null;
|
|
351
|
-
if (words.message) return words.message({ name: name || null, what, appName });
|
|
361
|
+
if (typeof words.message === "function") return words.message({ name: name || null, what, appName });
|
|
362
|
+
if (typeof words.message === "string" && words.message.trim()) {
|
|
363
|
+
return fillWords(words.message, { app: appName ?? "", name: name || "somebody else", what });
|
|
364
|
+
}
|
|
352
365
|
const viewing = appName ? `viewing ${appName} as` : "viewing as";
|
|
353
366
|
return `You’re ${viewing} ${name || "somebody else"}. Stop viewing as them before ${what}.`;
|
|
354
367
|
}
|
|
@@ -414,7 +427,10 @@ export function viewingAsGate(options: ViewingAsGateOptions): ViewingAsGate {
|
|
|
414
427
|
try {
|
|
415
428
|
viewing = await lookup();
|
|
416
429
|
} catch (error) {
|
|
417
|
-
const message =
|
|
430
|
+
const message = fillWords(
|
|
431
|
+
options.undetermined?.trim() || "Could not tell whether this request is a “view as”, so {what} was refused.",
|
|
432
|
+
{ what },
|
|
433
|
+
);
|
|
418
434
|
if (options.makeError) throw options.makeError(message);
|
|
419
435
|
throw new AdminRefusalError(adminRefusal("viewing-as", { "viewing-as": { message } }), { cause: error });
|
|
420
436
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* entry point stays free of any Better Auth import and safe for client components; the client plugin is
|
|
4
4
|
* `@connextar/house/auth/step-up/client`.
|
|
5
5
|
*/
|
|
6
|
-
export { stepUp, type PasskeyAddedData, type StepUpOptions } from "./step-up.js";
|
|
6
|
+
export { stepUp, type PasskeyAddedData, type StepUpMessages, type StepUpOptions } from "./step-up.js";
|
|
7
7
|
export {
|
|
8
8
|
DEFAULT_STEP_UP_MAX_ATTEMPTS,
|
|
9
9
|
DEFAULT_STEP_UP_WINDOW_SECONDS,
|
package/src/auth/step-up.ts
CHANGED
|
@@ -77,6 +77,24 @@ export interface StepUpOptions {
|
|
|
77
77
|
* passkey stays. With Better Auth's `advanced.backgroundTasks` set, it runs in the background.
|
|
78
78
|
*/
|
|
79
79
|
onPasskeyAdded?: (data: PasskeyAddedData) => Promise<void> | void;
|
|
80
|
+
/**
|
|
81
|
+
* The refusals' words, where the app's differ. Each replaces one sentence; the codes stay the same, so the app's
|
|
82
|
+
* client still tells them apart by `code`.
|
|
83
|
+
*/
|
|
84
|
+
messages?: StepUpMessages;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface StepUpMessages {
|
|
88
|
+
/** A passkey route refused for want of a recent confirmation. Default "Confirm it's you before adding a passkey." */
|
|
89
|
+
requiredForPasskey?: string;
|
|
90
|
+
/** Any other guarded route refused for want of one. */
|
|
91
|
+
required?: string;
|
|
92
|
+
/** Too many wrong codes and passwords this session. */
|
|
93
|
+
locked?: string;
|
|
94
|
+
/** A password offered by an account that confirms with its code. */
|
|
95
|
+
useCode?: string;
|
|
96
|
+
/** A password offered by an account that has none. */
|
|
97
|
+
noPassword?: string;
|
|
80
98
|
}
|
|
81
99
|
|
|
82
100
|
export interface PasskeyAddedData {
|
|
@@ -133,6 +151,10 @@ export function stepUp(options: StepUpOptions = {}) {
|
|
|
133
151
|
const routes = new Set<string>(options.routes ?? STEP_UP_PASSKEY_ROUTES);
|
|
134
152
|
const passkeyRoutes: readonly string[] = STEP_UP_PASSKEY_ROUTES;
|
|
135
153
|
const codeRoutes: readonly string[] = STEP_UP_CODE_ROUTES;
|
|
154
|
+
const said: StepUpMessages = Object.fromEntries(
|
|
155
|
+
Object.entries(options.messages ?? {}).map(([key, value]) => [key, value?.trim() || undefined]),
|
|
156
|
+
);
|
|
157
|
+
const lockedWords = said.locked ? { message: said.locked } : {};
|
|
136
158
|
|
|
137
159
|
if (!Number.isFinite(windowSeconds) || windowSeconds <= 0) {
|
|
138
160
|
throw new Error(`[step-up] windowSeconds must be a positive number of seconds, got ${String(windowSeconds)}.`);
|
|
@@ -243,8 +265,8 @@ export function stepUp(options: StepUpOptions = {}) {
|
|
|
243
265
|
throw new APIError("FORBIDDEN", {
|
|
244
266
|
code: STEP_UP_ERROR_CODES.STEP_UP_REQUIRED.code,
|
|
245
267
|
message: passkeyRoutes.includes(ctx.path)
|
|
246
|
-
? STEP_UP_ERROR_CODES.STEP_UP_REQUIRED.message
|
|
247
|
-
: STEP_UP_REQUIRED_GENERIC_MESSAGE,
|
|
268
|
+
? said.requiredForPasskey || STEP_UP_ERROR_CODES.STEP_UP_REQUIRED.message
|
|
269
|
+
: said.required || STEP_UP_REQUIRED_GENERIC_MESSAGE,
|
|
248
270
|
needs: status.needs,
|
|
249
271
|
});
|
|
250
272
|
}),
|
|
@@ -257,7 +279,7 @@ export function stepUp(options: StepUpOptions = {}) {
|
|
|
257
279
|
// No live session: a sign-in finishing with its code. Two-factor off: enrolment. Neither confirms anything.
|
|
258
280
|
if (!live || live.user.twoFactorEnabled !== true) return;
|
|
259
281
|
if ((await attemptsUsed(ctx.context, live.session.id)) >= maxAttempts) {
|
|
260
|
-
throw new APIError("FORBIDDEN", { ...STEP_UP_ERROR_CODES.STEP_UP_LOCKED, needs: "code" });
|
|
282
|
+
throw new APIError("FORBIDDEN", { ...STEP_UP_ERROR_CODES.STEP_UP_LOCKED, ...lockedWords, needs: "code" });
|
|
261
283
|
}
|
|
262
284
|
pending.set(ctx.context, { session: live.session });
|
|
263
285
|
}),
|
|
@@ -394,13 +416,13 @@ export function stepUp(options: StepUpOptions = {}) {
|
|
|
394
416
|
code: STEP_UP_ERROR_CODES.STEP_UP_PASSWORD_NOT_ACCEPTED.code,
|
|
395
417
|
message:
|
|
396
418
|
needs === "code"
|
|
397
|
-
? "Enter the code from your authenticator app instead."
|
|
398
|
-
: "This account has no password. Sign in again to confirm it's you.",
|
|
419
|
+
? said.useCode || "Enter the code from your authenticator app instead."
|
|
420
|
+
: said.noPassword || "This account has no password. Sign in again to confirm it's you.",
|
|
399
421
|
needs,
|
|
400
422
|
});
|
|
401
423
|
}
|
|
402
424
|
if ((await attemptsUsed(ctx.context, live.session.id)) >= maxAttempts) {
|
|
403
|
-
throw new APIError("FORBIDDEN", { ...STEP_UP_ERROR_CODES.STEP_UP_LOCKED, needs });
|
|
425
|
+
throw new APIError("FORBIDDEN", { ...STEP_UP_ERROR_CODES.STEP_UP_LOCKED, ...lockedWords, needs });
|
|
404
426
|
}
|
|
405
427
|
try {
|
|
406
428
|
// Better Auth's own check: the credential account's hash, its configured verifier, a constant-time
|
package/src/whats-new/index.ts
CHANGED
|
@@ -40,4 +40,4 @@ export {
|
|
|
40
40
|
type WhatsNewLinkProps,
|
|
41
41
|
type WhatsNewTitleProps,
|
|
42
42
|
} from "./body.js";
|
|
43
|
-
export { useWhatsNew, type UseWhatsNewOptions, type WhatsNew } from "./use-whats-new.js";
|
|
43
|
+
export { WHATS_NEW_SAVE_LABELS, useWhatsNew, type UseWhatsNewOptions, type WhatsNew } from "./use-whats-new.js";
|
|
@@ -16,8 +16,15 @@ export interface UseWhatsNewOptions {
|
|
|
16
16
|
markSeen: (version: string) => Promise<boolean>;
|
|
17
17
|
/** Somebody viewing as this person: never opens, never stores. */
|
|
18
18
|
readOnly?: boolean;
|
|
19
|
+
/** What the person is told when storing fails, where the app's words differ. */
|
|
20
|
+
labels?: Partial<typeof WHATS_NEW_SAVE_LABELS>;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
export const WHATS_NEW_SAVE_LABELS = {
|
|
24
|
+
saveFailedTitle: "Couldn't save that you've seen what's new",
|
|
25
|
+
saveFailedText: "It may show again next time you visit.",
|
|
26
|
+
};
|
|
27
|
+
|
|
21
28
|
export interface WhatsNew {
|
|
22
29
|
open: boolean;
|
|
23
30
|
/** For the dialog's `onOpenChange`. Closing it any way — Escape, a click outside — counts as seen. */
|
|
@@ -40,7 +47,7 @@ export interface WhatsNew {
|
|
|
40
47
|
* through the app's `ActionFeedbackProvider`, and the dialog stays closed for
|
|
41
48
|
* this visit rather than coming back to ask again.
|
|
42
49
|
*/
|
|
43
|
-
export function useWhatsNew({ initial, markSeen, readOnly = false }: UseWhatsNewOptions): WhatsNew {
|
|
50
|
+
export function useWhatsNew({ initial, markSeen, readOnly = false, labels }: UseWhatsNewOptions): WhatsNew {
|
|
44
51
|
const report = useActionReporter();
|
|
45
52
|
const [open, setOpenState] = React.useState(false);
|
|
46
53
|
const isOpen = React.useRef(false);
|
|
@@ -48,13 +55,13 @@ export function useWhatsNew({ initial, markSeen, readOnly = false }: UseWhatsNew
|
|
|
48
55
|
const stored = React.useRef(false);
|
|
49
56
|
|
|
50
57
|
// Read through a ref so the callbacks below keep one identity across renders.
|
|
51
|
-
const latest = React.useRef({ initial, markSeen, readOnly, report });
|
|
58
|
+
const latest = React.useRef({ initial, markSeen, readOnly, report, labels });
|
|
52
59
|
React.useEffect(() => {
|
|
53
|
-
latest.current = { initial, markSeen, readOnly, report };
|
|
60
|
+
latest.current = { initial, markSeen, readOnly, report, labels };
|
|
54
61
|
});
|
|
55
62
|
|
|
56
63
|
const store = React.useCallback(async (quietly: boolean) => {
|
|
57
|
-
const { initial: data, markSeen: save, readOnly: viewingAs, report: tell } = latest.current;
|
|
64
|
+
const { initial: data, markSeen: save, readOnly: viewingAs, report: tell, labels: own } = latest.current;
|
|
58
65
|
if (!data || viewingAs || stored.current) return;
|
|
59
66
|
stored.current = true;
|
|
60
67
|
let saved = false;
|
|
@@ -69,8 +76,8 @@ export function useWhatsNew({ initial, markSeen, readOnly = false }: UseWhatsNew
|
|
|
69
76
|
if (quietly) return;
|
|
70
77
|
tell({
|
|
71
78
|
tone: "error",
|
|
72
|
-
title:
|
|
73
|
-
description:
|
|
79
|
+
title: own?.saveFailedTitle || WHATS_NEW_SAVE_LABELS.saveFailedTitle,
|
|
80
|
+
description: own?.saveFailedText || WHATS_NEW_SAVE_LABELS.saveFailedText,
|
|
74
81
|
});
|
|
75
82
|
}, []);
|
|
76
83
|
|
package/src/words.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The house's wording convention: a default sentence that names something only the app knows — its name, the
|
|
3
|
+
* place its settings live, what its records are called — is a template with `{placeholders}`, and the app fills
|
|
4
|
+
* them. An app never rewrites the house's text after the fact: Quickie matched the view-as refusal with a regex,
|
|
5
|
+
* and FB Web re-threw the second-factor refusal in its own words, and either would have stopped working, silently,
|
|
6
|
+
* at the first changed character.
|
|
7
|
+
*
|
|
8
|
+
* `{name}` is replaced when `values` has `name`; a placeholder with no value is left as written, so a missing word
|
|
9
|
+
* shows up in a test rather than as an empty gap in a sentence.
|
|
10
|
+
*/
|
|
11
|
+
export function fillWords(template: string, values: Readonly<Record<string, string | null | undefined>>): string {
|
|
12
|
+
return template.replace(/\{([a-zA-Z][a-zA-Z0-9]*)\}/g, (whole, key: string) => {
|
|
13
|
+
const value = values[key];
|
|
14
|
+
return typeof value === "string" ? value : whole;
|
|
15
|
+
});
|
|
16
|
+
}
|