@molecule/app-settings-panel-angular 0.0.0-stage → 0.0.1-stage
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/LICENSE +202 -0
- package/README.md +1992 -2
- package/dist/account-section.component.d.ts +123 -0
- package/dist/account-section.component.d.ts.map +1 -0
- package/dist/account-section.component.js +350 -0
- package/dist/account-section.component.js.map +1 -0
- package/dist/appearance-section.component.d.ts +52 -0
- package/dist/appearance-section.component.d.ts.map +1 -0
- package/dist/appearance-section.component.js +128 -0
- package/dist/appearance-section.component.js.map +1 -0
- package/dist/auth-section.component.d.ts +125 -0
- package/dist/auth-section.component.d.ts.map +1 -0
- package/dist/auth-section.component.js +667 -0
- package/dist/auth-section.component.js.map +1 -0
- package/dist/billing-section.component.d.ts +52 -0
- package/dist/billing-section.component.d.ts.map +1 -0
- package/dist/billing-section.component.js +137 -0
- package/dist/billing-section.component.js.map +1 -0
- package/dist/context.d.ts +46 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +73 -0
- package/dist/context.js.map +1 -0
- package/dist/controlled-switch.d.ts +38 -0
- package/dist/controlled-switch.d.ts.map +1 -0
- package/dist/controlled-switch.js +56 -0
- package/dist/controlled-switch.js.map +1 -0
- package/dist/devices-section.component.d.ts +88 -0
- package/dist/devices-section.component.d.ts.map +1 -0
- package/dist/devices-section.component.js +374 -0
- package/dist/devices-section.component.js.map +1 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/dist/log-out-delete-section.component.d.ts +50 -0
- package/dist/log-out-delete-section.component.d.ts.map +1 -0
- package/dist/log-out-delete-section.component.js +249 -0
- package/dist/log-out-delete-section.component.js.map +1 -0
- package/dist/notifications-section.component.d.ts +78 -0
- package/dist/notifications-section.component.d.ts.map +1 -0
- package/dist/notifications-section.component.js +256 -0
- package/dist/notifications-section.component.js.map +1 -0
- package/dist/push-toggle.d.ts +12 -0
- package/dist/push-toggle.d.ts.map +1 -0
- package/dist/push-toggle.js +11 -0
- package/dist/push-toggle.js.map +1 -0
- package/dist/settings-container.component.d.ts +33 -0
- package/dist/settings-container.component.d.ts.map +1 -0
- package/dist/settings-container.component.js +64 -0
- package/dist/settings-container.component.js.map +1 -0
- package/dist/this-device-section.component.d.ts +40 -0
- package/dist/this-device-section.component.d.ts.map +1 -0
- package/dist/this-device-section.component.js +149 -0
- package/dist/this-device-section.component.js.map +1 -0
- package/dist/tiers-upgrade-section.component.d.ts +89 -0
- package/dist/tiers-upgrade-section.component.d.ts.map +1 -0
- package/dist/tiers-upgrade-section.component.js +407 -0
- package/dist/tiers-upgrade-section.component.js.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +78 -4
package/README.md
CHANGED
|
@@ -1,3 +1,1993 @@
|
|
|
1
|
-
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-11-14T15:07:22.379Z
|
|
7
|
+
-->
|
|
2
8
|
|
|
3
|
-
|
|
9
|
+
# @molecule/app-settings-panel-angular
|
|
10
|
+
|
|
11
|
+
`@molecule/app-settings-panel-angular` — the Angular counterpart of
|
|
12
|
+
`@molecule/app-settings-panel-react`: the same runtime export names, the
|
|
13
|
+
same props, and the same DOM for the same input. A composable,
|
|
14
|
+
batteries-included settings panel.
|
|
15
|
+
|
|
16
|
+
`<SettingsContainer [onClose]="…">` owns the layout and publishes `onClose`
|
|
17
|
+
via the settings-panel context; each section component is standalone and
|
|
18
|
+
loads its own data through the binding's hooks:
|
|
19
|
+
|
|
20
|
+
- `AccountSection` (as `<section mol-settings-account>`) — name/email edit
|
|
21
|
+
(`PATCH /api/users/:id` over the auth client's profile transport).
|
|
22
|
+
- `AppearanceSection` — dark-mode toggle (`useTheme()`).
|
|
23
|
+
- `AuthSection` — password change + TOTP two-factor
|
|
24
|
+
(`POST /api/users/:id/verify-two-factor`).
|
|
25
|
+
- `NotificationsSection` — web-push toggle (see the exported
|
|
26
|
+
`enablePushOnCurrentDevice` / `disablePushOnCurrentDevice` helpers and
|
|
27
|
+
their documented device-row contract).
|
|
28
|
+
- `BillingSection` — read-only plan display (`GET /api/billing/status`),
|
|
29
|
+
or `TiersUpgradeSection` — full Stripe upgrade/cancel flow
|
|
30
|
+
(`/api/billing/tiers|checkout|cancel`). Use one or the other, not both.
|
|
31
|
+
- `DevicesSection` / `ThisDeviceSection` — device list + current device
|
|
32
|
+
(`GET/DELETE /api/devices`).
|
|
33
|
+
- `LogOutDeleteSection` — sign out + delete account
|
|
34
|
+
(`DELETE /api/users/:id`).
|
|
35
|
+
|
|
36
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
37
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
38
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
39
|
+
|
|
40
|
+
## Quick Start
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { Component } from '@angular/core'
|
|
44
|
+
import {
|
|
45
|
+
AccountSection,
|
|
46
|
+
AppearanceSection,
|
|
47
|
+
AuthSection,
|
|
48
|
+
BillingSection,
|
|
49
|
+
DevicesSection,
|
|
50
|
+
LogOutDeleteSection,
|
|
51
|
+
NotificationsSection,
|
|
52
|
+
SettingsContainer,
|
|
53
|
+
ThisDeviceSection,
|
|
54
|
+
} from '@molecule/app-settings-panel-angular'
|
|
55
|
+
|
|
56
|
+
@Component({
|
|
57
|
+
selector: 'app-settings-panel',
|
|
58
|
+
standalone: true,
|
|
59
|
+
imports: [
|
|
60
|
+
AccountSection,
|
|
61
|
+
AppearanceSection,
|
|
62
|
+
AuthSection,
|
|
63
|
+
BillingSection,
|
|
64
|
+
DevicesSection,
|
|
65
|
+
LogOutDeleteSection,
|
|
66
|
+
NotificationsSection,
|
|
67
|
+
SettingsContainer,
|
|
68
|
+
ThisDeviceSection,
|
|
69
|
+
],
|
|
70
|
+
template: `
|
|
71
|
+
<!-- Pick sections, order them, and interleave custom ones. -->
|
|
72
|
+
<div mol-settings-container [onClose]="close">
|
|
73
|
+
<section mol-settings-account></section>
|
|
74
|
+
<section mol-settings-appearance></section>
|
|
75
|
+
<mol-auth-section></mol-auth-section>
|
|
76
|
+
<section mol-settings-notifications></section>
|
|
77
|
+
<section mol-settings-billing></section>
|
|
78
|
+
<section mol-settings-devices></section>
|
|
79
|
+
<section mol-settings-this-device></section>
|
|
80
|
+
<mol-log-out-delete-section></mol-log-out-delete-section>
|
|
81
|
+
</div>
|
|
82
|
+
`,
|
|
83
|
+
})
|
|
84
|
+
export class SettingsPanel {
|
|
85
|
+
close(): void {
|
|
86
|
+
// dismiss the drawer / panel hosting the settings
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Type
|
|
92
|
+
|
|
93
|
+
`feature`
|
|
94
|
+
|
|
95
|
+
## Installation
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
npm install @molecule/app-settings-panel-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-auth @molecule/app-http @molecule/app-push @molecule/app-ui @molecule/app-ui-angular @molecule/app-utilities
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## API
|
|
102
|
+
|
|
103
|
+
### Interfaces
|
|
104
|
+
|
|
105
|
+
#### `SettingsPanelContextValue`
|
|
106
|
+
|
|
107
|
+
Context published by `<SettingsContainer>` to its children (the same shape
|
|
108
|
+
React's `SettingsPanelContext` carries).
|
|
109
|
+
|
|
110
|
+
The container owns the dismiss-the-panel handler; sub-components that need
|
|
111
|
+
to close after an action (logout, delete account) read it from the context
|
|
112
|
+
rather than threading the prop down.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
interface SettingsPanelContextValue {
|
|
116
|
+
/** Closes the settings panel (the container's `onClose` prop). */
|
|
117
|
+
onClose: () => void
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### `Device`
|
|
122
|
+
|
|
123
|
+
A user's registered device (subset rendered in the settings list) — the
|
|
124
|
+
same shape React's `DevicesSection` renders.
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
interface Device {
|
|
128
|
+
/** The device row's id. */
|
|
129
|
+
id: string
|
|
130
|
+
/** Display name (blank rows fall back to the platform). */
|
|
131
|
+
name: string
|
|
132
|
+
/** Platform label. */
|
|
133
|
+
platform: string
|
|
134
|
+
/** Human-readable last-seen text, when the API supplies one. */
|
|
135
|
+
lastSeen?: string
|
|
136
|
+
/** `true` for the device making the current request (the API marks it). */
|
|
137
|
+
isCurrent?: boolean
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### `TiersUpgradeSectionProps`
|
|
142
|
+
|
|
143
|
+
Props for `TiersUpgradeSection` — the same props
|
|
144
|
+
`@molecule/app-settings-panel-react` accepts.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
interface TiersUpgradeSectionProps {
|
|
148
|
+
/**
|
|
149
|
+
* How the page leaves for the checkout URL the server returns. Defaults to a
|
|
150
|
+
* full-page navigation (`openUrl` from `@molecule/app-utilities`).
|
|
151
|
+
*/
|
|
152
|
+
redirectToCheckout?: (checkoutUrl: string) => void
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### `PushToggleHttp`
|
|
157
|
+
|
|
158
|
+
Minimal structural slice of `@molecule/app-http`'s HttpClient used here.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
interface PushToggleHttp {
|
|
162
|
+
get<T = unknown>(url: string): Promise<{ data: T }>
|
|
163
|
+
patch<T = unknown>(url: string, data?: unknown): Promise<{ data: T }>
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
#### `PushToggleDevice`
|
|
168
|
+
|
|
169
|
+
Device row slice returned by `GET /api/devices`.
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
interface PushToggleDevice {
|
|
173
|
+
id: string
|
|
174
|
+
isCurrent?: boolean
|
|
175
|
+
hasPushSubscription?: boolean
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### `PushToggleToken`
|
|
180
|
+
|
|
181
|
+
Push token slice returned by `@molecule/app-push` `register()`.
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
interface PushToggleToken {
|
|
185
|
+
value: string
|
|
186
|
+
platform: 'web' | 'ios' | 'android'
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Types
|
|
191
|
+
|
|
192
|
+
#### `PushToggleFailureReason`
|
|
193
|
+
|
|
194
|
+
Why an enable/disable attempt failed (mapped to i18n by the component).
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
type PushToggleFailureReason =
|
|
198
|
+
'permission-denied' | 'server-unconfigured' | 'register-failed' | 'persist-failed'
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### `PushToggleResult`
|
|
202
|
+
|
|
203
|
+
Result of an enable/disable attempt.
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
type PushToggleResult =
|
|
207
|
+
{ ok: true } | { ok: false; reason: PushToggleFailureReason; message?: string }
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Classes
|
|
211
|
+
|
|
212
|
+
#### `AccountSection`
|
|
213
|
+
|
|
214
|
+
AccountSection component.
|
|
215
|
+
|
|
216
|
+
Account fields over the shared profile lifecycle. Background validation uses
|
|
217
|
+
the auth client's configured route, mapping and session ownership guards.
|
|
218
|
+
|
|
219
|
+
`UserProfile.email` is optional (phone/handle accounts have none), so email
|
|
220
|
+
is validated only when non-empty and a blank email is never sent: clearing
|
|
221
|
+
the field leaves the stored address untouched, and a name-only account can
|
|
222
|
+
still save its name.
|
|
223
|
+
|
|
224
|
+
The host IS the `<section>` React renders. Blur-to-save is wired on the
|
|
225
|
+
`<form>` (`focusout` bubbles from the inputs, which the Angular `Input`
|
|
226
|
+
component does not re-emit) and dispatches on the blurred control's id —
|
|
227
|
+
exactly React's per-input `saveOnBlur`.
|
|
228
|
+
|
|
229
|
+
The email input's `aria-invalid` / `aria-describedby` are synced onto its
|
|
230
|
+
native control by an effect, the way React's rest props do
|
|
231
|
+
(`aria-invalid={emailError ? …} : undefined` — absent when valid, so the
|
|
232
|
+
attribute must be REMOVED, which the UI `Input`'s own always-on binding
|
|
233
|
+
cannot express); this mirrors what the UI package's `FormField` does when it
|
|
234
|
+
patches a control directly.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
@Component({
|
|
238
|
+
selector: 'section[mol-settings-account]',
|
|
239
|
+
standalone: true,
|
|
240
|
+
imports: [Flex, Input, Spinner],
|
|
241
|
+
template: `<div mol-flex [align]="'center'" [gap]="'sm'" [className]="cm.sp('mb', 3)">
|
|
242
|
+
<h3 [class]="cm.sectionHeading">
|
|
243
|
+
{{ t('settings.account', undefined, { defaultValue: 'Account' }) }}
|
|
244
|
+
</h3>
|
|
245
|
+
@if (profile.isSubmitting()) {
|
|
246
|
+
<div mol-spinner [size]="'xs'"></div>
|
|
247
|
+
}
|
|
248
|
+
</div>
|
|
249
|
+
<form [class]="cm.stack(3)" (submit)="submitForm($event)" (focusout)="saveOnFormBlur($event)">
|
|
250
|
+
<div>
|
|
251
|
+
<label for="settings-name" [class]="cm.formLabelSmall">{{
|
|
252
|
+
t('settings.name', undefined, { defaultValue: 'Name' })
|
|
253
|
+
}}</label>
|
|
254
|
+
<div
|
|
255
|
+
mol-input
|
|
256
|
+
#nameField
|
|
257
|
+
[id]="'settings-name'"
|
|
258
|
+
type="text"
|
|
259
|
+
autocomplete="name"
|
|
260
|
+
[size]="'sm'"
|
|
261
|
+
[value]="nameValue()"
|
|
262
|
+
[name]="'name'"
|
|
263
|
+
(change)="nameChange($event)"
|
|
264
|
+
></div>
|
|
265
|
+
</div>
|
|
266
|
+
<div>
|
|
267
|
+
<label for="settings-email" [class]="cm.formLabelSmall">{{
|
|
268
|
+
t('settings.email', undefined, { defaultValue: 'Email' })
|
|
269
|
+
}}</label>
|
|
270
|
+
<div
|
|
271
|
+
mol-input
|
|
272
|
+
#emailField
|
|
273
|
+
[id]="'settings-email'"
|
|
274
|
+
type="email"
|
|
275
|
+
autocomplete="email"
|
|
276
|
+
[size]="'sm'"
|
|
277
|
+
[value]="emailValue()"
|
|
278
|
+
[name]="'email'"
|
|
279
|
+
(change)="emailChange($event)"
|
|
280
|
+
></div>
|
|
281
|
+
@if (emailError(); as fieldError) {
|
|
282
|
+
<p
|
|
283
|
+
id="settings-email-error"
|
|
284
|
+
role="alert"
|
|
285
|
+
[class]="cm.cn(cm.textSize('xs'), cm.textError, cm.sp('mt', 1))"
|
|
286
|
+
>
|
|
287
|
+
{{ fieldError }}
|
|
288
|
+
</p>
|
|
289
|
+
}
|
|
290
|
+
@if (saveError(); as submitError) {
|
|
291
|
+
<p role="alert" [class]="cm.cn(cm.textSize('xs'), cm.textError, cm.sp('mt', 1))">
|
|
292
|
+
{{ submitError }}
|
|
293
|
+
</p>
|
|
294
|
+
}
|
|
295
|
+
</div>
|
|
296
|
+
</form>`,
|
|
297
|
+
})
|
|
298
|
+
export class AccountSection implements AfterViewInit {
|
|
299
|
+
/** The account fields over the shared profile lifecycle. */
|
|
300
|
+
protected readonly profile = useProfileForm<UserProfile, AccountValues>({
|
|
301
|
+
defaultValues: { name: '', email: '' },
|
|
302
|
+
toValues: (user) => ({ name: user.name ?? '', email: user.email ?? '' }),
|
|
303
|
+
toPatch: ({ email, ...changes }) => (email ? { ...changes, email } : changes),
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
/** The name field's registration (its onChange marks the field edited). */
|
|
307
|
+
protected readonly nameReg = this.profile.register('name')
|
|
308
|
+
/** The email field's registration, with the email format validation. */
|
|
309
|
+
protected readonly emailReg = this.profile.register('email', { email: true })
|
|
310
|
+
|
|
311
|
+
/** Reactive translation, read in the injection context. */
|
|
312
|
+
protected readonly i18n = useTranslation()
|
|
313
|
+
|
|
314
|
+
private readonly authClient = useAuthClient<UserProfile>()
|
|
315
|
+
|
|
316
|
+
/** The name field's host (the native control is looked up inside it). */
|
|
317
|
+
@ViewChild('nameField', { read: ElementRef }) nameField?: ElementRef<HTMLElement>
|
|
318
|
+
/** The email field's host. */
|
|
319
|
+
@ViewChild('emailField', { read: ElementRef }) emailField?: ElementRef<HTMLElement>
|
|
320
|
+
|
|
321
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
322
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
323
|
+
|
|
324
|
+
/** The translation function for the template. */
|
|
325
|
+
protected t = this.i18n.t
|
|
326
|
+
|
|
327
|
+
/** The name field's controlled value (form state is the change signal). */
|
|
328
|
+
protected readonly nameValue = computed(() => {
|
|
329
|
+
void this.profile.formState()
|
|
330
|
+
return this.profile.getValue('name')
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
/** The email field's controlled value. */
|
|
334
|
+
protected readonly emailValue = computed(() => {
|
|
335
|
+
void this.profile.formState()
|
|
336
|
+
return this.profile.getValue('email')
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
/** The email field's validation message, or null when valid. */
|
|
340
|
+
protected readonly emailError = computed(() => {
|
|
341
|
+
void this.profile.formState()
|
|
342
|
+
return this.profile.getError('email') ?? null
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
/** The latest save failure's display text, or null. */
|
|
346
|
+
protected readonly saveError = computed((): string | null => {
|
|
347
|
+
const error = this.profile.error()
|
|
348
|
+
if (!error) return null
|
|
349
|
+
return error instanceof Error
|
|
350
|
+
? error.message
|
|
351
|
+
: this.t('settings.failedToUpdateProfile', undefined, {
|
|
352
|
+
defaultValue: 'Failed to update profile',
|
|
353
|
+
})
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
/** The cached native email control (re-resolved when the view is rebuilt). */
|
|
357
|
+
private emailControl: HTMLInputElement | null = null
|
|
358
|
+
|
|
359
|
+
/** Starts the auth client's background session validation (React's mount effect) and wires the email control's aria sync. */
|
|
360
|
+
constructor() {
|
|
361
|
+
// A provider's background validation must not cause an unhandled
|
|
362
|
+
// rejection: render its auth state while useProfileForm reports mutation
|
|
363
|
+
// failures.
|
|
364
|
+
void this.authClient.validateSession?.().catch((_error) => {})
|
|
365
|
+
// React's rest props on the email input: present with the error target
|
|
366
|
+
// while the field is invalid, REMOVED while valid. An effect (view hooks
|
|
367
|
+
// are skipped by signal-driven refreshes) keeps it exact.
|
|
368
|
+
effect(() => {
|
|
369
|
+
const error = this.emailError()
|
|
370
|
+
untracked(() => this.syncEmailAria(error))
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Applies the initial rest-prop state (hooks run on the mount passes). */
|
|
375
|
+
ngAfterViewInit(): void {
|
|
376
|
+
const name = this.nameField?.nativeElement.querySelector('input')
|
|
377
|
+
if (name) this.nameReg.ref?.(name)
|
|
378
|
+
this.resolveEmailControl()
|
|
379
|
+
this.syncEmailAria(this.emailError())
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Forwards the field's `change` (React's text-input onChange). */
|
|
383
|
+
protected nameChange(event: Event): void {
|
|
384
|
+
this.nameReg.onChange(event)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Forwards the field's `change` (React's text-input onChange). */
|
|
388
|
+
protected emailChange(event: Event): void {
|
|
389
|
+
this.emailReg.onChange(event)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Submits the form (React's `profile.formProps` onSubmit). */
|
|
393
|
+
protected submitForm(event: Event): void {
|
|
394
|
+
this.profile.formProps.onSubmit(event)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Saves on blur, exactly React's per-input `saveOnBlur`: the field's registration blur first (touch + field validation), then the profile submit. */
|
|
398
|
+
protected saveOnFormBlur(event: FocusEvent): void {
|
|
399
|
+
const id = (event.target as HTMLElement | null)?.id
|
|
400
|
+
if (id === 'settings-name') this.saveOnBlur(this.nameReg)
|
|
401
|
+
else if (id === 'settings-email') this.saveOnBlur(this.emailReg)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** React's `saveOnBlur` wrapper: one blur, then one submit. */
|
|
405
|
+
private saveOnBlur(registration: { onBlur: () => void }): void {
|
|
406
|
+
registration.onBlur()
|
|
407
|
+
void this.profile.submit()
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Looks the native email control up inside the UI `Input`'s host and registers it as the field ref (React's spread `ref`, so a failed submit focuses the first in… */
|
|
411
|
+
private resolveEmailControl(): void {
|
|
412
|
+
this.emailControl =
|
|
413
|
+
(this.emailField?.nativeElement.querySelector('input') as HTMLInputElement | null) ?? null
|
|
414
|
+
if (this.emailControl) this.emailReg.ref?.(this.emailControl)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Applies React's rest-prop state for one error value: `aria-invalid` and `aria-describedby` on the native control while invalid, removed while valid. */
|
|
418
|
+
private syncEmailAria(error: string | null): void {
|
|
419
|
+
if (!this.emailControl || !this.emailControl.isConnected) this.resolveEmailControl()
|
|
420
|
+
const control = this.emailControl
|
|
421
|
+
if (!control) return
|
|
422
|
+
if (error) {
|
|
423
|
+
control.setAttribute('aria-invalid', 'true')
|
|
424
|
+
control.setAttribute('aria-describedby', 'settings-email-error')
|
|
425
|
+
} else {
|
|
426
|
+
control.removeAttribute('aria-invalid')
|
|
427
|
+
control.removeAttribute('aria-describedby')
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
_(Member comments are shortened to their first sentence to fit; the full declaration is in `node_modules/@molecule/app-settings-panel-angular/dist/account-section.component.d.ts`.)_
|
|
434
|
+
|
|
435
|
+
#### `AppearanceSection`
|
|
436
|
+
|
|
437
|
+
AppearanceSection component.
|
|
438
|
+
|
|
439
|
+
Appearance section — dark-mode toggle wired to the theme bond. The host IS
|
|
440
|
+
the `<section>` React renders.
|
|
441
|
+
|
|
442
|
+
Apps that expose other appearance knobs (font size, density, etc.) can
|
|
443
|
+
either add more sub-components to this package or render their own section
|
|
444
|
+
in line with this one.
|
|
445
|
+
|
|
446
|
+
The `id` / `aria-label` rest props of React's `<Switch>` are host
|
|
447
|
+
attributes here: the UI `Switch` moves host attributes onto its visible
|
|
448
|
+
trigger on every check, so the label follows the locale the same way. The
|
|
449
|
+
controlled `checked` is re-synced by an effect (React re-renders the hidden
|
|
450
|
+
checkbox's property; see {@link ControlledSwitchSync}).
|
|
451
|
+
|
|
452
|
+
```typescript
|
|
453
|
+
@Component({
|
|
454
|
+
selector: 'section[mol-settings-appearance]',
|
|
455
|
+
standalone: true,
|
|
456
|
+
imports: [Flex, Switch],
|
|
457
|
+
template: `<h3 [class]="cm.cn(cm.sectionHeading, cm.sp('mb', 3))">
|
|
458
|
+
{{ t('settings.appearance', undefined, { defaultValue: 'Appearance' }) }}
|
|
459
|
+
</h3>
|
|
460
|
+
<div mol-flex [align]="'center'" [justify]="'between'">
|
|
461
|
+
<span [class]="cm.textSize('sm')">{{
|
|
462
|
+
t('settings.darkMode', undefined, { defaultValue: 'Dark mode' })
|
|
463
|
+
}}</span>
|
|
464
|
+
<mol-switch
|
|
465
|
+
#themeSwitch
|
|
466
|
+
id="settings-theme-toggle"
|
|
467
|
+
[attr.aria-label]="t('theme.toggle', undefined, { defaultValue: 'Toggle theme' })"
|
|
468
|
+
[checked]="isDark()"
|
|
469
|
+
(change)="theme.toggleTheme()"
|
|
470
|
+
[size]="'sm'"
|
|
471
|
+
></mol-switch>
|
|
472
|
+
</div>`,
|
|
473
|
+
})
|
|
474
|
+
export class AppearanceSection implements AfterViewInit {
|
|
475
|
+
/** The theme bond's state and actions. */
|
|
476
|
+
protected readonly theme = useTheme()
|
|
477
|
+
|
|
478
|
+
/** Reactive translation, read in the injection context. */
|
|
479
|
+
protected readonly i18n = useTranslation()
|
|
480
|
+
|
|
481
|
+
/** The translation function for the template. */
|
|
482
|
+
protected t = this.i18n.t
|
|
483
|
+
|
|
484
|
+
/** Whether the dark mode is on (React's `mode === 'dark'`). */
|
|
485
|
+
protected readonly isDark = computed(() => this.theme.mode() === 'dark')
|
|
486
|
+
|
|
487
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
488
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
489
|
+
|
|
490
|
+
/** The theme switch's host element (the hidden checkbox lives inside). */
|
|
491
|
+
@ViewChild('themeSwitch', { read: ElementRef }) themeSwitch?: ElementRef<HTMLElement>
|
|
492
|
+
|
|
493
|
+
/** The controlled-`checked` sync (created once the host exists). */
|
|
494
|
+
private sync?: ControlledSwitchSync
|
|
495
|
+
|
|
496
|
+
/** Re-syncs the switch's hidden checkbox whenever the mode changes. */
|
|
497
|
+
constructor() {
|
|
498
|
+
effect(() => {
|
|
499
|
+
const checked = this.isDark()
|
|
500
|
+
untracked(() => this.runSync(checked))
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** Captures the mount-time attribute state (hooks run on the mount passes). */
|
|
505
|
+
ngAfterViewInit(): void {
|
|
506
|
+
this.runSync(this.isDark())
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Runs the controlled-`checked` sync once the switch host exists.
|
|
511
|
+
* @param checked - The controlled value this render shows.
|
|
512
|
+
*/
|
|
513
|
+
private runSync(checked: boolean): void {
|
|
514
|
+
if (!this.sync && this.themeSwitch) {
|
|
515
|
+
this.sync = new ControlledSwitchSync(this.themeSwitch.nativeElement)
|
|
516
|
+
}
|
|
517
|
+
this.sync?.sync(checked)
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
#### `AuthSection`
|
|
523
|
+
|
|
524
|
+
AuthSection component.
|
|
525
|
+
|
|
526
|
+
Authentication section — change password (modal) + two-factor (TOTP) setup.
|
|
527
|
+
React's root is a fragment (the `<section>` plus two modals), so the host is
|
|
528
|
+
unwrapped by the DOM normalizer and `display: contents` keeps the layout
|
|
529
|
+
fragment-like; an OAuth-only user renders NOTHING (React's `return null`).
|
|
530
|
+
|
|
531
|
+
Two-factor uses the real enrollment flow against the user resource's
|
|
532
|
+
`POST /users/:id/verify-two-factor` endpoint (`@molecule/api-two-factor`):
|
|
533
|
+
|
|
534
|
+
- Enable → `{action:'setup'}` returns a QR code + secret to scan into an
|
|
535
|
+
authenticator app → user enters the 6-digit code → `{action:'enable', token}`.
|
|
536
|
+
- Disable → user enters a current code → `{action:'disable', token}`.
|
|
537
|
+
The current status is read from `/users/me`. (This replaces the previous
|
|
538
|
+
boolean toggle, which PATCHed `twoFactorEnabled` — a field the update
|
|
539
|
+
handler deliberately ignores — so it never actually enrolled 2FA.)
|
|
540
|
+
|
|
541
|
+
Auto-hides for OAuth-only users (`user.oauthServer` truthy) since
|
|
542
|
+
password / 2FA wouldn't apply. If the app's API has no two-factor
|
|
543
|
+
provider bonded, setup fails gracefully with an inline message.
|
|
544
|
+
|
|
545
|
+
`useChangePassword`'s state is a live getter over a plain object (the
|
|
546
|
+
Angular binding's `usePromise` port), so the pending/error reads are
|
|
547
|
+
mediated by a version signal the handlers bump at React's re-render points.
|
|
548
|
+
|
|
549
|
+
```typescript
|
|
550
|
+
@Component({
|
|
551
|
+
selector: 'mol-auth-section',
|
|
552
|
+
standalone: true,
|
|
553
|
+
imports: [Alert, Button, Flex, Input, Modal],
|
|
554
|
+
host: {
|
|
555
|
+
'data-parity-unwrap': '',
|
|
556
|
+
style: 'display: contents',
|
|
557
|
+
},
|
|
558
|
+
template: `@if (!isOauthOnly()) {
|
|
559
|
+
<section>
|
|
560
|
+
<h3 [class]="cm.cn(cm.sectionHeading, cm.sp('mb', 3))">
|
|
561
|
+
{{ t('settings.authentication', undefined, { defaultValue: 'Authentication' }) }}
|
|
562
|
+
</h3>
|
|
563
|
+
<div [class]="cm.stack(3)">
|
|
564
|
+
<button
|
|
565
|
+
mol-button
|
|
566
|
+
[variant]="'outline'"
|
|
567
|
+
[size]="'sm'"
|
|
568
|
+
(click)="showPasswordModal.set(true)"
|
|
569
|
+
>
|
|
570
|
+
{{ t('settings.changePassword', undefined, { defaultValue: 'Change password' }) }}
|
|
571
|
+
</button>
|
|
572
|
+
<div mol-flex [align]="'center'" [justify]="'between'">
|
|
573
|
+
<span [class]="cm.textSize('sm')">{{ t('settings.twoFactor', undefined, { defaultValue: 'Two-factor authentication' }) }}@if (twoFactorEnabled()) {<span [class]="cm.cn(cm.textSize('xs'), cm.textSubtle, cm.sp('ml', 2))">{{ t('settings.twoFactorOn', undefined, { defaultValue: 'On' }) }}</span>}</span>
|
|
574
|
+
@if (twoFactorEnabled()) {
|
|
575
|
+
<button
|
|
576
|
+
mol-button
|
|
577
|
+
[variant]="'outline'"
|
|
578
|
+
[size]="'sm'"
|
|
579
|
+
(click)="openDisableTf()"
|
|
580
|
+
data-mol-id="two-factor-disable"
|
|
581
|
+
>
|
|
582
|
+
{{ t('settings.twoFactorDisable', undefined, { defaultValue: 'Disable' }) }}
|
|
583
|
+
</button>
|
|
584
|
+
} @else {
|
|
585
|
+
<button
|
|
586
|
+
mol-button
|
|
587
|
+
[variant]="'outline'"
|
|
588
|
+
[size]="'sm'"
|
|
589
|
+
(click)="startEnable()"
|
|
590
|
+
data-mol-id="two-factor-enable"
|
|
591
|
+
>
|
|
592
|
+
{{ t('settings.twoFactorEnable', undefined, { defaultValue: 'Enable' }) }}
|
|
593
|
+
</button>
|
|
594
|
+
}
|
|
595
|
+
</div>
|
|
596
|
+
</div>
|
|
597
|
+
</section>
|
|
598
|
+
<mol-modal
|
|
599
|
+
[open]="showPasswordModal()"
|
|
600
|
+
(close)="closePasswordModal()"
|
|
601
|
+
[title]="
|
|
602
|
+
t('settings.changePasswordModal.title', undefined, { defaultValue: 'Change Password' })
|
|
603
|
+
"
|
|
604
|
+
>
|
|
605
|
+
@if (pwError(); as changeError) {
|
|
606
|
+
<div mol-alert [status]="'error'" [className]="cm.sp('mb', 4)">{{ changeError }}</div>
|
|
607
|
+
}
|
|
608
|
+
<form [class]="cm.stack(4)" (submit)="submitPassword($event)">
|
|
609
|
+
<div>
|
|
610
|
+
<label for="current-pw" [class]="cm.formLabel">{{
|
|
611
|
+
t('settings.changePasswordModal.currentPassword', undefined, {
|
|
612
|
+
defaultValue: 'Current Password',
|
|
613
|
+
})
|
|
614
|
+
}}</label>
|
|
615
|
+
<div
|
|
616
|
+
mol-input
|
|
617
|
+
[id]="'current-pw'"
|
|
618
|
+
type="password"
|
|
619
|
+
autocomplete="current-password"
|
|
620
|
+
[required]="true"
|
|
621
|
+
[value]="currentPassword()"
|
|
622
|
+
(change)="currentPassword.set($any($event.target).value)"
|
|
623
|
+
></div>
|
|
624
|
+
</div>
|
|
625
|
+
<div>
|
|
626
|
+
<label for="new-pw" [class]="cm.formLabel">{{
|
|
627
|
+
t('settings.changePasswordModal.newPassword', undefined, {
|
|
628
|
+
defaultValue: 'New Password',
|
|
629
|
+
})
|
|
630
|
+
}}</label>
|
|
631
|
+
<div
|
|
632
|
+
mol-input
|
|
633
|
+
[id]="'new-pw'"
|
|
634
|
+
type="password"
|
|
635
|
+
autocomplete="new-password"
|
|
636
|
+
[required]="true"
|
|
637
|
+
[value]="newPassword()"
|
|
638
|
+
(change)="newPassword.set($any($event.target).value)"
|
|
639
|
+
></div>
|
|
640
|
+
</div>
|
|
641
|
+
<button mol-button type="submit" [fullWidth]="true" [loading]="pwStatus() === 'pending'">
|
|
642
|
+
@if (pwStatus() === 'pending') {
|
|
643
|
+
{{
|
|
644
|
+
t('settings.changePasswordModal.changing', undefined, { defaultValue: 'Changing...' })
|
|
645
|
+
}}
|
|
646
|
+
} @else {
|
|
647
|
+
{{ t('settings.changePassword', undefined, { defaultValue: 'Change password' }) }}
|
|
648
|
+
}
|
|
649
|
+
</button>
|
|
650
|
+
</form>
|
|
651
|
+
</mol-modal>
|
|
652
|
+
<mol-modal [open]="tfModal() !== null" (close)="closeTf()" [title]="tfTitle()">
|
|
653
|
+
@if (tfError(); as tfFailure) {
|
|
654
|
+
<div mol-alert [status]="'error'" [className]="cm.sp('mb', 4)">{{ tfFailure }}</div>
|
|
655
|
+
}
|
|
656
|
+
@if (tfModal() === 'enable' && tfSetup()?.QRImageUrl) {
|
|
657
|
+
<div [class]="cm.cn(cm.stack(3), cm.sp('mb', 4))">
|
|
658
|
+
<p [class]="cm.cn(cm.textSize('sm'), cm.textMuted)">
|
|
659
|
+
{{
|
|
660
|
+
t('settings.twoFactorScan', undefined, {
|
|
661
|
+
defaultValue:
|
|
662
|
+
'Scan this QR code with your authenticator app, then enter the 6-digit code.',
|
|
663
|
+
})
|
|
664
|
+
}}
|
|
665
|
+
</p>
|
|
666
|
+
<img
|
|
667
|
+
[attr.src]="tfSetup()?.QRImageUrl"
|
|
668
|
+
[attr.alt]="
|
|
669
|
+
t('settings.twoFactorQrAlt', undefined, { defaultValue: 'Two-factor QR code' })
|
|
670
|
+
"
|
|
671
|
+
[class]="cm.cn(cm.mxAuto, cm.maxW('xs'))"
|
|
672
|
+
/>
|
|
673
|
+
@if (tfSetup()?.secret) {
|
|
674
|
+
<p [class]="cm.cn(cm.textSize('xs'), cm.textSubtle, cm.settingsPanelTwoFactorSecret)">
|
|
675
|
+
{{ tfSetup()?.secret }}
|
|
676
|
+
</p>
|
|
677
|
+
}
|
|
678
|
+
</div>
|
|
679
|
+
}
|
|
680
|
+
@if (tfModal() === 'disable' || tfSetup()) {
|
|
681
|
+
<form [class]="cm.stack(4)" (submit)="submitTfForm($event)">
|
|
682
|
+
<div>
|
|
683
|
+
<label for="tf-token" [class]="cm.formLabel">{{
|
|
684
|
+
t('settings.twoFactorCode', undefined, { defaultValue: 'Authentication code' })
|
|
685
|
+
}}</label>
|
|
686
|
+
<div
|
|
687
|
+
mol-input
|
|
688
|
+
[id]="'tf-token'"
|
|
689
|
+
type="text"
|
|
690
|
+
autocomplete="one-time-code"
|
|
691
|
+
[required]="true"
|
|
692
|
+
[value]="tfToken()"
|
|
693
|
+
(change)="tfToken.set($any($event.target).value)"
|
|
694
|
+
></div>
|
|
695
|
+
</div>
|
|
696
|
+
<button
|
|
697
|
+
mol-button
|
|
698
|
+
type="submit"
|
|
699
|
+
[color]="tfModal() === 'disable' ? 'error' : 'primary'"
|
|
700
|
+
[fullWidth]="true"
|
|
701
|
+
[loading]="tfLoading()"
|
|
702
|
+
>
|
|
703
|
+
@if (tfModal() === 'disable') {
|
|
704
|
+
{{ t('settings.twoFactorDisable', undefined, { defaultValue: 'Disable' }) }}
|
|
705
|
+
} @else {
|
|
706
|
+
{{ t('settings.twoFactorVerify', undefined, { defaultValue: 'Verify & enable' }) }}
|
|
707
|
+
}
|
|
708
|
+
</button>
|
|
709
|
+
</form>
|
|
710
|
+
}
|
|
711
|
+
</mol-modal>
|
|
712
|
+
}`,
|
|
713
|
+
})
|
|
714
|
+
export class AuthSection {
|
|
715
|
+
protected readonly auth = useAuth<UserProfile>()
|
|
716
|
+
|
|
717
|
+
protected readonly i18n = useTranslation()
|
|
718
|
+
|
|
719
|
+
protected t = this.i18n.t
|
|
720
|
+
|
|
721
|
+
private readonly http = useHttpClient()
|
|
722
|
+
|
|
723
|
+
private readonly pw = useChangePassword()
|
|
724
|
+
|
|
725
|
+
protected readonly showPasswordModal = signal(false)
|
|
726
|
+
protected readonly currentPassword = signal('')
|
|
727
|
+
protected readonly newPassword = signal('')
|
|
728
|
+
|
|
729
|
+
protected readonly twoFactorEnabled = signal(false)
|
|
730
|
+
protected readonly tfModal = signal<null | 'enable' | 'disable'>(null)
|
|
731
|
+
protected readonly tfSetup = signal<TwoFactorSetup | null>(null)
|
|
732
|
+
protected readonly tfToken = signal('')
|
|
733
|
+
protected readonly tfLoading = signal(false)
|
|
734
|
+
protected readonly tfError = signal('')
|
|
735
|
+
|
|
736
|
+
private readonly pwVersion = signal(0)
|
|
737
|
+
|
|
738
|
+
protected readonly pwStatus = computed(() => {
|
|
739
|
+
void this.pwVersion()
|
|
740
|
+
return this.pw.status
|
|
741
|
+
})
|
|
742
|
+
|
|
743
|
+
protected readonly pwError = computed((): string | null => {
|
|
744
|
+
void this.pwVersion()
|
|
745
|
+
const error = this.pw.error
|
|
746
|
+
if (!error) return null
|
|
747
|
+
return (
|
|
748
|
+
error.message ||
|
|
749
|
+
this.t('settings.changePasswordModal.error', undefined, {
|
|
750
|
+
defaultValue: 'Failed to change password.',
|
|
751
|
+
})
|
|
752
|
+
)
|
|
753
|
+
})
|
|
754
|
+
|
|
755
|
+
private readonly twoFactorRead = { cancelled: false }
|
|
756
|
+
|
|
757
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
758
|
+
|
|
759
|
+
protected isOauthOnly(): boolean {
|
|
760
|
+
return !!this.auth.user()?.oauthServer
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
protected tfTitle(): string {
|
|
764
|
+
return this.tfModal() === 'disable'
|
|
765
|
+
? this.t('settings.twoFactorDisableTitle', undefined, {
|
|
766
|
+
defaultValue: 'Disable two-factor authentication',
|
|
767
|
+
})
|
|
768
|
+
: this.t('settings.twoFactorEnableTitle', undefined, {
|
|
769
|
+
defaultValue: 'Enable two-factor authentication',
|
|
770
|
+
})
|
|
771
|
+
}
|
|
772
|
+
// … truncated — 8 more members: constructor, closePasswordModal, openDisableTf, startEnable, submitTf, submitTfForm, closeTf, submitPassword; the full declaration is in node_modules/@molecule/app-settings-panel-angular/dist/auth-section.component.d.ts
|
|
773
|
+
```
|
|
774
|
+
|
|
775
|
+
_(Member comments are left out to fit; the full declaration is in `node_modules/@molecule/app-settings-panel-angular/dist/auth-section.component.d.ts`.)_
|
|
776
|
+
|
|
777
|
+
#### `BillingSection`
|
|
778
|
+
|
|
779
|
+
BillingSection component.
|
|
780
|
+
|
|
781
|
+
Billing section — current plan + Upgrade button. The host IS the
|
|
782
|
+
`<section>` React renders.
|
|
783
|
+
|
|
784
|
+
Fetches `/api/billing/status` on mount and uses the returned plan name as a
|
|
785
|
+
more accurate label than the parent-supplied default. The `plan` input
|
|
786
|
+
remains a fallback (e.g. for offline rendering or apps that don't expose
|
|
787
|
+
`/billing/status`).
|
|
788
|
+
|
|
789
|
+
The Upgrade button navigates to `upgradeTo` (default `/settings`). Pass
|
|
790
|
+
`upgradeTo="/billing"` or `upgradeTo="/pricing"` if your app routes
|
|
791
|
+
elsewhere. Apps with a multi-tier checkout flow should use
|
|
792
|
+
`TiersUpgradeSection` instead.
|
|
793
|
+
|
|
794
|
+
```typescript
|
|
795
|
+
@Component({
|
|
796
|
+
selector: 'section[mol-settings-billing]',
|
|
797
|
+
standalone: true,
|
|
798
|
+
imports: [Button, Flex],
|
|
799
|
+
inputs: ['plan', 'upgradeTo'],
|
|
800
|
+
template: `<h3 [class]="cm.cn(cm.sectionHeading, cm.sp('mb', 3))">
|
|
801
|
+
{{ t('settings.billing', undefined, { defaultValue: 'Billing' }) }}
|
|
802
|
+
</h3>
|
|
803
|
+
<div mol-flex [align]="'center'" [justify]="'between'">
|
|
804
|
+
<span [class]="cm.textSize('sm')"
|
|
805
|
+
>{{ t('settings.plan', undefined, { defaultValue: 'Plan: ' })
|
|
806
|
+
}}<strong>{{ planLabel() }}</strong></span
|
|
807
|
+
>
|
|
808
|
+
<button mol-button [variant]="'outline'" [size]="'sm'" (click)="upgrade()">
|
|
809
|
+
{{ t('settings.upgrade', undefined, { defaultValue: 'Upgrade' }) }}
|
|
810
|
+
</button>
|
|
811
|
+
</div>`,
|
|
812
|
+
})
|
|
813
|
+
export class BillingSection {
|
|
814
|
+
/** Fallback plan name when `/api/billing/status` is unavailable. */
|
|
815
|
+
plan = 'Free'
|
|
816
|
+
/** Route the Upgrade button navigates to. */
|
|
817
|
+
upgradeTo = '/settings'
|
|
818
|
+
|
|
819
|
+
/** Reactive translation, read in the injection context. */
|
|
820
|
+
protected readonly i18n = useTranslation()
|
|
821
|
+
|
|
822
|
+
/** The translation function for the template. */
|
|
823
|
+
protected t = this.i18n.t
|
|
824
|
+
|
|
825
|
+
private readonly http = useHttpClient()
|
|
826
|
+
|
|
827
|
+
/** The binding's router (throws outside one, like react-router's useNavigate). */
|
|
828
|
+
private readonly router = useRouter()
|
|
829
|
+
|
|
830
|
+
/** The plan name loaded from `/api/billing/status`, or null. */
|
|
831
|
+
private readonly livePlan = signal<string | null>(null)
|
|
832
|
+
|
|
833
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
834
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* The displayed plan label (React's `livePlan ?? plan`).
|
|
838
|
+
* @returns The plan name.
|
|
839
|
+
*/
|
|
840
|
+
protected planLabel(): string {
|
|
841
|
+
return this.livePlan() ?? this.plan
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Reads the current plan from the bonded entitlements/billing status
|
|
846
|
+
* endpoint on mount (React's mount effect). Fire-and-forget; the
|
|
847
|
+
* input-supplied `plan` is the fallback if the fetch fails. A result that
|
|
848
|
+
* lands after unmount is dropped like React's cancelled one.
|
|
849
|
+
*/
|
|
850
|
+
constructor() {
|
|
851
|
+
let cancelled = false
|
|
852
|
+
inject(DestroyRef).onDestroy(() => {
|
|
853
|
+
cancelled = true
|
|
854
|
+
})
|
|
855
|
+
void this.http
|
|
856
|
+
.get<{ name?: string }>('/api/billing/status')
|
|
857
|
+
.then((res) => {
|
|
858
|
+
if (cancelled) return
|
|
859
|
+
const body =
|
|
860
|
+
(res as { data?: { name?: string }; name?: string }).data ?? (res as { name?: string })
|
|
861
|
+
const name = body?.name
|
|
862
|
+
if (typeof name === 'string' && name) this.livePlan.set(name)
|
|
863
|
+
})
|
|
864
|
+
.catch((_error) => {
|
|
865
|
+
// fallback to the input-supplied plan label
|
|
866
|
+
})
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/** Navigates to the upgrade route (React's `navigate(upgradeTo)`). */
|
|
870
|
+
protected upgrade(): void {
|
|
871
|
+
this.router.navigate(this.upgradeTo)
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
#### `DevicesSection`
|
|
877
|
+
|
|
878
|
+
DevicesSection component.
|
|
879
|
+
|
|
880
|
+
Devices section — lists the user's registered devices and lets them
|
|
881
|
+
revoke (sign out) any device other than the one they're currently using.
|
|
882
|
+
The host IS the `<section>` React renders.
|
|
883
|
+
|
|
884
|
+
Revoking deletes the device row (`DELETE /api/devices/:id`). The API's
|
|
885
|
+
authorization layer enforces server-side revocation: it rejects that
|
|
886
|
+
device's session the next time it makes a request (within the
|
|
887
|
+
device-exists cache TTL), so the removed device is actually signed out —
|
|
888
|
+
not just hidden from the list. The current device is labelled
|
|
889
|
+
"This device" and is not revocable here (use Sign out for the current
|
|
890
|
+
session). Recreates molecule v1's device-revocation behaviour.
|
|
891
|
+
|
|
892
|
+
Apps that want a different trailing element per row can pass a
|
|
893
|
+
`renderRowIcon` template (its context carries the device as `$implicit`
|
|
894
|
+
and `a0`, the way the conformance kit maps a React render function), which
|
|
895
|
+
then replaces the built-in revoke control; pass an empty template to
|
|
896
|
+
suppress it entirely.
|
|
897
|
+
|
|
898
|
+
```typescript
|
|
899
|
+
@Component({
|
|
900
|
+
selector: 'section[mol-settings-devices]',
|
|
901
|
+
standalone: true,
|
|
902
|
+
imports: [Button, Flex, Icon, NgTemplateOutlet, Spinner],
|
|
903
|
+
inputs: ['renderRowIcon'],
|
|
904
|
+
// prettier-ignore
|
|
905
|
+
template: `<div mol-flex [align]="'center'" [gap]="'sm'" [className]="cm.sp('mb', 3)">
|
|
906
|
+
<h3 [class]="cm.sectionHeading">
|
|
907
|
+
{{ t('settings.devices', undefined, { defaultValue: 'Devices' }) }}
|
|
908
|
+
</h3>
|
|
909
|
+
@if (loading()) {
|
|
910
|
+
<div mol-spinner [size]="'xs'"></div>
|
|
911
|
+
}
|
|
912
|
+
</div>
|
|
913
|
+
@if (devices().length > 0) {
|
|
914
|
+
<ul [class]="cm.stack(2)">
|
|
915
|
+
@for (device of devices(); track device.id) {
|
|
916
|
+
<li>
|
|
917
|
+
<div
|
|
918
|
+
mol-flex
|
|
919
|
+
[align]="'center'"
|
|
920
|
+
[justify]="'between'"
|
|
921
|
+
[className]="cm.cn(cm.textSize('sm'), cm.sp('py', 1))"
|
|
922
|
+
>
|
|
923
|
+
<span>{{ device.name || device.platform }}@if (device.isCurrent) {<span [class]="cm.cn(cm.textSize('xs'), cm.textSubtle, cm.sp('ml', 2))">{{ t('settings.thisDevice', undefined, { defaultValue: 'This Device' }) }}</span>}</span>
|
|
924
|
+
@if (renderRowIcon) {
|
|
925
|
+
<ng-container
|
|
926
|
+
[ngTemplateOutlet]="renderRowIcon"
|
|
927
|
+
[ngTemplateOutletContext]="rowContext(device)"
|
|
928
|
+
/>
|
|
929
|
+
} @else if (device.isCurrent) {
|
|
930
|
+
<svg
|
|
931
|
+
mol-icon-root
|
|
932
|
+
[name]="'chevron-right'"
|
|
933
|
+
[size]="16"
|
|
934
|
+
[className]="cm.textSubtle"
|
|
935
|
+
></svg>
|
|
936
|
+
} @else if (isConfirming(device)) {
|
|
937
|
+
<div mol-flex [align]="'center'" [gap]="'sm'">
|
|
938
|
+
<button
|
|
939
|
+
mol-button
|
|
940
|
+
[variant]="'ghost'"
|
|
941
|
+
[size]="'sm'"
|
|
942
|
+
[disabled]="isRevoking(device)"
|
|
943
|
+
(click)="confirmingId.set(null)"
|
|
944
|
+
>
|
|
945
|
+
{{ t('common.cancel', undefined, { defaultValue: 'Cancel' }) }}
|
|
946
|
+
</button>
|
|
947
|
+
<button
|
|
948
|
+
mol-button
|
|
949
|
+
[color]="'error'"
|
|
950
|
+
[size]="'sm'"
|
|
951
|
+
[loading]="isRevoking(device)"
|
|
952
|
+
(click)="revoke(device.id)"
|
|
953
|
+
data-mol-id="device-revoke-confirm"
|
|
954
|
+
>
|
|
955
|
+
{{ t('settings.signOutDevice', undefined, { defaultValue: 'Sign out' }) }}
|
|
956
|
+
</button>
|
|
957
|
+
</div>
|
|
958
|
+
} @else {
|
|
959
|
+
<button
|
|
960
|
+
mol-button
|
|
961
|
+
[variant]="'ghost'"
|
|
962
|
+
[size]="'sm'"
|
|
963
|
+
(click)="confirmingId.set(device.id)"
|
|
964
|
+
data-mol-id="device-revoke"
|
|
965
|
+
>
|
|
966
|
+
{{ t('settings.signOutDevice', undefined, { defaultValue: 'Sign out' }) }}
|
|
967
|
+
</button>
|
|
968
|
+
}
|
|
969
|
+
</div>
|
|
970
|
+
@if (isConfirming(device)) {
|
|
971
|
+
<p [class]="cm.cn(cm.textSize('xs'), cm.textMuted, cm.sp('mt', 1))">
|
|
972
|
+
{{
|
|
973
|
+
t('settings.revokeDeviceWarning', undefined, {
|
|
974
|
+
defaultValue: "This device's access will be revoked shortly. Sign it out?",
|
|
975
|
+
})
|
|
976
|
+
}}
|
|
977
|
+
</p>
|
|
978
|
+
}
|
|
979
|
+
</li>
|
|
980
|
+
}
|
|
981
|
+
</ul>
|
|
982
|
+
} @else {
|
|
983
|
+
@if (!loading()) {
|
|
984
|
+
<p [class]="cm.cn(cm.textSize('sm'), cm.textMuted)">
|
|
985
|
+
{{ t('settings.noDevices', undefined, { defaultValue: 'No devices found' }) }}
|
|
986
|
+
</p>
|
|
987
|
+
}
|
|
988
|
+
}`,
|
|
989
|
+
})
|
|
990
|
+
export class DevicesSection {
|
|
991
|
+
/**
|
|
992
|
+
* A custom trailing element per row (React's `renderRowIcon` callback).
|
|
993
|
+
* The template's context carries the device as `$implicit` / `a0`; giving
|
|
994
|
+
* one replaces the built-in revoke control.
|
|
995
|
+
*/
|
|
996
|
+
renderRowIcon?: TemplateRef<unknown> | null
|
|
997
|
+
|
|
998
|
+
/** Reactive translation, read in the injection context. */
|
|
999
|
+
protected readonly i18n = useTranslation()
|
|
1000
|
+
|
|
1001
|
+
/** The translation function for the template. */
|
|
1002
|
+
protected t = this.i18n.t
|
|
1003
|
+
|
|
1004
|
+
private readonly http = useHttpClient()
|
|
1005
|
+
|
|
1006
|
+
/** The registered devices. */
|
|
1007
|
+
protected readonly devices = signal<Device[]>([])
|
|
1008
|
+
/** Whether the device list is loading. */
|
|
1009
|
+
protected readonly loading = signal(true)
|
|
1010
|
+
/** The id of the row showing its revoke confirmation. */
|
|
1011
|
+
protected readonly confirmingId = signal<string | null>(null)
|
|
1012
|
+
/** The id of the device whose revoke request is running. */
|
|
1013
|
+
protected readonly revokingId = signal<string | null>(null)
|
|
1014
|
+
|
|
1015
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
1016
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* Whether one row shows its revoke confirmation.
|
|
1020
|
+
* @param device - The row's device.
|
|
1021
|
+
* @returns True when the row is confirming.
|
|
1022
|
+
*/
|
|
1023
|
+
protected isConfirming(device: Device): boolean {
|
|
1024
|
+
return this.confirmingId() === device.id
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Whether one row's revoke request is running.
|
|
1029
|
+
* @param device - The row's device.
|
|
1030
|
+
* @returns True when the row is revoking.
|
|
1031
|
+
*/
|
|
1032
|
+
protected isRevoking(device: Device): boolean {
|
|
1033
|
+
return this.revokingId() === device.id
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/**
|
|
1037
|
+
* The template context for one row's custom renderer (the way the
|
|
1038
|
+
* conformance kit maps a React render function: `$implicit` = the first
|
|
1039
|
+
* argument, `a0`… the argument list).
|
|
1040
|
+
* @param device - The row's device.
|
|
1041
|
+
* @returns The template context.
|
|
1042
|
+
*/
|
|
1043
|
+
protected rowContext(device: Device): Record<string, unknown> {
|
|
1044
|
+
return { $implicit: device, a0: device, a1: undefined, a2: undefined }
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* Loads the devices (React's `load`): GET /api/devices responds with the
|
|
1049
|
+
* ARRAY itself (see the device resource's query handler) — response.data IS
|
|
1050
|
+
* the rows. This used to read `response.data.data`, which is always
|
|
1051
|
+
* undefined for that shape, so the section rendered "No devices" for
|
|
1052
|
+
* everyone. Devices endpoint is optional; the section renders empty when
|
|
1053
|
+
* unavailable.
|
|
1054
|
+
*/
|
|
1055
|
+
private async load(): Promise<void> {
|
|
1056
|
+
try {
|
|
1057
|
+
const response = await this.http.get<Device[]>('/api/devices')
|
|
1058
|
+
this.devices.set(Array.isArray(response.data) ? response.data : [])
|
|
1059
|
+
} catch (_error) {
|
|
1060
|
+
// Devices endpoint is optional; the section renders empty when unavailable.
|
|
1061
|
+
} finally {
|
|
1062
|
+
this.loading.set(false)
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
/** Loads once on mount (React's mount effect). */
|
|
1067
|
+
constructor() {
|
|
1068
|
+
inject(DestroyRef)
|
|
1069
|
+
void this.load()
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* Signs one device out and reloads (React's `revoke`); best-effort — the
|
|
1074
|
+
* row is kept so the user can retry the revoke.
|
|
1075
|
+
* @param id - The device row's id.
|
|
1076
|
+
*/
|
|
1077
|
+
protected async revoke(id: string): Promise<void> {
|
|
1078
|
+
this.revokingId.set(id)
|
|
1079
|
+
try {
|
|
1080
|
+
await this.http.delete(`/api/devices/${id}`)
|
|
1081
|
+
this.confirmingId.set(null)
|
|
1082
|
+
await this.load()
|
|
1083
|
+
} catch (_error) {
|
|
1084
|
+
// Best-effort — keep the row so the user can retry the revoke.
|
|
1085
|
+
} finally {
|
|
1086
|
+
this.revokingId.set(null)
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
```
|
|
1091
|
+
|
|
1092
|
+
#### `LogOutDeleteSection`
|
|
1093
|
+
|
|
1094
|
+
LogOutDeleteSection component.
|
|
1095
|
+
|
|
1096
|
+
Bottom actions section — Log out + Delete account. React's root is a
|
|
1097
|
+
fragment (the `<section>` plus the delete-account modal), so the host is
|
|
1098
|
+
unwrapped by the DOM normalizer and `display: contents` keeps the layout
|
|
1099
|
+
fragment-like.
|
|
1100
|
+
|
|
1101
|
+
Owns the delete-account modal internally (the trigger lives in the
|
|
1102
|
+
same section so the modal lives here too). Reads `onClose` from
|
|
1103
|
+
the `<SettingsContainer>` context to dismiss the panel after either
|
|
1104
|
+
action and navigates to `/login`.
|
|
1105
|
+
|
|
1106
|
+
```typescript
|
|
1107
|
+
@Component({
|
|
1108
|
+
selector: 'mol-log-out-delete-section',
|
|
1109
|
+
standalone: true,
|
|
1110
|
+
imports: [Alert, Button, Input, Modal],
|
|
1111
|
+
host: {
|
|
1112
|
+
'data-parity-unwrap': '',
|
|
1113
|
+
style: 'display: contents',
|
|
1114
|
+
},
|
|
1115
|
+
template: `<section [class]="cm.stack(3)">
|
|
1116
|
+
<button
|
|
1117
|
+
mol-button
|
|
1118
|
+
[color]="'error'"
|
|
1119
|
+
[fullWidth]="true"
|
|
1120
|
+
(click)="logout()"
|
|
1121
|
+
data-mol-id="user-menu-logout"
|
|
1122
|
+
>
|
|
1123
|
+
{{ t('settings.logOut', undefined, { defaultValue: 'Sign out' }) }}
|
|
1124
|
+
</button>
|
|
1125
|
+
<button
|
|
1126
|
+
mol-button
|
|
1127
|
+
[variant]="'ghost'"
|
|
1128
|
+
[fullWidth]="true"
|
|
1129
|
+
(click)="showModal.set(true)"
|
|
1130
|
+
data-mol-id="user-menu-delete-account"
|
|
1131
|
+
>
|
|
1132
|
+
{{ t('settings.deleteAccount', undefined, { defaultValue: 'Delete account' }) }}
|
|
1133
|
+
</button>
|
|
1134
|
+
</section>
|
|
1135
|
+
<mol-modal
|
|
1136
|
+
[open]="showModal()"
|
|
1137
|
+
(close)="showModal.set(false)"
|
|
1138
|
+
[title]="
|
|
1139
|
+
t('settings.deleteAccountModal.title', undefined, { defaultValue: 'Delete Account' })
|
|
1140
|
+
"
|
|
1141
|
+
>
|
|
1142
|
+
<p [class]="cm.cn(cm.textSize('sm'), cm.textMuted, cm.sp('mb', 4))">
|
|
1143
|
+
{{
|
|
1144
|
+
t('settings.deleteAccountModal.warning', undefined, {
|
|
1145
|
+
defaultValue: 'This action cannot be undone. Please enter your password to confirm.',
|
|
1146
|
+
})
|
|
1147
|
+
}}
|
|
1148
|
+
</p>
|
|
1149
|
+
@if (deleteError(); as submitError) {
|
|
1150
|
+
<div mol-alert [status]="'error'" [className]="cm.sp('mb', 4)">{{ submitError }}</div>
|
|
1151
|
+
}
|
|
1152
|
+
<form [class]="cm.stack(4)" (submit)="deleteAccount($event)">
|
|
1153
|
+
<div>
|
|
1154
|
+
<label for="delete-pw" [class]="cm.formLabel">{{
|
|
1155
|
+
t('settings.deleteAccountModal.password', undefined, { defaultValue: 'Password' })
|
|
1156
|
+
}}</label>
|
|
1157
|
+
<div
|
|
1158
|
+
mol-input
|
|
1159
|
+
[id]="'delete-pw'"
|
|
1160
|
+
type="password"
|
|
1161
|
+
[required]="true"
|
|
1162
|
+
[value]="password()"
|
|
1163
|
+
(change)="password.set($any($event.target).value)"
|
|
1164
|
+
></div>
|
|
1165
|
+
</div>
|
|
1166
|
+
<button mol-button type="submit" [color]="'error'" [fullWidth]="true" [loading]="loading()">
|
|
1167
|
+
@if (loading()) {
|
|
1168
|
+
{{
|
|
1169
|
+
t('settings.deleteAccountModal.deleting', undefined, { defaultValue: 'Deleting...' })
|
|
1170
|
+
}}
|
|
1171
|
+
} @else {
|
|
1172
|
+
{{ t('settings.deleteAccount', undefined, { defaultValue: 'Delete account' }) }}
|
|
1173
|
+
}
|
|
1174
|
+
</button>
|
|
1175
|
+
</form>
|
|
1176
|
+
</mol-modal>`,
|
|
1177
|
+
})
|
|
1178
|
+
export class LogOutDeleteSection {
|
|
1179
|
+
/** Reactive translation, read in the injection context. */
|
|
1180
|
+
protected readonly i18n = useTranslation()
|
|
1181
|
+
|
|
1182
|
+
/** The translation function for the template. */
|
|
1183
|
+
protected t = this.i18n.t
|
|
1184
|
+
|
|
1185
|
+
/** The binding's router (throws outside one, like react-router's useNavigate). */
|
|
1186
|
+
private readonly router = useRouter()
|
|
1187
|
+
|
|
1188
|
+
/** The signed-in user and the auth actions. */
|
|
1189
|
+
private readonly auth = useAuth<UserProfile>()
|
|
1190
|
+
|
|
1191
|
+
private readonly http = useHttpClient()
|
|
1192
|
+
|
|
1193
|
+
/** The container's `onClose` (throws outside a `<SettingsContainer>`). */
|
|
1194
|
+
private readonly panel = useSettingsPanelContext()
|
|
1195
|
+
|
|
1196
|
+
/** Whether the delete-account modal is open. */
|
|
1197
|
+
protected readonly showModal = signal(false)
|
|
1198
|
+
/** The delete-account password field's value. */
|
|
1199
|
+
protected readonly password = signal('')
|
|
1200
|
+
/** Whether the delete request is running. */
|
|
1201
|
+
protected readonly loading = signal(false)
|
|
1202
|
+
/** The delete-account modal's error message. */
|
|
1203
|
+
protected readonly deleteError = signal('')
|
|
1204
|
+
|
|
1205
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
1206
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
1207
|
+
|
|
1208
|
+
/** Signs out, closes the panel and returns to `/login` (React's `handleLogout`). */
|
|
1209
|
+
protected async logout(): Promise<void> {
|
|
1210
|
+
await this.auth.logout()
|
|
1211
|
+
this.panel.onClose()
|
|
1212
|
+
this.router.navigate('/login')
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* Deletes the account (React's `handleDelete`): submits the password,
|
|
1217
|
+
* signs out, closes the panel and returns to `/login`; a failure shows the
|
|
1218
|
+
* server's message and keeps the modal.
|
|
1219
|
+
* @param event - The native submit event.
|
|
1220
|
+
*/
|
|
1221
|
+
protected async deleteAccount(event: Event): Promise<void> {
|
|
1222
|
+
event.preventDefault()
|
|
1223
|
+
this.loading.set(true)
|
|
1224
|
+
this.deleteError.set('')
|
|
1225
|
+
try {
|
|
1226
|
+
await this.http.delete(`/api/users/${this.auth.user()?.id}`, {
|
|
1227
|
+
data: { password: this.password() },
|
|
1228
|
+
})
|
|
1229
|
+
await this.auth.logout()
|
|
1230
|
+
this.panel.onClose()
|
|
1231
|
+
this.router.navigate('/login')
|
|
1232
|
+
} catch (err) {
|
|
1233
|
+
this.deleteError.set(
|
|
1234
|
+
serverErrorMessage(err) ??
|
|
1235
|
+
this.t('settings.failedToDeleteAccount', undefined, {
|
|
1236
|
+
defaultValue: 'Failed to delete account.',
|
|
1237
|
+
}),
|
|
1238
|
+
)
|
|
1239
|
+
} finally {
|
|
1240
|
+
this.loading.set(false)
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
```
|
|
1245
|
+
|
|
1246
|
+
#### `NotificationsSection`
|
|
1247
|
+
|
|
1248
|
+
NotificationsSection component.
|
|
1249
|
+
|
|
1250
|
+
Push-notification toggle section — the full receive-side enable chain:
|
|
1251
|
+
browser permission → runtime VAPID public key
|
|
1252
|
+
(`GET /api/devices/push/public-key`) → `pushManager.subscribe` with
|
|
1253
|
+
`applicationServerKey` → PATCH the subscription onto the current device
|
|
1254
|
+
row (`/api/devices/:id { pushSubscription, hasPushSubscription }`), which
|
|
1255
|
+
is where the api-side push fan-outs look for it. The host IS the
|
|
1256
|
+
`<section>` React renders.
|
|
1257
|
+
|
|
1258
|
+
Initial state reflects SERVER truth (the current device row's
|
|
1259
|
+
`hasPushSubscription`), and every failure surfaces as an honest inline
|
|
1260
|
+
message — a dev build without a service worker fails fast instead of
|
|
1261
|
+
hanging the switch.
|
|
1262
|
+
|
|
1263
|
+
```typescript
|
|
1264
|
+
@Component({
|
|
1265
|
+
selector: 'section[mol-settings-notifications]',
|
|
1266
|
+
standalone: true,
|
|
1267
|
+
imports: [Flex, Switch],
|
|
1268
|
+
template: `<h3 [class]="cm.cn(cm.sectionHeading, cm.sp('mb', 3))">
|
|
1269
|
+
{{ t('settings.notifications', undefined, { defaultValue: 'Notifications' }) }}
|
|
1270
|
+
</h3>
|
|
1271
|
+
<div mol-flex [align]="'center'" [justify]="'between'">
|
|
1272
|
+
<span [class]="cm.textSize('sm')">{{
|
|
1273
|
+
t('settings.pushNotifications', undefined, { defaultValue: 'Push notifications' })
|
|
1274
|
+
}}</span>
|
|
1275
|
+
<mol-switch
|
|
1276
|
+
#pushSwitch
|
|
1277
|
+
[checked]="enabled()"
|
|
1278
|
+
[disabled]="busy()"
|
|
1279
|
+
[attr.aria-label]="
|
|
1280
|
+
t('settings.pushNotifications', undefined, { defaultValue: 'Push notifications' })
|
|
1281
|
+
"
|
|
1282
|
+
(change)="pushToggleChanged($event)"
|
|
1283
|
+
[size]="'sm'"
|
|
1284
|
+
data-mol-id="settings-push-toggle"
|
|
1285
|
+
></mol-switch>
|
|
1286
|
+
</div>
|
|
1287
|
+
@if (error(); as pushError) {
|
|
1288
|
+
<p
|
|
1289
|
+
[class]="cm.cn(cm.textSize('xs'), cm.textError, cm.sp('mt', 1))"
|
|
1290
|
+
role="alert"
|
|
1291
|
+
data-mol-id="settings-push-error"
|
|
1292
|
+
>
|
|
1293
|
+
{{ pushError }}
|
|
1294
|
+
</p>
|
|
1295
|
+
}`,
|
|
1296
|
+
})
|
|
1297
|
+
export class NotificationsSection implements AfterViewInit {
|
|
1298
|
+
/** Reactive translation, read in the injection context. */
|
|
1299
|
+
protected readonly i18n = useTranslation()
|
|
1300
|
+
|
|
1301
|
+
/** The translation function for the template. */
|
|
1302
|
+
protected t = this.i18n.t
|
|
1303
|
+
|
|
1304
|
+
private readonly http = useHttpClient()
|
|
1305
|
+
|
|
1306
|
+
/** The push provider's permission/registration actions. */
|
|
1307
|
+
private readonly push = usePush()
|
|
1308
|
+
|
|
1309
|
+
/** Whether the current device's push subscription is on. */
|
|
1310
|
+
protected readonly enabled = signal(false)
|
|
1311
|
+
/** Whether an enable/disable chain is running (the switch is disabled). */
|
|
1312
|
+
protected readonly busy = signal(false)
|
|
1313
|
+
/** The inline failure message, or null. */
|
|
1314
|
+
protected readonly error = signal<string | null>(null)
|
|
1315
|
+
|
|
1316
|
+
/** The push switch's host element (the hidden checkbox lives inside). */
|
|
1317
|
+
@ViewChild('pushSwitch', { read: ElementRef }) pushSwitch?: ElementRef<HTMLElement>
|
|
1318
|
+
|
|
1319
|
+
/** The controlled-`checked` sync (created once the host exists). */
|
|
1320
|
+
private sync?: ControlledSwitchSync
|
|
1321
|
+
|
|
1322
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
1323
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* Reads the server's push state for THIS device (React's mount effect).
|
|
1327
|
+
* Resolves false (never throws) when the devices endpoint is unavailable;
|
|
1328
|
+
* a result that lands after unmount is dropped like React's cancelled one.
|
|
1329
|
+
*/
|
|
1330
|
+
constructor() {
|
|
1331
|
+
let cancelled = false
|
|
1332
|
+
inject(DestroyRef).onDestroy(() => {
|
|
1333
|
+
cancelled = true
|
|
1334
|
+
})
|
|
1335
|
+
// Server truth for THIS device; resolves false (never throws) when the
|
|
1336
|
+
// devices endpoint is unavailable.
|
|
1337
|
+
void readCurrentDevicePushEnabled(this.http).then((serverEnabled) => {
|
|
1338
|
+
if (!cancelled && serverEnabled) this.enabled.set(true)
|
|
1339
|
+
})
|
|
1340
|
+
// React's controlled `checked` re-render, as an effect (view hooks are
|
|
1341
|
+
// skipped by signal-driven refreshes).
|
|
1342
|
+
effect(() => {
|
|
1343
|
+
const checked = this.enabled()
|
|
1344
|
+
untracked(() => this.runSync(checked))
|
|
1345
|
+
})
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/** Captures the mount-time attribute state (hooks run on the mount passes). */
|
|
1349
|
+
ngAfterViewInit(): void {
|
|
1350
|
+
this.runSync(this.enabled())
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
/**
|
|
1354
|
+
* Runs the controlled-`checked` sync once the switch host exists.
|
|
1355
|
+
* @param checked - The controlled value this render shows.
|
|
1356
|
+
*/
|
|
1357
|
+
private runSync(checked: boolean): void {
|
|
1358
|
+
if (!this.sync && this.pushSwitch) {
|
|
1359
|
+
this.sync = new ControlledSwitchSync(this.pushSwitch.nativeElement)
|
|
1360
|
+
}
|
|
1361
|
+
this.sync?.sync(checked)
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
/**
|
|
1365
|
+
* The switch's change: the hidden checkbox's `checked` at emit time (React
|
|
1366
|
+
* reads `e.target.checked` before its controlled restore).
|
|
1367
|
+
* @param event - The native change event the UI Switch re-emitted.
|
|
1368
|
+
*/
|
|
1369
|
+
protected pushToggleChanged(event: Event): void {
|
|
1370
|
+
const checked = (event.target as HTMLInputElement | null)?.checked ?? false
|
|
1371
|
+
void this.handleToggle(checked)
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* The localized message for one toggle failure (React's `failureMessage`).
|
|
1376
|
+
* @param result - The failed toggle result.
|
|
1377
|
+
* @returns The message text.
|
|
1378
|
+
*/
|
|
1379
|
+
private failureMessage(result: Extract<PushToggleResult, { ok: false }>): string {
|
|
1380
|
+
switch (result.reason) {
|
|
1381
|
+
case 'permission-denied':
|
|
1382
|
+
return this.t('settings.pushPermissionDenied', undefined, {
|
|
1383
|
+
defaultValue: 'Notification permission was denied.',
|
|
1384
|
+
})
|
|
1385
|
+
case 'server-unconfigured':
|
|
1386
|
+
return this.t('settings.pushServerUnavailable', undefined, {
|
|
1387
|
+
defaultValue: 'Push notifications are not configured on this server.',
|
|
1388
|
+
})
|
|
1389
|
+
case 'register-failed':
|
|
1390
|
+
// The push provider throws already-localized messages (not supported /
|
|
1391
|
+
// no service worker in a dev build / push-service rejection).
|
|
1392
|
+
return (
|
|
1393
|
+
result.message ||
|
|
1394
|
+
this.t('settings.pushUpdateFailed', undefined, {
|
|
1395
|
+
defaultValue: 'Could not update push notification settings.',
|
|
1396
|
+
})
|
|
1397
|
+
)
|
|
1398
|
+
case 'persist-failed':
|
|
1399
|
+
// `result.message` here is the HTTP client's error (a status line or a
|
|
1400
|
+
// network error), not text written for the user, so it is not shown.
|
|
1401
|
+
return this.t('settings.pushUpdateFailed', undefined, {
|
|
1402
|
+
defaultValue: 'Could not update push notification settings.',
|
|
1403
|
+
})
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
/**
|
|
1408
|
+
* Runs one enable/disable chain (React's `handleToggle`).
|
|
1409
|
+
* @param next - Whether the switch turned on.
|
|
1410
|
+
*/
|
|
1411
|
+
private async handleToggle(next: boolean): Promise<void> {
|
|
1412
|
+
if (this.busy()) return
|
|
1413
|
+
this.busy.set(true)
|
|
1414
|
+
this.error.set(null)
|
|
1415
|
+
try {
|
|
1416
|
+
if (next) {
|
|
1417
|
+
const result = await enablePushOnCurrentDevice({
|
|
1418
|
+
http: this.http,
|
|
1419
|
+
requestPermission: this.push.requestPermission,
|
|
1420
|
+
register: this.push.register,
|
|
1421
|
+
})
|
|
1422
|
+
if (result.ok) {
|
|
1423
|
+
this.enabled.set(true)
|
|
1424
|
+
} else {
|
|
1425
|
+
this.error.set(this.failureMessage(result))
|
|
1426
|
+
}
|
|
1427
|
+
} else {
|
|
1428
|
+
const result = await disablePushOnCurrentDevice({
|
|
1429
|
+
http: this.http,
|
|
1430
|
+
unregister: this.push.unregister,
|
|
1431
|
+
})
|
|
1432
|
+
// The switch goes off locally either way; a failed server clear is
|
|
1433
|
+
// still reported so the user knows sends may continue.
|
|
1434
|
+
this.enabled.set(false)
|
|
1435
|
+
if (!result.ok) {
|
|
1436
|
+
this.error.set(this.failureMessage(result))
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
} finally {
|
|
1440
|
+
this.busy.set(false)
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
```
|
|
1445
|
+
|
|
1446
|
+
#### `SettingsContainer`
|
|
1447
|
+
|
|
1448
|
+
SettingsContainer component.
|
|
1449
|
+
|
|
1450
|
+
Outer settings-panel layout: padded vertical stack that hosts the section
|
|
1451
|
+
sub-components (projected content). The host IS the `<div>` React renders
|
|
1452
|
+
(`div[mol-settings-container]`), with `cm.sp('p', 6)` + `cm.stack(6)`.
|
|
1453
|
+
|
|
1454
|
+
Publishes `onClose` to descendants via the settings-panel context so
|
|
1455
|
+
`<LogOutDeleteSection>` etc. can dismiss the panel after an action without
|
|
1456
|
+
explicit prop threading. The registration happens in the constructor, which
|
|
1457
|
+
Angular runs before any projected child is created.
|
|
1458
|
+
|
|
1459
|
+
```typescript
|
|
1460
|
+
@Component({
|
|
1461
|
+
selector: 'div[mol-settings-container]',
|
|
1462
|
+
standalone: true,
|
|
1463
|
+
inputs: ['onClose'],
|
|
1464
|
+
host: {
|
|
1465
|
+
'[class]': 'classes()',
|
|
1466
|
+
},
|
|
1467
|
+
template: `<ng-content></ng-content>`,
|
|
1468
|
+
})
|
|
1469
|
+
export class SettingsContainer {
|
|
1470
|
+
/** Closes the settings panel, published to sections through the container context. */
|
|
1471
|
+
onClose?: () => void
|
|
1472
|
+
|
|
1473
|
+
private readonly host = inject(ElementRef)
|
|
1474
|
+
|
|
1475
|
+
/** The context registration removal (set in the constructor). */
|
|
1476
|
+
private removeContext: () => void = () => {}
|
|
1477
|
+
|
|
1478
|
+
/** Registers the container context before any projected child resolves it. */
|
|
1479
|
+
constructor() {
|
|
1480
|
+
this.removeContext = provideSettingsPanelContext(this.host.nativeElement, () => this.onClose)
|
|
1481
|
+
inject(DestroyRef).onDestroy(this.removeContext)
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
1485
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
1486
|
+
|
|
1487
|
+
/**
|
|
1488
|
+
* Computes the container class list (`cm.sp('p', 6)` + `cm.stack(6)`).
|
|
1489
|
+
* @returns The class string.
|
|
1490
|
+
*/
|
|
1491
|
+
classes(): string {
|
|
1492
|
+
return this.cm.cn(this.cm.sp('p', 6), this.cm.stack(6))
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
```
|
|
1496
|
+
|
|
1497
|
+
#### `ThisDeviceSection`
|
|
1498
|
+
|
|
1499
|
+
ThisDeviceSection component.
|
|
1500
|
+
|
|
1501
|
+
"This device" detail strip — OS, browser, online/offline. The host IS the
|
|
1502
|
+
`<section>` React renders.
|
|
1503
|
+
|
|
1504
|
+
Reads everything from `@molecule/app-device` through the `useDevice` hook
|
|
1505
|
+
(`deviceInfo` and `isOnline()`), so the bonded device provider decides what
|
|
1506
|
+
it shows. No side effects, no app-specific state.
|
|
1507
|
+
|
|
1508
|
+
The network line's `isOnline()` call is mediated by a `computed` over the
|
|
1509
|
+
i18n notifications (its only change signal), so the provider is read once
|
|
1510
|
+
per render — the same call count React's render records — and again on a
|
|
1511
|
+
locale switch, exactly like a React re-render.
|
|
1512
|
+
|
|
1513
|
+
```typescript
|
|
1514
|
+
@Component({
|
|
1515
|
+
selector: 'section[mol-settings-this-device]',
|
|
1516
|
+
standalone: true,
|
|
1517
|
+
imports: [Flex],
|
|
1518
|
+
template: `<h3 [class]="cm.cn(cm.sectionHeading, cm.sp('mb', 3))">
|
|
1519
|
+
{{ t('settings.thisDevice', undefined, { defaultValue: 'This Device' }) }}
|
|
1520
|
+
</h3>
|
|
1521
|
+
<dl [class]="cm.cn(cm.stack(1), cm.textSize('sm'))">
|
|
1522
|
+
<div mol-flex [justify]="'between'">
|
|
1523
|
+
<dt [class]="cm.textMuted">
|
|
1524
|
+
{{ t('settings.platform', undefined, { defaultValue: 'Platform' }) }}
|
|
1525
|
+
</dt>
|
|
1526
|
+
<dd>{{ platformLabel() }}</dd>
|
|
1527
|
+
</div>
|
|
1528
|
+
<div mol-flex [justify]="'between'">
|
|
1529
|
+
<dt [class]="cm.textMuted">
|
|
1530
|
+
{{ t('settings.browser', undefined, { defaultValue: 'Browser' }) }}
|
|
1531
|
+
</dt>
|
|
1532
|
+
<dd>{{ browserLabel() }}</dd>
|
|
1533
|
+
</div>
|
|
1534
|
+
<div mol-flex [justify]="'between'">
|
|
1535
|
+
<dt [class]="cm.textMuted">
|
|
1536
|
+
{{ t('settings.network', undefined, { defaultValue: 'Network' }) }}
|
|
1537
|
+
</dt>
|
|
1538
|
+
<dd>{{ networkLabel() }}</dd>
|
|
1539
|
+
</div>
|
|
1540
|
+
</dl>`,
|
|
1541
|
+
})
|
|
1542
|
+
export class ThisDeviceSection {
|
|
1543
|
+
/** The bonded device provider's info (read once, like React's memoized hook). */
|
|
1544
|
+
protected readonly device = useDevice()
|
|
1545
|
+
|
|
1546
|
+
/** Reactive translation, read in the injection context. */
|
|
1547
|
+
protected readonly i18n = useTranslation()
|
|
1548
|
+
|
|
1549
|
+
/** The translation function for the template. */
|
|
1550
|
+
protected t = this.i18n.t
|
|
1551
|
+
|
|
1552
|
+
/** The bonded ClassMap (styling tokens resolve at render time). */
|
|
1553
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
1554
|
+
|
|
1555
|
+
/** The OS name, or the localized Unknown fallback. */
|
|
1556
|
+
protected readonly platformLabel = computed(() => {
|
|
1557
|
+
return (
|
|
1558
|
+
this.device.deviceInfo.os.name ||
|
|
1559
|
+
this.t('settings.unknown', undefined, { defaultValue: 'Unknown' })
|
|
1560
|
+
)
|
|
1561
|
+
})
|
|
1562
|
+
|
|
1563
|
+
/** The browser name, or the localized Unknown fallback. */
|
|
1564
|
+
protected readonly browserLabel = computed(() => {
|
|
1565
|
+
return (
|
|
1566
|
+
this.device.deviceInfo.browser.name ||
|
|
1567
|
+
this.t('settings.unknown', undefined, { defaultValue: 'Unknown' })
|
|
1568
|
+
)
|
|
1569
|
+
})
|
|
1570
|
+
|
|
1571
|
+
/**
|
|
1572
|
+
* The network line: `isOnline()` runs when this computed first renders and
|
|
1573
|
+
* again whenever the locale changes (the t() calls track the i18n
|
|
1574
|
+
* notifications), matching React's per-render call exactly.
|
|
1575
|
+
*/
|
|
1576
|
+
protected readonly networkLabel = computed(() => {
|
|
1577
|
+
return this.device.isOnline()
|
|
1578
|
+
? this.t('settings.online', undefined, { defaultValue: 'Online' })
|
|
1579
|
+
: this.t('settings.offline', undefined, { defaultValue: 'Offline' })
|
|
1580
|
+
})
|
|
1581
|
+
}
|
|
1582
|
+
```
|
|
1583
|
+
|
|
1584
|
+
#### `TiersUpgradeSection`
|
|
1585
|
+
|
|
1586
|
+
TiersUpgradeSection component.
|
|
1587
|
+
|
|
1588
|
+
Billing section with full multi-tier upgrade flow. React's root is a
|
|
1589
|
+
fragment (the `<section>` plus the upgrade modal), so the host is unwrapped
|
|
1590
|
+
by the DOM normalizer and `display: contents` keeps the layout
|
|
1591
|
+
fragment-like.
|
|
1592
|
+
|
|
1593
|
+
Replaces the simpler `BillingSection` for apps backed by
|
|
1594
|
+
`@molecule/api-payments-stripe` + `@molecule/api-resource-payment` — loads
|
|
1595
|
+
the user's current plan from `/api/billing/status`, loads available tiers
|
|
1596
|
+
from `/api/billing/tiers`, and renders an Upgrade modal with a Subscribe
|
|
1597
|
+
button per tier price. Subscribe POSTs to `/api/billing/checkout` and
|
|
1598
|
+
redirects to the Stripe checkout URL; paid users see a Cancel button that
|
|
1599
|
+
POSTs to `/api/billing/cancel`. A refused request shows the server's
|
|
1600
|
+
message.
|
|
1601
|
+
|
|
1602
|
+
```typescript
|
|
1603
|
+
@Component({
|
|
1604
|
+
selector: 'mol-tiers-upgrade-section',
|
|
1605
|
+
standalone: true,
|
|
1606
|
+
imports: [Alert, Button, ConfirmButton, Flex, Modal],
|
|
1607
|
+
inputs: ['redirectToCheckout'],
|
|
1608
|
+
host: {
|
|
1609
|
+
'data-parity-unwrap': '',
|
|
1610
|
+
style: 'display: contents',
|
|
1611
|
+
},
|
|
1612
|
+
template: `<section>
|
|
1613
|
+
<h3 [class]="cm.cn(cm.sectionHeading, cm.sp('mb', 3))">
|
|
1614
|
+
{{ t('settings.billing', undefined, { defaultValue: 'Billing' }) }}
|
|
1615
|
+
</h3>
|
|
1616
|
+
<div mol-flex [align]="'center'" [justify]="'between'">
|
|
1617
|
+
<span [class]="cm.textSize('sm')"
|
|
1618
|
+
>{{ t('settings.plan', undefined, { defaultValue: 'Plan: ' })
|
|
1619
|
+
}}<strong>{{ plan() }}</strong></span
|
|
1620
|
+
>
|
|
1621
|
+
@if (planKey() === 'free') {
|
|
1622
|
+
<button mol-button [variant]="'outline'" [size]="'sm'" (click)="showUpgrade.set(true)">
|
|
1623
|
+
{{ t('settings.upgrade', undefined, { defaultValue: 'Upgrade' }) }}
|
|
1624
|
+
</button>
|
|
1625
|
+
} @else {
|
|
1626
|
+
<button
|
|
1627
|
+
mol-confirm-button
|
|
1628
|
+
[variant]="'outline'"
|
|
1629
|
+
[size]="'sm'"
|
|
1630
|
+
[onConfirm]="cancelSubscription"
|
|
1631
|
+
data-mol-id="billing-cancel-subscription"
|
|
1632
|
+
>
|
|
1633
|
+
<ng-container ngProjectAs="[slot=label]">{{
|
|
1634
|
+
t('settings.billing.cancel', undefined, { defaultValue: 'Cancel' })
|
|
1635
|
+
}}</ng-container
|
|
1636
|
+
><ng-container ngProjectAs="[slot=confirmLabel]">{{
|
|
1637
|
+
t('settings.billing.cancelConfirm', undefined, {
|
|
1638
|
+
defaultValue: 'Cancel your subscription?',
|
|
1639
|
+
})
|
|
1640
|
+
}}</ng-container>
|
|
1641
|
+
</button>
|
|
1642
|
+
}
|
|
1643
|
+
</div>
|
|
1644
|
+
</section>
|
|
1645
|
+
<mol-modal
|
|
1646
|
+
[open]="showUpgrade()"
|
|
1647
|
+
(close)="showUpgrade.set(false)"
|
|
1648
|
+
[title]="t('settings.billing.upgradeTitle', undefined, { defaultValue: 'Upgrade your plan' })"
|
|
1649
|
+
>
|
|
1650
|
+
@if (upgradeError(); as checkoutError) {
|
|
1651
|
+
<div mol-alert [status]="'error'" [className]="cm.sp('mb', 4)">{{ checkoutError }}</div>
|
|
1652
|
+
}
|
|
1653
|
+
@if (tiers().length === 0) {
|
|
1654
|
+
<p [class]="cm.cn(cm.textSize('sm'), cm.textMuted)">
|
|
1655
|
+
{{
|
|
1656
|
+
t('settings.billing.noTiers', undefined, { defaultValue: 'No paid plans configured.' })
|
|
1657
|
+
}}
|
|
1658
|
+
</p>
|
|
1659
|
+
} @else {
|
|
1660
|
+
<ul [class]="cm.stack(3)">
|
|
1661
|
+
@for (entry of subscribeEntries(); track entry.key) {
|
|
1662
|
+
<li [class]="cm.sp('p', 4)">
|
|
1663
|
+
<div mol-flex [align]="'center'" [justify]="'between'">
|
|
1664
|
+
<div>
|
|
1665
|
+
<p [class]="cm.fontWeight('semibold')">
|
|
1666
|
+
{{ entry.tier.name }} ({{ entry.price.interval }})
|
|
1667
|
+
</p>
|
|
1668
|
+
<p [class]="cm.cn(cm.textSize('xs'), cm.textMuted)">
|
|
1669
|
+
{{ entry.price.priceLabel }}
|
|
1670
|
+
</p>
|
|
1671
|
+
</div>
|
|
1672
|
+
<button
|
|
1673
|
+
mol-button
|
|
1674
|
+
[size]="'sm'"
|
|
1675
|
+
[loading]="upgradeLoading() === entry.price.stripePriceId"
|
|
1676
|
+
(click)="startCheckout(entry.price.stripePriceId)"
|
|
1677
|
+
>
|
|
1678
|
+
{{ t('settings.billing.subscribe', undefined, { defaultValue: 'Subscribe' }) }}
|
|
1679
|
+
</button>
|
|
1680
|
+
</div>
|
|
1681
|
+
</li>
|
|
1682
|
+
}
|
|
1683
|
+
</ul>
|
|
1684
|
+
}
|
|
1685
|
+
</mol-modal>`,
|
|
1686
|
+
})
|
|
1687
|
+
export class TiersUpgradeSection {
|
|
1688
|
+
redirectToCheckout: (checkoutUrl: string) => void = openUrl
|
|
1689
|
+
|
|
1690
|
+
protected readonly i18n = useTranslation()
|
|
1691
|
+
|
|
1692
|
+
protected t = this.i18n.t
|
|
1693
|
+
|
|
1694
|
+
private readonly http = useHttpClient()
|
|
1695
|
+
|
|
1696
|
+
protected readonly plan = signal('Free')
|
|
1697
|
+
protected readonly planKey = signal('free')
|
|
1698
|
+
protected readonly tiers = signal<Tier[]>([])
|
|
1699
|
+
protected readonly showUpgrade = signal(false)
|
|
1700
|
+
protected readonly upgradeLoading = signal<string | null>(null)
|
|
1701
|
+
protected readonly upgradeError = signal('')
|
|
1702
|
+
|
|
1703
|
+
protected readonly cm: UIClassMap = getClassMap()
|
|
1704
|
+
|
|
1705
|
+
protected subscribeEntries(): { key: string; tier: Tier; price: TierPrice }[] {
|
|
1706
|
+
const entries: { key: string; tier: Tier; price: TierPrice }[] = []
|
|
1707
|
+
for (const tier of this.tiers()) {
|
|
1708
|
+
if (tier.planKey === 'free') continue
|
|
1709
|
+
for (const price of tier.prices) {
|
|
1710
|
+
if (!price.stripePriceId) continue
|
|
1711
|
+
entries.push({ key: `${tier.planKey}-${price.stripePriceId}`, tier, price })
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
return entries
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
constructor() {
|
|
1718
|
+
let cancelled = false
|
|
1719
|
+
inject(DestroyRef).onDestroy(() => {
|
|
1720
|
+
cancelled = true
|
|
1721
|
+
})
|
|
1722
|
+
void Promise.all([
|
|
1723
|
+
this.http
|
|
1724
|
+
.get<{ planKey?: string; name?: string }>('/api/billing/status')
|
|
1725
|
+
.catch((_error) => null),
|
|
1726
|
+
this.http
|
|
1727
|
+
.get<{
|
|
1728
|
+
data?: Array<{
|
|
1729
|
+
key: string
|
|
1730
|
+
name: string
|
|
1731
|
+
prices: Array<{ stripePriceId: string | null; period: string; price: string }>
|
|
1732
|
+
}>
|
|
1733
|
+
}>('/api/billing/tiers')
|
|
1734
|
+
.catch((_error) => null),
|
|
1735
|
+
])
|
|
1736
|
+
.then(([statusRes, tiersRes]) => {
|
|
1737
|
+
if (cancelled) return
|
|
1738
|
+
const status = statusRes?.data ?? null
|
|
1739
|
+
if (status?.name) this.plan.set(status.name)
|
|
1740
|
+
if (status?.planKey) this.planKey.set(status.planKey)
|
|
1741
|
+
const list = tiersRes?.data?.data ?? []
|
|
1742
|
+
if (Array.isArray(list)) {
|
|
1743
|
+
this.tiers.set(
|
|
1744
|
+
list.map((tier) => ({
|
|
1745
|
+
planKey: tier.key,
|
|
1746
|
+
name: tier.name,
|
|
1747
|
+
prices: (tier.prices || []).map((p) => ({
|
|
1748
|
+
stripePriceId: p.stripePriceId ?? undefined,
|
|
1749
|
+
interval: p.period,
|
|
1750
|
+
priceLabel: p.price,
|
|
1751
|
+
})),
|
|
1752
|
+
})),
|
|
1753
|
+
)
|
|
1754
|
+
}
|
|
1755
|
+
})
|
|
1756
|
+
.catch((_error) => {})
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
protected async startCheckout(stripePriceId: string | undefined): Promise<void> {
|
|
1760
|
+
if (!stripePriceId) return
|
|
1761
|
+
this.upgradeError.set('')
|
|
1762
|
+
this.upgradeLoading.set(stripePriceId)
|
|
1763
|
+
try {
|
|
1764
|
+
const res = await this.http.post<{ checkoutUrl?: string; updated?: boolean }>(
|
|
1765
|
+
'/api/billing/checkout',
|
|
1766
|
+
{ priceId: stripePriceId },
|
|
1767
|
+
)
|
|
1768
|
+
const data = res.data
|
|
1769
|
+
if (data?.checkoutUrl) {
|
|
1770
|
+
this.redirectToCheckout(data.checkoutUrl)
|
|
1771
|
+
return
|
|
1772
|
+
}
|
|
1773
|
+
if (data?.updated) {
|
|
1774
|
+
this.showUpgrade.set(false)
|
|
1775
|
+
return
|
|
1776
|
+
}
|
|
1777
|
+
this.upgradeError.set(
|
|
1778
|
+
this.t('settings.billing.checkoutFailed', undefined, {
|
|
1779
|
+
defaultValue: 'Could not start checkout.',
|
|
1780
|
+
}),
|
|
1781
|
+
)
|
|
1782
|
+
} catch (err) {
|
|
1783
|
+
this.upgradeError.set(
|
|
1784
|
+
serverErrorMessage(err) ??
|
|
1785
|
+
this.t('settings.billing.checkoutFailed', undefined, {
|
|
1786
|
+
defaultValue: 'Could not start checkout.',
|
|
1787
|
+
}),
|
|
1788
|
+
)
|
|
1789
|
+
} finally {
|
|
1790
|
+
this.upgradeLoading.set(null)
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
protected cancelSubscription = async (): Promise<void> => {
|
|
1795
|
+
this.upgradeError.set('')
|
|
1796
|
+
try {
|
|
1797
|
+
await this.http.post('/api/billing/cancel', {})
|
|
1798
|
+
this.plan.set('Free')
|
|
1799
|
+
this.planKey.set('free')
|
|
1800
|
+
} catch (err) {
|
|
1801
|
+
this.upgradeError.set(
|
|
1802
|
+
serverErrorMessage(err) ??
|
|
1803
|
+
this.t('settings.billing.cancelFailed', undefined, {
|
|
1804
|
+
defaultValue: 'Could not cancel.',
|
|
1805
|
+
}),
|
|
1806
|
+
)
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
```
|
|
1811
|
+
|
|
1812
|
+
_(Member comments are left out to fit; the full declaration is in `node_modules/@molecule/app-settings-panel-angular/dist/tiers-upgrade-section.component.d.ts`.)_
|
|
1813
|
+
|
|
1814
|
+
### Functions
|
|
1815
|
+
|
|
1816
|
+
#### `provideSettingsPanelContext(host, readOnClose)`
|
|
1817
|
+
|
|
1818
|
+
Registers the container context on `SettingsContainer`'s host element. The
|
|
1819
|
+
value closes over the container instance, so the context always exposes the
|
|
1820
|
+
CURRENT `onClose` prop — React re-renders a new context value object when
|
|
1821
|
+
the prop changes; reading the live field here has the same effect.
|
|
1822
|
+
|
|
1823
|
+
```typescript
|
|
1824
|
+
function provideSettingsPanelContext(
|
|
1825
|
+
host: Element,
|
|
1826
|
+
readOnClose: () => (() => void) | undefined,
|
|
1827
|
+
): () => void
|
|
1828
|
+
```
|
|
1829
|
+
|
|
1830
|
+
- `host` — The container's host element.
|
|
1831
|
+
- `readOnClose` — Reads the container's current `onClose` prop.
|
|
1832
|
+
|
|
1833
|
+
**Returns:** A function that removes the registration (call on destroy).
|
|
1834
|
+
|
|
1835
|
+
#### `useSettingsPanelContext()`
|
|
1836
|
+
|
|
1837
|
+
Reads the `onClose` handler exposed by the parent `<SettingsContainer>`.
|
|
1838
|
+
Throws if used outside the container so component misuse is loud.
|
|
1839
|
+
|
|
1840
|
+
The lookup is deferred to the first property read (an event handler, long
|
|
1841
|
+
after Angular has created and attached every element), so a section nested
|
|
1842
|
+
in projected content resolves the container reliably.
|
|
1843
|
+
|
|
1844
|
+
```typescript
|
|
1845
|
+
function useSettingsPanelContext(): SettingsPanelContextValue
|
|
1846
|
+
```
|
|
1847
|
+
|
|
1848
|
+
**Returns:** The container context.
|
|
1849
|
+
|
|
1850
|
+
#### `findCurrentDevice(devices)`
|
|
1851
|
+
|
|
1852
|
+
Picks the caller's own device row: the api flags the session device
|
|
1853
|
+
`isCurrent`; fall back to the first row for sessions predating the flag.
|
|
1854
|
+
|
|
1855
|
+
```typescript
|
|
1856
|
+
function findCurrentDevice(devices: PushToggleDevice[] | undefined): PushToggleDevice | undefined
|
|
1857
|
+
```
|
|
1858
|
+
|
|
1859
|
+
- `devices` — Rows from `GET /api/devices`.
|
|
1860
|
+
|
|
1861
|
+
**Returns:** The current device row, or `undefined` when the user has none.
|
|
1862
|
+
|
|
1863
|
+
#### `subscriptionFromToken(token)`
|
|
1864
|
+
|
|
1865
|
+
Converts an `@molecule/app-push` token into the device resource's
|
|
1866
|
+
`pushSubscription` shape (web PushSubscription JSON, FCM registration for
|
|
1867
|
+
Android, APNs registration for iOS).
|
|
1868
|
+
|
|
1869
|
+
```typescript
|
|
1870
|
+
function subscriptionFromToken(token: PushToggleToken): unknown
|
|
1871
|
+
```
|
|
1872
|
+
|
|
1873
|
+
- `token` — The push token returned by `register()`.
|
|
1874
|
+
|
|
1875
|
+
**Returns:** The `pushSubscription` value to PATCH onto the device row.
|
|
1876
|
+
|
|
1877
|
+
#### `enablePushOnCurrentDevice(deps)`
|
|
1878
|
+
|
|
1879
|
+
Runs the full enable chain: browser permission → runtime VAPID public key
|
|
1880
|
+
(`GET /api/devices/push/public-key`) → `register({ vapidPublicKey })` →
|
|
1881
|
+
persist the subscription on the current device row.
|
|
1882
|
+
|
|
1883
|
+
Every failure is returned as a typed reason (never thrown) so the UI can
|
|
1884
|
+
show an honest, specific message instead of hanging or silently reverting.
|
|
1885
|
+
|
|
1886
|
+
```typescript
|
|
1887
|
+
function enablePushOnCurrentDevice(deps: {
|
|
1888
|
+
http: PushToggleHttp
|
|
1889
|
+
requestPermission: () => Promise<string>
|
|
1890
|
+
register: (options?: { vapidPublicKey?: string }) => Promise<PushToggleToken>
|
|
1891
|
+
}): Promise<PushToggleResult>
|
|
1892
|
+
```
|
|
1893
|
+
|
|
1894
|
+
- `deps` — The http client + push actions from `usePush()`.
|
|
1895
|
+
- `deps.http` — Authenticated http client (`useHttpClient()`).
|
|
1896
|
+
- `deps.requestPermission` — `usePush().requestPermission`.
|
|
1897
|
+
- `deps.register` — `usePush().register`.
|
|
1898
|
+
|
|
1899
|
+
**Returns:** `{ ok: true }` or a typed failure.
|
|
1900
|
+
|
|
1901
|
+
#### `disablePushOnCurrentDevice(deps)`
|
|
1902
|
+
|
|
1903
|
+
Disables push: unsubscribes the browser (best-effort — a dev build without
|
|
1904
|
+
a service worker has nothing to unsubscribe) and ALWAYS clears the server
|
|
1905
|
+
state so no further pushes target this device.
|
|
1906
|
+
|
|
1907
|
+
```typescript
|
|
1908
|
+
function disablePushOnCurrentDevice(deps: {
|
|
1909
|
+
http: PushToggleHttp
|
|
1910
|
+
unregister: () => Promise<void>
|
|
1911
|
+
}): Promise<PushToggleResult>
|
|
1912
|
+
```
|
|
1913
|
+
|
|
1914
|
+
- `deps` — The http client + push action from `usePush()`.
|
|
1915
|
+
- `deps.http` — Authenticated http client (`useHttpClient()`).
|
|
1916
|
+
- `deps.unregister` — `usePush().unregister`.
|
|
1917
|
+
|
|
1918
|
+
**Returns:** `{ ok: true }` or a typed failure (server state not cleared).
|
|
1919
|
+
|
|
1920
|
+
#### `readCurrentDevicePushEnabled(http)`
|
|
1921
|
+
|
|
1922
|
+
Reads whether push is currently enabled for THIS device (server truth:
|
|
1923
|
+
the current device row's `hasPushSubscription`).
|
|
1924
|
+
|
|
1925
|
+
```typescript
|
|
1926
|
+
function readCurrentDevicePushEnabled(http: PushToggleHttp): Promise<boolean>
|
|
1927
|
+
```
|
|
1928
|
+
|
|
1929
|
+
- `http` — Authenticated http client (`useHttpClient()`).
|
|
1930
|
+
|
|
1931
|
+
**Returns:** `true` when the current device has a stored subscription.
|
|
1932
|
+
|
|
1933
|
+
### Constants
|
|
1934
|
+
|
|
1935
|
+
#### `SettingsPanelContext`
|
|
1936
|
+
|
|
1937
|
+
Names the container context `SettingsContainer` publishes and sections
|
|
1938
|
+
consume (React's `SettingsPanelContext` object). Angular's counterpart is
|
|
1939
|
+
the DOM-scoped registration on the container's host element; this constant
|
|
1940
|
+
documents and keys that contract.
|
|
1941
|
+
|
|
1942
|
+
```typescript
|
|
1943
|
+
const SettingsPanelContext: 'molecule-settings-panel-context'
|
|
1944
|
+
```
|
|
1945
|
+
|
|
1946
|
+
## Injection Notes
|
|
1947
|
+
|
|
1948
|
+
### Requirements
|
|
1949
|
+
|
|
1950
|
+
Peer dependencies:
|
|
1951
|
+
|
|
1952
|
+
- `@angular/common` ^22.0.0
|
|
1953
|
+
- `@angular/core` ^22.0.0
|
|
1954
|
+
- `@angular/platform-browser` ^22.0.0
|
|
1955
|
+
- `@molecule/app-angular` ^3.5.0
|
|
1956
|
+
- `@molecule/app-auth` ^3.0.0
|
|
1957
|
+
- `@molecule/app-http` ^1.8.0
|
|
1958
|
+
- `@molecule/app-push` ^1.1.0
|
|
1959
|
+
- `@molecule/app-ui` ^1.0.1
|
|
1960
|
+
- `@molecule/app-ui-angular` ^1.1.0
|
|
1961
|
+
- `@molecule/app-utilities` ^1.0.6
|
|
1962
|
+
|
|
1963
|
+
### Runtime Dependencies
|
|
1964
|
+
|
|
1965
|
+
- `@angular/common`
|
|
1966
|
+
- `@angular/core`
|
|
1967
|
+
- `@angular/platform-browser`
|
|
1968
|
+
- `@molecule/app-angular`
|
|
1969
|
+
- `@molecule/app-auth`
|
|
1970
|
+
- `@molecule/app-http`
|
|
1971
|
+
- `@molecule/app-push`
|
|
1972
|
+
- `@molecule/app-ui`
|
|
1973
|
+
- `@molecule/app-ui-angular`
|
|
1974
|
+
- `@molecule/app-utilities`
|
|
1975
|
+
|
|
1976
|
+
- Wiring prereqs: sections need the standard `@molecule/app-angular`
|
|
1977
|
+
provider stack — `provideI18n`, `provideHttp` (authenticated client),
|
|
1978
|
+
`provideAuth`, `provideTheme` (AppearanceSection), the bonded device and
|
|
1979
|
+
push providers (NotificationsSection/ThisDeviceSection) — plus a bonded
|
|
1980
|
+
ClassMap. `BillingSection` and `LogOutDeleteSection` also need the
|
|
1981
|
+
binding's router (`provideRouter`) and throw outside one.
|
|
1982
|
+
- Section components throw if rendered outside `<SettingsContainer>`
|
|
1983
|
+
(they read its context for `onClose`).
|
|
1984
|
+
- Server contract: the molecule API surface from
|
|
1985
|
+
`@molecule/api-resource-user`, `@molecule/api-resource-device`,
|
|
1986
|
+
`@molecule/api-two-factor`, and the billing endpoints
|
|
1987
|
+
(`/api/billing/*`) wired by the payments stack. Missing read endpoints
|
|
1988
|
+
degrade gracefully (sections render empty); the mutating actions do not.
|
|
1989
|
+
- Translations: `@molecule/app-locales-settings-panel` companion bond.
|
|
1990
|
+
|
|
1991
|
+
## Translations
|
|
1992
|
+
|
|
1993
|
+
Translation strings are provided by `@molecule/app-locales-settings-panel`.
|