Spaces:
Sleeping
Sleeping
File size: 27,319 Bytes
79ed62f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | # System Notices β Technical Documentation & Dev Guide
System notices are server-evaluated, user-targeted messages shown in the TREK UI as modals, banners, or toasts. They are used for onboarding, upgrade announcements, breaking change warnings, and time-boxed campaigns. Every aspect β targeting, display, copy, and dismissal β is controlled from one place: the server-side registry.
---
## Table of Contents
1. [Architecture overview](#1-architecture-overview)
2. [Data flow](#2-data-flow)
3. [Database schema](#3-database-schema)
4. [The notice registry](#4-the-notice-registry)
5. [Notice fields reference](#5-notice-fields-reference)
6. [Condition system](#6-condition-system)
7. [Display types](#7-display-types)
8. [CTAs (call to action)](#8-ctas-call-to-action)
9. [i18n β translation keys](#9-i18n--translation-keys)
10. [Client store & dismissal](#10-client-store--dismissal)
11. [Sorting & priority](#11-sorting--priority)
12. [How-to recipes](#12-how-to-recipes)
13. [Testing](#13-testing)
14. [Rules & constraints](#14-rules--constraints)
---
## 1. Architecture overview
```
server/src/systemNotices/
βββ types.ts β TypeScript types (SystemNotice, NoticeCondition, β¦)
βββ registry.ts β Authoritative list of all notices (edit here to add/change/remove)
βββ conditions.ts β Condition evaluators + custom predicate registry
βββ service.ts β Queries DB, evaluates conditions, sorts, strips server-only fields
server/src/routes/systemNotices.ts β REST endpoints
client/src/store/systemNoticeStore.ts β Zustand store (fetch + optimistic dismiss)
client/src/components/SystemNotices/
βββ SystemNoticeHost.tsx β Renders all three channels (modal / banner / toast)
βββ SystemNoticeModal.tsx β Modal renderer (pager, animations, keyboard nav)
βββ SystemNoticeBanner.tsx β Banner + toast renderers
βββ noticeActions.ts β Client-side action registry for action-kind CTAs
client/src/pages/Trips/noticeActions.ts β Example domain action registration
```
There are **no database rows for notice definitions**. The registry is code-only. The database only stores which notices a user has dismissed.
---
## 2. Data flow
```
1. User authenticates
β
βΌ
2. authStore.loadUser() completes
β
βΌ
3. SystemNoticeHost mounts β calls useSystemNoticeStore.fetch()
β (also triggered on cold page reload if store not yet loaded)
βΌ
4. GET /api/system-notices/active
β
βΌ
5. service.getActiveNoticesFor(userId)
βββ reads user row (login_count, first_seen_version, role)
βββ counts user trips
βββ reads user_notice_dismissals
βββ filters SYSTEM_NOTICES:
β β not dismissed
β β within [minVersion, maxVersion) range for the running app version
β β all conditions pass (AND logic)
βββ sorts by priority β severity β publishedAt (desc)
βββ strips server-only fields (conditions, publishedAt, minVersion, maxVersion, priority)
β
βΌ
6. Client receives SystemNoticeDTO[]
β
βΌ
7. SystemNoticeHost partitions by display type
βββ modal β ModalRenderer (multi-page pager, slide transitions)
βββ banner β BannerRenderer (sticky top bar, max 2)
βββ toast β ToastRenderer (fires window.__addToast, auto-dismisses)
β
βΌ
8. User dismisses β POST /api/system-notices/:id/dismiss
βββ Server: INSERT OR IGNORE into user_notice_dismissals
βββ Client: optimistic remove from store (retry once on failure)
```
---
## 3. Database schema
Added in **migration 101** (`server/src/db/migrations.ts`).
### `users` columns (added by migration 101)
| Column | Type | Default | Purpose |
|---|---|---|---|
| `first_seen_version` | `TEXT` | `'0.0.0'` | App version at account creation. Used by `existingUserBeforeVersion` condition. Backfilled users get `'0.0.0'`. |
| `login_count` | `INTEGER` | `0` | Incremented on each successful login. Used by `firstLogin` condition. |
### `user_notice_dismissals`
| Column | Type | Notes |
|---|---|---|
| `user_id` | `INTEGER` | FK β `users.id` CASCADE DELETE |
| `notice_id` | `TEXT` | Matches `SystemNotice.id` from registry |
| `dismissed_at` | `INTEGER` | Unix ms timestamp |
Primary key: `(user_id, notice_id)` β dismissals are idempotent.
---
## 4. The notice registry
**`server/src/systemNotices/registry.ts`** is the single source of truth. Add, change, or retire notices here.
```typescript
export const SYSTEM_NOTICES: SystemNotice[] = [
{
id: 'my-notice', // β globally unique, never reuse
display: 'modal',
severity: 'info',
titleKey: 'system_notice.my_notice.title',
bodyKey: 'system_notice.my_notice.body',
dismissible: true,
conditions: [{ kind: 'firstLogin' }],
publishedAt: '2026-05-01T00:00:00Z',
priority: 50,
},
];
```
### The golden rule for IDs
**Never remove or renumber an entry. Never reuse an ID.**
Dismissals are stored in the database keyed by `id`. Removing an entry means dismissed users would see it again if you ever add a notice with the same ID. If a notice is no longer needed, set `maxVersion` to the upper version on which it should appear (e.g. `4.0.0` means show notice until `4.0.0` is reached) β do not delete the entry.
---
## 5. Notice fields reference
### Required fields
| Field | Type | Description |
|---|---|---|
| `id` | `string` | Globally unique, stable identifier. Use kebab-case, descriptive, version-scoped when appropriate (`v3-photos`, `welcome-v1`). Max recommended length: 40 chars. |
| `display` | `'modal' \| 'banner' \| 'toast'` | How the notice is rendered. See [Β§7 Display types](#7-display-types). |
| `severity` | `'info' \| 'warn' \| 'critical'` | Affects colour scheme and accessibility role. `critical` notices cannot be toasts. |
| `titleKey` | `string` | i18n key for the title. |
| `bodyKey` | `string` | i18n key for the body. Markdown supported in modals; plain text only in banners/toasts. |
| `dismissible` | `boolean` | If `false`, the X button and ESC key are hidden/blocked. Use only for `critical` notices that require action before proceeding. |
| `conditions` | `NoticeCondition[]` | Empty array (`[]`) means always shown (same as `[{ kind: 'always' }]`). All conditions must pass (AND logic). |
| `publishedAt` | `string` | ISO 8601 date. Used as a tiebreaker in sorting. Set to the deployment date. |
### Optional fields
| Field | Type | Description |
|---|---|---|
| `priority` | `number` | Higher number = shown first. Primary sort key. Default: `0`. |
| `minVersion` | `string` | Lowest app version (inclusive, semver) that should show this notice. Omit for no lower bound. |
| `maxVersion` | `string` | Upper bound (exclusive, semver) β notice is hidden once this version ships. `maxVersion: '4.0.0'` means shown on `< 4.0.0`. Omit for no upper bound. |
| `icon` | `string` | Lucide icon name (e.g. `'Sparkles'`, `'ImageOff'`). Shown in the modal's severity icon circle. Falls back to the severity default icon if absent or unrecognised. |
| `bodyParams` | `Record<string, string>` | Interpolation parameters for `bodyKey`. Values replace `{key}` placeholders in the translated string. **Never hardcode version numbers or dates directly in translation strings β use this instead.** |
| `media` | `NoticeMedia` | Image to display in the modal. See below. |
| `highlights` | `Array<{ labelKey: string; iconName?: string }>` | Bullet-point feature list rendered below the body in modals. Each entry is a translation key + optional Lucide icon name. |
| `cta` | `NoticeCta` | Primary action button. See [Β§8 CTAs](#8-ctas-call-to-action). |
> **Version bounds:** The range is `[minVersion, maxVersion)` β lower bound inclusive, upper bound exclusive. So `maxVersion: '4.0.0'` hides the notice once the app reaches 4.0.0. Both bounds are compared after stripping prerelease/build metadata via `semver.coerce`, so a server running `3.0.0-pre.42` is treated as `3.0.0` β consistent with `existingUserBeforeVersion` and staging environments behave like production.
### `NoticeMedia`
```typescript
interface NoticeMedia {
src: string; // URL or path
srcDark?: string; // Optional dark-mode variant
altKey: string; // i18n key for alt text
placement?: 'hero' | 'inline'; // default: 'hero' (full-width above body)
aspectRatio?: string; // CSS aspect-ratio value, default '16/9'
}
```
### Character limits
| Field | Modal | Banner | Toast |
|---|---|---|---|
| Title | β€ 40 chars | β€ 40 chars | β€ 40 chars |
| Body | β€ 400 chars (markdown) | β€ 140 chars (plain) | β€ 80 chars (plain) |
| CTA label | β€ 20 chars, a verb | β€ 20 chars | β€ 20 chars |
---
## 6. Condition system
Conditions are evaluated **server-side** on every `GET /api/system-notices/active` call. The client never sees conditions β only the filtered result.
All conditions in `conditions[]` must pass (AND logic). To implement OR logic, create multiple notices with overlapping IDs is not possible β instead use a `custom` predicate with internal OR logic.
### Built-in conditions
#### `always`
```typescript
{ kind: 'always' }
```
Always passes. Equivalent to an empty `conditions` array.
---
#### `firstLogin`
```typescript
{ kind: 'firstLogin' }
```
Passes when `users.login_count <= 1`. The counter is incremented during login, so this fires on the first fetch after the very first login. Useful for onboarding notices.
---
#### `noTrips`
```typescript
{ kind: 'noTrips' }
```
Passes when the user has zero trips. Often combined with `firstLogin`.
---
#### `existingUserBeforeVersion`
```typescript
{ kind: 'existingUserBeforeVersion', version: '3.0.0' }
```
Passes when:
- `users.first_seen_version < version` (user existed before this version)
- AND the running app version `>= version` (the version has been deployed)
Backfilled/legacy users have `first_seen_version = '0.0.0'` and always pass the first condition. Use this for upgrade announcements targeting users who were around before a breaking change.
---
#### `dateWindow`
```typescript
{ kind: 'dateWindow', startsAt: '2026-06-01T00:00:00Z', endsAt: '2026-07-01T00:00:00Z' }
```
Passes when the current server time is inside `[startsAt, endsAt]`. `endsAt` is optional (open-ended). Use for campaigns, maintenance banners, and time-limited promotions.
---
#### `role`
```typescript
{ kind: 'role', roles: ['admin'] }
// or both roles:
{ kind: 'role', roles: ['admin', 'user'] }
```
Passes when the user's role is in the given list.
---
#### `addonEnabled`
```typescript
{ kind: 'addonEnabled', addonId: 'journey' }
```
Passes when the named addon is enabled in admin settings. Addon IDs are the string values in `server/src/addons.ts` (`ADDON_IDS`). Use this to gate notices that promote features behind an addon.
---
#### `custom`
```typescript
{ kind: 'custom', id: 'my-predicate-id' }
```
Delegates evaluation to a predicate registered server-side with `registerPredicate`. This is the escape hatch for logic not covered by the built-in conditions.
**Registering a custom predicate:**
```typescript
// server/src/systemNotices/conditions.ts exports registerPredicate
import { registerPredicate } from '../systemNotices/conditions.js';
registerPredicate('has-immich-configured', (ctx) => {
// ctx.user = { login_count, first_seen_version, role, noTrips }
// ctx.currentAppVersion = string
// ctx.now = Date
return someDbCheck(ctx.user);
});
```
Register predicates at application startup before the first `getActiveNoticesFor` call.
---
### Combining conditions (AND)
```typescript
conditions: [
{ kind: 'existingUserBeforeVersion', version: '3.0.0' },
{ kind: 'addonEnabled', addonId: 'journey' },
]
// Only shows to pre-3.0 users AND only if the journey addon is enabled.
```
---
## 7. Display types
### `modal`
Full-screen overlay with backdrop. On mobile: bottom sheet with drag-to-dismiss. On desktop: centered card.
**Features:**
- Markdown body (via `react-markdown` + `remark-gfm` + `rehype-sanitize`)
- Optional hero or inline image
- Optional highlights list (icon + label bullets)
- Optional CTA button + "Not now" link
- OK button when no CTA is defined
- **Multi-page pager**: when multiple modal notices are active simultaneously, they are rendered as a paginated single modal with prev/next arrows, dot indicators, `N / M` counter, and keyboard arrow navigation
- Slide transition between pages
- ESC to dismiss all (if current notice is dismissible)
- CTA and OK dismiss **all** active modal notices, not just the current page
- "Not now" dismisses only the current page
**Non-dismissible modals** (`dismissible: false`): X button, ESC key, and pager navigation are all disabled until the user acts on the CTA. Use only for `critical` severity.
---
### `banner`
Sticky top bar below the navigation. Slides in with a translate-Y animation.
**Constraints:**
- Maximum 2 banners shown simultaneously (the 2 highest-priority active banners)
- Plain text only (no markdown)
- RTL-aware left-border accent
- Reports its height via a CSS variable `--banner-stack-h` for layout reflow
---
### `toast`
Fires the global `window.__addToast` toast system. Auto-dismisses after 6 s (`info`) or 9 s (`warn`). The notice is dismissed from the store after the toast expires.
**Constraints:**
- `critical` severity is not allowed as a toast β the renderer logs a warning and auto-dismisses it instead
- Plain text only
- No interaction (no CTA rendered via toast)
---
## 8. CTAs (call to action)
A CTA renders as the primary blue button in modals and as an underline link in banners. There are two kinds.
### `nav` β navigate to a route
```typescript
cta: {
kind: 'nav',
labelKey: 'system_notice.my_notice.cta_label',
href: '/journey',
}
```
On click: navigates to `href` using React Router, then **dismisses all active modal notices** (or the current banner notice). The label is resolved through the i18n system.
---
### `action` β run a registered client-side handler
```typescript
cta: {
kind: 'action',
labelKey: 'system_notice.my_notice.cta_label',
actionId: 'open:trip-create',
dismissOnAction: true, // default true β set false to keep notice open after action
}
```
On click: looks up `actionId` in the client-side action registry and calls the handler, then **dismisses all active modal notices**.
**To add a new action:**
1. Create (or extend) a `noticeActions.ts` file in the relevant feature directory:
```typescript
// client/src/pages/MyFeature/noticeActions.ts
import { registerNoticeAction } from '../../components/SystemNotices/noticeActions.js';
registerNoticeAction('open:my-feature', ({ navigate }) => {
navigate('/my-feature?from=notice');
});
```
2. Import it as a side-effect in `client/src/App.tsx`:
```typescript
import './pages/MyFeature/noticeActions.js'
```
3. The registry integrity test (`server/tests/unit/systemNotices/registry.test.ts`) automatically scans all `noticeActions.ts` files and verifies that every `actionId` in the registry is registered. The test will fail if you add an `actionId` to the registry without registering it on the client.
**Action handler signature:**
```typescript
(ctx: NoticeActionContext) => void | Promise<void>
interface NoticeActionContext {
navigate: NavigateFunction; // React Router navigate function
}
```
### Dismiss behaviour summary
| Trigger | What is dismissed |
|---|---|
| X button (modal) | All active modal notices |
| ESC key | All active modal notices (if current is dismissible) |
| CTA button | All active modal notices |
| OK button (no CTA) | All active modal notices |
| "Not now" link | Current page only |
| Banner dismiss (X) | That banner only |
| Backdrop click (modal) | Current page only |
| Swipe down (mobile) | Current page only |
| Toast expires | That toast only |
---
## 9. i18n β translation keys
Every notice field that is user-visible (`titleKey`, `bodyKey`, CTA `labelKey`, highlight `labelKey`, media `altKey`) is an i18n key resolved through `useTranslation().t()`. The key string is what gets stored in the registry; the display value lives in the translation files.
**Translation files location:** `client/src/i18n/translations/` (15 files: `en`, `de`, `fr`, `es`, `it`, `nl`, `pl`, `cs`, `hu`, `ru`, `zh`, `zhTw`, `ar`, `br`, `id`)
### Key naming convention
```
system_notice.<notice_id_snake>.<field>
```
Examples:
```
system_notice.welcome_v1.title
system_notice.welcome_v1.body
system_notice.welcome_v1.cta_label
system_notice.welcome_v1.highlight_plan
system_notice.welcome_v1.hero_alt
```
### Adding keys
Add the English key to `client/src/i18n/translations/en.ts` first, then replicate to the other 14 files. Group related notice keys together with a comment:
```typescript
// System notices β my feature
'system_notice.my_notice.title': 'My feature is here',
'system_notice.my_notice.body': 'Here is what changed.',
'system_notice.my_notice.cta_label': 'Explore',
```
### `bodyParams` interpolation
For values that vary at runtime (version numbers, dates, counts), use `{placeholder}` syntax in the translation string and pass `bodyParams` in the registry entry:
```typescript
// In registry:
bodyKey: 'system_notice.my_notice.body',
bodyParams: { version: '3.1.0', date: '1 May 2026' },
// In en.ts:
'system_notice.my_notice.body': 'TREK {version} was released on {date}.',
```
**Never hardcode dynamic values directly in translation strings.** The interpolation runs client-side in `ModalRenderer` before rendering.
### Multiline bodies (modals only)
Use `\n\n` (escaped, not literal newlines) for paragraph breaks in modal body strings:
```typescript
'system_notice.my_notice.body': 'First paragraph.\n\nSecond paragraph.',
```
Literal newlines in single-quoted TypeScript strings cause a parse error.
### Pager i18n keys
The pager UI uses its own keys (already present in all 15 files):
```
system_notice.pager.prev β "Previous notice"
system_notice.pager.next β "Next notice"
system_notice.pager.counter β "{current} / {total}"
system_notice.pager.goto β "Go to notice {n}"
system_notice.pager.position β "Notice {current} of {total}" (aria-live)
```
---
## 10. Client store & dismissal
`client/src/store/systemNoticeStore.ts` (Zustand, no persistence).
| Action | Behaviour |
|---|---|
| `fetch()` | `GET /api/system-notices/active`. Fails silently (non-critical). Sets `loaded = true` regardless. |
| `dismiss(id)` | Optimistic: removes notice from store immediately. POSTs to `/api/system-notices/{id}/dismiss` in background with one retry on failure. |
`SystemNoticeHost` triggers `fetch()` on mount if `loaded === false`. Auth store also triggers it after login, so on a fresh login the fetch happens exactly once.
---
## 11. Sorting & priority
Notices are sorted before being sent to the client. The sort order is:
1. **`priority`** (descending) β primary key. Higher number appears first.
2. **`severity`** (descending) β tiebreaker: `critical` (2) > `warn` (1) > `info` (0).
3. **`publishedAt`** (descending) β final tiebreaker: more recent notices first.
This means `priority` always wins over severity. Assign priorities deliberately so the intended reading order is preserved when multiple notices are active simultaneously.
Current priority allocations in the registry:
| Range | Use |
|---|---|
| 100 | Onboarding / first-login |
| 80β90 | Major version upgrade notices |
| 50β70 | Feature announcements |
| 10β40 | Campaigns, banners |
| 0 (default) | Miscellaneous |
---
## 12. How-to recipes
### Add a new modal notice
1. **Registry** β add an entry to `SYSTEM_NOTICES` in `server/src/systemNotices/registry.ts`:
```typescript
{
id: 'my-feature-v2',
display: 'modal',
severity: 'info',
icon: 'Zap',
titleKey: 'system_notice.my_feature_v2.title',
bodyKey: 'system_notice.my_feature_v2.body',
highlights: [
{ labelKey: 'system_notice.my_feature_v2.highlight_one', iconName: 'Check' },
],
cta: {
kind: 'nav',
labelKey: 'system_notice.my_feature_v2.cta_label',
href: '/my-feature',
},
dismissible: true,
conditions: [{ kind: 'existingUserBeforeVersion', version: '2.0.0' }],
publishedAt: '2026-06-01T00:00:00Z',
priority: 60,
},
```
2. **i18n** β add keys to `client/src/i18n/translations/en.ts` and the 14 other language files.
3. **Test** β run `cd server && npx vitest run tests/unit/systemNotices/` to verify registry integrity.
---
### Add a notice with an action CTA
1. Create the action handler in the relevant feature directory:
```typescript
// client/src/pages/MyFeature/noticeActions.ts
import { registerNoticeAction } from '../../components/SystemNotices/noticeActions.js';
registerNoticeAction('open:my-feature-dialog', ({ navigate }) => {
navigate('/my-feature?dialog=welcome');
});
```
2. Import it in `client/src/App.tsx`:
```typescript
import './pages/MyFeature/noticeActions.js'
```
3. Reference the `actionId` in the registry:
```typescript
cta: {
kind: 'action',
labelKey: 'system_notice.my_notice.cta_label',
actionId: 'open:my-feature-dialog',
},
```
The registry integrity test will catch any `actionId` that appears in the registry but lacks a `registerNoticeAction` call.
---
### Retire a notice (stop showing it)
**Do not delete the entry.** Set `maxVersion` to the last app version on which the notice should appear. Once the app is upgraded past that version, the service filters it out automatically. The database row for dismissed users remains harmless.
```typescript
{
id: 'old-campaign',
// ... all existing fields unchanged ...
maxVersion: '3.1.0', // hidden once 3.1.0 ships (exclusive upper bound)
}
```
To scope a notice to a specific version window (e.g. a v3-only announcement), combine both bounds:
```typescript
{
id: 'v3-only',
minVersion: '3.0.0',
maxVersion: '4.0.0', // shown on >= 3.0.0 and < 4.0.0
}
```
---
### Show a notice only during a campaign window
Combine `dateWindow` with any other targeting conditions:
```typescript
conditions: [
{ kind: 'dateWindow', startsAt: '2026-06-15T00:00:00Z', endsAt: '2026-06-30T23:59:59Z' },
{ kind: 'role', roles: ['admin'] },
],
```
---
### Show a notice only if an addon is enabled
```typescript
conditions: [
{ kind: 'addonEnabled', addonId: 'journey' },
],
```
Addon IDs are the string values in `server/src/addons.ts` β `ADDON_IDS`.
---
### Add a custom condition
```typescript
// server/src/startup.ts (or wherever your bootstrap code runs)
import { registerPredicate } from './systemNotices/conditions.js';
registerPredicate('has-no-profile-photo', (ctx) => {
const row = db.prepare('SELECT avatar FROM users WHERE id = ?').get(ctx.user.id);
return !row?.avatar;
});
```
Then reference it in the registry:
```typescript
conditions: [{ kind: 'custom', id: 'has-no-profile-photo' }],
```
---
### Create a multipage upgrade announcement
Give multiple notices the same `conditions` and adjacent `priority` values. The pager groups all active modal notices together automatically β no extra wiring required.
```typescript
// Page 1 β breaking change (higher priority, warn severity)
{ id: 'v4-breaking', priority: 90, severity: 'warn', conditions: [{ kind: 'existingUserBeforeVersion', version: '4.0.0' }], ... },
// Page 2 β new feature (lower priority, info severity)
{ id: 'v4-feature', priority: 80, severity: 'info', conditions: [{ kind: 'existingUserBeforeVersion', version: '4.0.0' }], ... },
```
Users who have already dismissed page 1 will only see page 2 on their next session.
---
## 13. Testing
### Server unit tests
**`server/tests/unit/systemNotices/conditions.test.ts`**
Tests each condition kind in isolation using `evaluate()` directly. No DB required.
**`server/tests/unit/systemNotices/registry.test.ts`**
Validates registry integrity:
- No duplicate `id` values
- All `action` CTA `actionId`s have a corresponding `registerNoticeAction()` call in the client source (scanned via regex β no JSON file needed)
- All `publishedAt` values parse as valid ISO dates
Run: `cd server && npx vitest run tests/unit/systemNotices/`
**`server/tests/integration/systemNotices.test.ts`**
Integration tests against a real in-memory SQLite database:
- `GET /api/system-notices/active` returns 401 without auth, returns correct notices per user state
- `POST /api/system-notices/:id/dismiss` stores the dismissal and filters on subsequent requests
- Dismissing an unknown ID returns 404
Run: `cd server && npx vitest run tests/integration/systemNotices.test.ts`
---
### Client unit tests
**`client/src/components/SystemNotices/SystemNoticeModal.test.tsx`**
Tests `ModalRenderer` with fake timers (`vi.useFakeTimers()`) and MSW for the dismiss endpoint. Key helpers:
```typescript
// Flush the 500 ms grace delay that gates the modal's visible state
async function flushGraceDelay() {
await act(async () => { vi.runAllTimers(); });
}
// Minimal notice factory
function makeNotice(overrides?: Partial<SystemNoticeDTO>): SystemNoticeDTO
```
Covered cases (FE-SN-MODAL-001 to 018):
- Grace delay before visibility
- Dismiss button, X button, ESC key
- Non-dismissible notices (all affordances blocked)
- CTA nav button β dismisses all notices
- Body param interpolation
- Pager: counter, dots, prev/next buttons, keyboard arrows, dot click, non-dismissible lock
- Dismiss-does-not-skip regression
- X and ESC dismiss all in multipage scenario
- Last notice close
Run: `cd client && npm run test -- SystemNoticeModal`
---
### Running all notice tests
```bash
cd server && npx vitest run tests/unit/systemNotices/ tests/integration/systemNotices.test.ts
cd client && npm run test -- SystemNoticeModal
```
---
## 14. Rules & constraints
| Rule | Reason |
|---|---|
| Never delete or reuse a notice `id` | Dismissal records are keyed by `id`. Deletion causes dismissed users to see the notice again. |
| Never use literal newlines in translation strings | Single-quoted TS strings with literal newlines cause esbuild parse errors. Use `\n\n` (escaped). |
| Never hardcode version numbers or dates in translation strings | Use `bodyParams` so strings stay translatable without retranslation per release. |
| `critical` severity must have `dismissible: false` | `critical` toasts are auto-dismissed with a warning; a dismissible critical modal is inconsistent UX. |
| `critical` must not use `display: 'toast'` | The toast renderer logs a warning and auto-dismisses critical toasts rather than showing them. |
| CTA labels β€ 20 chars, sentence case, a verb | Consistent button copy across the app. |
| Priorities must be set explicitly for upgrade notices | Adjacent notices form a multipage group; ordering matters for the reading flow. |
| `action` CTA `actionId` must be registered client-side | The registry integrity test enforces this. Add both the registry entry and the `registerNoticeAction` call in the same PR. |
| `maxVersion` over deletion for retiring notices | See Β§12. Deletion would cause dismissed users to re-see the notice if the ID were ever reused. |
|