@l.x/notifications 1.0.3 → 1.0.5
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/.depcheckrc +14 -0
- package/.eslintrc.js +20 -0
- package/LICENSE +122 -0
- package/README.md +548 -0
- package/package.json +42 -1
- package/project.json +24 -0
- package/src/getIsNotificationServiceLocalOverrideEnabled.ts +7 -0
- package/src/global.d.ts +2 -0
- package/src/index.ts +41 -0
- package/src/notification-data-source/NotificationDataSource.ts +10 -0
- package/src/notification-data-source/getNotificationQueryOptions.ts +85 -0
- package/src/notification-data-source/implementations/createIntervalNotificationDataSource.ts +77 -0
- package/src/notification-data-source/implementations/createLocalTriggerDataSource.test.ts +492 -0
- package/src/notification-data-source/implementations/createLocalTriggerDataSource.ts +177 -0
- package/src/notification-data-source/implementations/createNotificationDataSource.ts +19 -0
- package/src/notification-data-source/implementations/createPollingNotificationDataSource.test.ts +398 -0
- package/src/notification-data-source/implementations/createPollingNotificationDataSource.ts +74 -0
- package/src/notification-data-source/implementations/createReactiveDataSource.ts +113 -0
- package/src/notification-data-source/types/ReactiveCondition.ts +60 -0
- package/src/notification-processor/NotificationProcessor.ts +26 -0
- package/src/notification-processor/implementations/createBaseNotificationProcessor.test.ts +854 -0
- package/src/notification-processor/implementations/createBaseNotificationProcessor.ts +254 -0
- package/src/notification-processor/implementations/createNotificationProcessor.test.ts +130 -0
- package/src/notification-processor/implementations/createNotificationProcessor.ts +15 -0
- package/src/notification-renderer/NotificationRenderer.ts +8 -0
- package/src/notification-renderer/components/BannerTemplate.tsx +188 -0
- package/src/notification-renderer/components/InlineBannerNotification.tsx +123 -0
- package/src/notification-renderer/implementations/createNotificationRenderer.ts +16 -0
- package/src/notification-renderer/utils/iconUtils.ts +103 -0
- package/src/notification-service/NotificationService.ts +49 -0
- package/src/notification-service/implementations/createNotificationService.test.ts +1092 -0
- package/src/notification-service/implementations/createNotificationService.ts +368 -0
- package/src/notification-telemetry/NotificationTelemetry.ts +44 -0
- package/src/notification-telemetry/implementations/createNotificationTelemetry.test.ts +99 -0
- package/src/notification-telemetry/implementations/createNotificationTelemetry.ts +33 -0
- package/src/notification-tracker/NotificationTracker.ts +14 -0
- package/src/notification-tracker/implementations/createApiNotificationTracker.test.ts +465 -0
- package/src/notification-tracker/implementations/createApiNotificationTracker.ts +154 -0
- package/src/notification-tracker/implementations/createNoopNotificationTracker.ts +44 -0
- package/src/notification-tracker/implementations/createNotificationTracker.ts +31 -0
- package/src/utils/formatNotificationType.test.ts +25 -0
- package/src/utils/formatNotificationType.ts +25 -0
- package/tsconfig.json +34 -0
- package/tsconfig.lint.json +8 -0
- package/vitest-setup.ts +1 -0
- package/vitest.config.ts +14 -0
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/.depcheckrc
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
ignores: [
|
|
2
|
+
# Test coverage tool
|
|
3
|
+
"@vitest/coverage-v8",
|
|
4
|
+
|
|
5
|
+
# Standard ignores
|
|
6
|
+
"typescript",
|
|
7
|
+
"@typescript/native-preview",
|
|
8
|
+
"depcheck",
|
|
9
|
+
|
|
10
|
+
# Internal packages / workspaces - used in test files
|
|
11
|
+
"@universe/notifications",
|
|
12
|
+
"@universe/api",
|
|
13
|
+
"utilities",
|
|
14
|
+
]
|
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
extends: ['@luxamm/eslint-config/lib'],
|
|
3
|
+
parserOptions: {
|
|
4
|
+
tsconfigRootDir: __dirname,
|
|
5
|
+
},
|
|
6
|
+
overrides: [
|
|
7
|
+
{
|
|
8
|
+
files: ['*.ts', '*.tsx'],
|
|
9
|
+
rules: {
|
|
10
|
+
'no-relative-import-paths/no-relative-import-paths': [
|
|
11
|
+
'error',
|
|
12
|
+
{
|
|
13
|
+
allowSameFolder: false,
|
|
14
|
+
prefix: '@luxexchange/notifications',
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Lux Ecosystem License
|
|
2
|
+
Version 1.2, December 2025
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2020-2025 Lux Industries Inc.
|
|
5
|
+
All rights reserved.
|
|
6
|
+
|
|
7
|
+
TECHNOLOGY PORTFOLIO - PATENT APPLICATIONS PLANNED
|
|
8
|
+
Contact: licensing@lux.network
|
|
9
|
+
|
|
10
|
+
================================================================================
|
|
11
|
+
TERMS AND CONDITIONS
|
|
12
|
+
================================================================================
|
|
13
|
+
|
|
14
|
+
1. DEFINITIONS
|
|
15
|
+
|
|
16
|
+
"Lux Primary Network" means the official Lux blockchain with Network ID=1
|
|
17
|
+
and EVM Chain ID=96369.
|
|
18
|
+
|
|
19
|
+
"Authorized Network" means the Lux Primary Network, official testnets/devnets,
|
|
20
|
+
and any L1/L2/L3 chain descending from the Lux Primary Network.
|
|
21
|
+
|
|
22
|
+
"Descending Chain" means an L1/L2/L3 chain built on, anchored to, or deriving
|
|
23
|
+
security from the Lux Primary Network or its authorized testnets.
|
|
24
|
+
|
|
25
|
+
"Research Use" means non-commercial academic research, education, personal
|
|
26
|
+
study, or evaluation purposes.
|
|
27
|
+
|
|
28
|
+
"Commercial Use" means any use in connection with a product or service
|
|
29
|
+
offered for sale or fee, internal use by a for-profit entity, or any use
|
|
30
|
+
to generate revenue.
|
|
31
|
+
|
|
32
|
+
2. GRANT OF LICENSE
|
|
33
|
+
|
|
34
|
+
Subject to these terms, Lux Industries Inc grants you a non-exclusive,
|
|
35
|
+
royalty-free license to:
|
|
36
|
+
|
|
37
|
+
(a) Use for Research Use without restriction;
|
|
38
|
+
|
|
39
|
+
(b) Operate on the Lux Primary Network (Network ID=1, EVM Chain ID=96369);
|
|
40
|
+
|
|
41
|
+
(c) Operate on official Lux testnets and devnets;
|
|
42
|
+
|
|
43
|
+
(d) Operate L1/L2/L3 chains descending from the Lux Primary Network;
|
|
44
|
+
|
|
45
|
+
(e) Build applications within the Lux ecosystem;
|
|
46
|
+
|
|
47
|
+
(f) Contribute improvements back to the original repositories.
|
|
48
|
+
|
|
49
|
+
3. RESTRICTIONS
|
|
50
|
+
|
|
51
|
+
Without a commercial license from Lux Industries Inc, you may NOT:
|
|
52
|
+
|
|
53
|
+
(a) Fork the Lux Network or any Lux software;
|
|
54
|
+
|
|
55
|
+
(b) Create competing networks not descending from Lux Primary Network;
|
|
56
|
+
|
|
57
|
+
(c) Use for Commercial Use outside the Lux ecosystem;
|
|
58
|
+
|
|
59
|
+
(d) Sublicense or transfer rights outside the Lux ecosystem;
|
|
60
|
+
|
|
61
|
+
(e) Use to create competing blockchain networks, exchanges, custody
|
|
62
|
+
services, or cryptographic systems outside the Lux ecosystem.
|
|
63
|
+
|
|
64
|
+
4. NO FORKS POLICY
|
|
65
|
+
|
|
66
|
+
Lux Industries Inc maintains ZERO TOLERANCE for unauthorized forks.
|
|
67
|
+
Any fork or deployment on an unauthorized network constitutes:
|
|
68
|
+
|
|
69
|
+
(a) Breach of this license;
|
|
70
|
+
(b) Grounds for immediate legal action.
|
|
71
|
+
|
|
72
|
+
5. RIGHTS RESERVATION
|
|
73
|
+
|
|
74
|
+
All rights not explicitly granted are reserved by Lux Industries Inc.
|
|
75
|
+
|
|
76
|
+
We plan to apply for patent protection for the technology in this
|
|
77
|
+
repository. Any implementation outside the Lux ecosystem may require
|
|
78
|
+
a separate commercial license.
|
|
79
|
+
|
|
80
|
+
6. DISCLAIMER OF WARRANTY
|
|
81
|
+
|
|
82
|
+
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
83
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
84
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
85
|
+
|
|
86
|
+
7. LIMITATION OF LIABILITY
|
|
87
|
+
|
|
88
|
+
IN NO EVENT SHALL LUX INDUSTRIES INC BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
89
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
90
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
|
|
91
|
+
|
|
92
|
+
8. TERMINATION
|
|
93
|
+
|
|
94
|
+
This license terminates immediately upon any breach, including but not
|
|
95
|
+
limited to deployment on unauthorized networks or creation of forks.
|
|
96
|
+
|
|
97
|
+
9. GOVERNING LAW
|
|
98
|
+
|
|
99
|
+
This License shall be governed by the laws of the State of Delaware.
|
|
100
|
+
|
|
101
|
+
10. COMMERCIAL LICENSING
|
|
102
|
+
|
|
103
|
+
For commercial use outside the Lux ecosystem:
|
|
104
|
+
|
|
105
|
+
Lux Industries Inc.
|
|
106
|
+
Email: licensing@lux.network
|
|
107
|
+
Subject: Commercial License Request
|
|
108
|
+
|
|
109
|
+
================================================================================
|
|
110
|
+
TL;DR
|
|
111
|
+
================================================================================
|
|
112
|
+
|
|
113
|
+
- Research/academic use = OK
|
|
114
|
+
- Lux Primary Network (Network ID=1, Chain ID=96369) = OK
|
|
115
|
+
- L1/L2/L3 chains descending from Lux Primary Network = OK
|
|
116
|
+
- Commercial products outside Lux ecosystem = Contact licensing@lux.network
|
|
117
|
+
- Forks = Absolutely not
|
|
118
|
+
|
|
119
|
+
================================================================================
|
|
120
|
+
|
|
121
|
+
See LP-0012 for full licensing documentation:
|
|
122
|
+
https://github.com/luxfi/lps/blob/main/LPs/lp-0012-ecosystem-licensing.md
|
package/README.md
ADDED
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
# @universe/notifications
|
|
2
|
+
|
|
3
|
+
A platform-agnostic notification service for fetching, processing, storing, and displaying in-app notifications across web, mobile, and extension platforms.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Architecture](#architecture)
|
|
8
|
+
- [Core Concepts](#core-concepts)
|
|
9
|
+
- [Notification Types](#notification-types-contentstyle)
|
|
10
|
+
- [Data Source Patterns](#data-source-patterns)
|
|
11
|
+
- [Notification Chains](#notification-chains)
|
|
12
|
+
- [Notification ID Conventions](#notification-id-conventions)
|
|
13
|
+
- [User Actions](#user-actions-onclickaction)
|
|
14
|
+
- [Getting Started](#getting-started)
|
|
15
|
+
- [Platform Integrations](#platform-integrations)
|
|
16
|
+
- [Web](#web-integration)
|
|
17
|
+
- [Mobile](#mobile-integration)
|
|
18
|
+
- [Extension](#extension-integration)
|
|
19
|
+
- [Common Patterns](#common-patterns)
|
|
20
|
+
- [Testing](#testing)
|
|
21
|
+
- [Troubleshooting](#troubleshooting)
|
|
22
|
+
- [API Reference](#api-reference)
|
|
23
|
+
|
|
24
|
+
## Architecture
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
┌─────────────────────────────────────────────────────────────────────────┐
|
|
28
|
+
│ NotificationService │
|
|
29
|
+
│ (Orchestrator) │
|
|
30
|
+
├─────────────────────────────────────────────────────────────────────────┤
|
|
31
|
+
│ │
|
|
32
|
+
│ ┌──────────────────┐ ┌───────────────────┐ ┌──────────────┐ │
|
|
33
|
+
│ │ DataSources │────▶│ Processor │────▶│ Renderer │ │
|
|
34
|
+
│ │ │ │ │ │ │ │
|
|
35
|
+
│ │ • Polling (API) │ │ • Filters tracked │ │ • canRender │ │
|
|
36
|
+
│ │ • Reactive │ │ • Limits by style │ │ • render() │ │
|
|
37
|
+
│ │ • LocalTrigger │ │ • Separates chains│ │ • cleanup() │ │
|
|
38
|
+
│ │ • Interval │ │ │ │ │ │
|
|
39
|
+
│ └──────────────────┘ └───────────────────┘ └──────────────┘ │
|
|
40
|
+
│ │ │ │ │
|
|
41
|
+
│ │ ▼ │ │
|
|
42
|
+
│ │ ┌───────────────────┐ │ │
|
|
43
|
+
│ │ │ Tracker │◀───────────┘ │
|
|
44
|
+
│ │ │ │ │
|
|
45
|
+
│ │ │ • isProcessed() │ │
|
|
46
|
+
│ │ │ • track() │ │
|
|
47
|
+
│ │ │ • cleanup() │ │
|
|
48
|
+
│ │ └───────────────────┘ │
|
|
49
|
+
│ │ │ │
|
|
50
|
+
│ ▼ ▼ │
|
|
51
|
+
│ ┌─────────────────────────────────────────────────────────────────┐ │
|
|
52
|
+
│ │ Telemetry │ │
|
|
53
|
+
│ │ onNotificationReceived → onNotificationShown → onInteracted │ │
|
|
54
|
+
│ └─────────────────────────────────────────────────────────────────┘ │
|
|
55
|
+
│ │
|
|
56
|
+
└─────────────────────────────────────────────────────────────────────────┘
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Component Responsibilities
|
|
60
|
+
|
|
61
|
+
| Component | Responsibility |
|
|
62
|
+
|-----------|----------------|
|
|
63
|
+
| **DataSource** | Feeds notifications to the service (API polling, state subscriptions, local triggers) |
|
|
64
|
+
| **Processor** | Filters tracked notifications, enforces style limits, separates primary vs chained |
|
|
65
|
+
| **Renderer** | Platform-specific UI rendering with canRender/render/cleanup lifecycle |
|
|
66
|
+
| **Tracker** | Tracks acknowledged notifications to prevent re-showing |
|
|
67
|
+
| **Telemetry** | Reports lifecycle events (received, shown, interacted) to analytics |
|
|
68
|
+
|
|
69
|
+
### Lifecycle
|
|
70
|
+
|
|
71
|
+
1. **Initialize** — Service starts all data sources
|
|
72
|
+
2. **Receive** — Data sources emit notifications to the service
|
|
73
|
+
3. **Process** — Processor filters and categorizes notifications
|
|
74
|
+
4. **Render** — Renderer displays primary notifications, stores chained for later
|
|
75
|
+
5. **Interact** — User clicks trigger actions (DISMISS, ACK, POPUP, EXTERNAL_LINK)
|
|
76
|
+
6. **Track** — ACK action marks notification as processed
|
|
77
|
+
7. **Destroy** — Service cleans up data sources and active renders
|
|
78
|
+
|
|
79
|
+
## Core Concepts
|
|
80
|
+
|
|
81
|
+
### Notification Types (ContentStyle)
|
|
82
|
+
|
|
83
|
+
| Style | Description | Max Concurrent | Typical Use |
|
|
84
|
+
|-------|-------------|----------------|-------------|
|
|
85
|
+
| `MODAL` | Full-screen overlays requiring user action | 1 | Feature announcements, onboarding flows |
|
|
86
|
+
| `SYSTEM_BANNER` | System alerts (typically at screen bottom) | 1 | Offline status, storage warnings |
|
|
87
|
+
| `LOWER_LEFT_BANNER` | Promotional banners stacked in lower-left | 3 | Chain promotions, feature highlights |
|
|
88
|
+
|
|
89
|
+
### Data Source Patterns
|
|
90
|
+
|
|
91
|
+
The service supports four patterns for feeding notifications:
|
|
92
|
+
|
|
93
|
+
#### 1. Polling (Backend API via React Query)
|
|
94
|
+
|
|
95
|
+
Best for: Backend-driven notifications with automatic caching and refetch.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { createPollingNotificationDataSource, getNotificationQueryOptions } from '@universe/notifications'
|
|
99
|
+
|
|
100
|
+
const pollingDataSource = createPollingNotificationDataSource({
|
|
101
|
+
queryClient,
|
|
102
|
+
queryOptions: getNotificationQueryOptions({ address, platformType, appId }),
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### 2. Reactive (Push-based Subscriptions)
|
|
107
|
+
|
|
108
|
+
Best for: Instant response to state changes (network status, storage quota).
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { createReactiveDataSource, type ReactiveCondition } from '@universe/notifications'
|
|
112
|
+
|
|
113
|
+
const offlineCondition: ReactiveCondition<{ isConnected: boolean }> = {
|
|
114
|
+
notificationId: 'local:session:offline',
|
|
115
|
+
subscribe: (onStateChange) => {
|
|
116
|
+
return NetInfo.addEventListener((state) => {
|
|
117
|
+
onStateChange({ isConnected: state.isConnected })
|
|
118
|
+
})
|
|
119
|
+
},
|
|
120
|
+
shouldShow: (state) => state.isConnected === false,
|
|
121
|
+
createNotification: () => new Notification({ /* ... */ }),
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const reactiveDataSource = createReactiveDataSource({
|
|
125
|
+
condition: offlineCondition,
|
|
126
|
+
tracker,
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### 3. LocalTrigger (Condition-based Polling)
|
|
131
|
+
|
|
132
|
+
Best for: Periodic checks of local state (Redux selectors, app conditions).
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { createLocalTriggerDataSource, type TriggerCondition } from '@universe/notifications'
|
|
136
|
+
|
|
137
|
+
const backupTrigger: TriggerCondition = {
|
|
138
|
+
id: 'local:backup_reminder',
|
|
139
|
+
shouldShow: async () => {
|
|
140
|
+
const account = selectActiveAccount(getState())
|
|
141
|
+
return account && !hasExternalBackup(account)
|
|
142
|
+
},
|
|
143
|
+
createNotification: () => new Notification({ /* ... */ }),
|
|
144
|
+
onAcknowledge: () => dispatch(setLastSeenTs(Date.now())),
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const localTriggerDataSource = createLocalTriggerDataSource({
|
|
148
|
+
triggers: [backupTrigger],
|
|
149
|
+
tracker,
|
|
150
|
+
pollIntervalMs: 5000,
|
|
151
|
+
})
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### 4. Interval (Simple Periodic Checks)
|
|
155
|
+
|
|
156
|
+
Best for: Custom periodic notification fetching.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { createIntervalNotificationDataSource } from '@universe/notifications'
|
|
160
|
+
|
|
161
|
+
const intervalDataSource = createIntervalNotificationDataSource({
|
|
162
|
+
pollIntervalMs: 30000,
|
|
163
|
+
source: 'legacy_banners',
|
|
164
|
+
logFileTag: 'LegacyBanners',
|
|
165
|
+
getNotifications: async () => fetchLegacyBanners(),
|
|
166
|
+
})
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Notification Chains
|
|
170
|
+
|
|
171
|
+
Notifications can trigger follow-up notifications using the `POPUP` action:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
// Step 1: User sees welcome banner
|
|
175
|
+
{
|
|
176
|
+
id: 'welcome_step_1',
|
|
177
|
+
content: {
|
|
178
|
+
buttons: [{
|
|
179
|
+
text: 'Learn More',
|
|
180
|
+
onClick: {
|
|
181
|
+
onClick: [OnClickAction.DISMISS, OnClickAction.POPUP],
|
|
182
|
+
onClickLink: 'welcome_step_2' // ← triggers next notification
|
|
183
|
+
}
|
|
184
|
+
}]
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Step 2: Detailed modal (stored as "chained" until triggered)
|
|
189
|
+
{
|
|
190
|
+
id: 'welcome_step_2',
|
|
191
|
+
content: { style: ContentStyle.MODAL, /* ... */ }
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
The processor automatically identifies root vs chained notifications using graph analysis.
|
|
196
|
+
|
|
197
|
+
### Notification ID Conventions
|
|
198
|
+
|
|
199
|
+
| Prefix | Storage | Behavior |
|
|
200
|
+
|--------|---------|----------|
|
|
201
|
+
| (none) | API + localStorage | Permanent tracking, synced with backend |
|
|
202
|
+
| `local:` | localStorage only | Permanent tracking, local only (no API calls) |
|
|
203
|
+
| `local:session:` | sessionStorage | Resets on app restart (e.g., offline banner) |
|
|
204
|
+
|
|
205
|
+
### User Actions (OnClickAction)
|
|
206
|
+
|
|
207
|
+
| Action | Effect |
|
|
208
|
+
|--------|--------|
|
|
209
|
+
| `DISMISS` | Hides the notification (can reappear if not ACK'd) |
|
|
210
|
+
| `ACK` | Marks as acknowledged, prevents re-showing |
|
|
211
|
+
| `POPUP` | Shows the notification specified in `onClickLink` |
|
|
212
|
+
| `EXTERNAL_LINK` | Navigates to URL in `onClickLink` via `onNavigate` handler |
|
|
213
|
+
|
|
214
|
+
Actions are combined in arrays and executed sequentially:
|
|
215
|
+
```typescript
|
|
216
|
+
onClick: [OnClickAction.DISMISS, OnClickAction.ACK] // Hide and mark processed
|
|
217
|
+
onClick: [OnClickAction.DISMISS, OnClickAction.POPUP] // Hide and show next
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Getting Started
|
|
221
|
+
|
|
222
|
+
### Initialize the Service
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
import {
|
|
226
|
+
createNotificationService,
|
|
227
|
+
createPollingNotificationDataSource,
|
|
228
|
+
createBaseNotificationProcessor,
|
|
229
|
+
createNotificationTracker,
|
|
230
|
+
createNotificationRenderer,
|
|
231
|
+
createNotificationTelemetry,
|
|
232
|
+
} from '@universe/notifications'
|
|
233
|
+
|
|
234
|
+
const notificationService = createNotificationService({
|
|
235
|
+
dataSources: [
|
|
236
|
+
createPollingNotificationDataSource({ queryClient, queryOptions }),
|
|
237
|
+
// Add more data sources as needed
|
|
238
|
+
],
|
|
239
|
+
tracker: createNotificationTracker(storageAdapter),
|
|
240
|
+
processor: createBaseNotificationProcessor(tracker),
|
|
241
|
+
renderer: createNotificationRenderer({ onRender, canRender }),
|
|
242
|
+
telemetry: createNotificationTelemetry({ analytics }),
|
|
243
|
+
onNavigate: (url) => window.open(url, '_blank'),
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
await notificationService.initialize()
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Handle User Interactions
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
// When user clicks a button (index 0)
|
|
253
|
+
notificationService.onNotificationClick(notificationId, { type: 'button', index: 0 })
|
|
254
|
+
|
|
255
|
+
// When user clicks the dismiss/close button
|
|
256
|
+
notificationService.onNotificationClick(notificationId, { type: 'dismiss' })
|
|
257
|
+
|
|
258
|
+
// When user clicks the background
|
|
259
|
+
notificationService.onNotificationClick(notificationId, { type: 'background' })
|
|
260
|
+
|
|
261
|
+
// When notification is shown to user (for telemetry)
|
|
262
|
+
notificationService.onNotificationShown(notificationId)
|
|
263
|
+
|
|
264
|
+
// When render fails (e.g., unknown notification style)
|
|
265
|
+
notificationService.onRenderFailed(notificationId)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Cleanup
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
// On unmount or navigation
|
|
272
|
+
notificationService.destroy()
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Platform Integrations
|
|
276
|
+
|
|
277
|
+
### Web Integration
|
|
278
|
+
|
|
279
|
+
**Location:** `apps/web/src/notification-service/`
|
|
280
|
+
|
|
281
|
+
```
|
|
282
|
+
apps/web/src/notification-service/
|
|
283
|
+
├── WebNotificationService.tsx # Service initialization
|
|
284
|
+
├── createLocalStorageAdapter.ts # localStorage-based tracker
|
|
285
|
+
├── notification-renderer/
|
|
286
|
+
│ ├── NotificationContainer.tsx # Renders all notification types
|
|
287
|
+
│ ├── StackedLowerLeftBanners.tsx # Framer-motion stacking animations
|
|
288
|
+
│ ├── notificationStore.ts # Zustand store for UI state
|
|
289
|
+
│ └── components/
|
|
290
|
+
│ └── SystemBannerNotification.tsx
|
|
291
|
+
├── data-sources/
|
|
292
|
+
│ ├── createLegacyBannersNotificationDataSource.ts
|
|
293
|
+
│ └── createSystemAlertsDataSource.ts
|
|
294
|
+
└── telemetry/
|
|
295
|
+
└── getNotificationTelemetry.ts
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
**Key features:**
|
|
299
|
+
- Zustand store pattern for UI state management
|
|
300
|
+
- Framer-motion animations for stacked banners
|
|
301
|
+
- localStorage-based notification tracking
|
|
302
|
+
|
|
303
|
+
### Mobile Integration
|
|
304
|
+
|
|
305
|
+
**Location:** `apps/mobile/src/notification-service/`
|
|
306
|
+
|
|
307
|
+
```
|
|
308
|
+
apps/mobile/src/notification-service/
|
|
309
|
+
├── MobileNotificationServiceManager.tsx # Service manager component
|
|
310
|
+
├── MobileNotificationService.ts # Service initialization
|
|
311
|
+
├── createMobileStorageAdapter.ts # MMKV-based tracker
|
|
312
|
+
├── handleNotificationNavigation.ts # Navigation handler
|
|
313
|
+
├── notification-renderer/
|
|
314
|
+
│ ├── NotificationContainer.tsx # Routes to custom renderers
|
|
315
|
+
│ ├── SystemBannerPortal.tsx # Portal for system banners
|
|
316
|
+
│ └── createMobileNotificationRenderer.ts
|
|
317
|
+
├── renderers/
|
|
318
|
+
│ ├── BackupReminderModalRenderer.tsx # Custom modal for backup reminder
|
|
319
|
+
│ └── OfflineBannerRenderer.tsx # Custom banner for offline state
|
|
320
|
+
├── triggers/
|
|
321
|
+
│ ├── backupReminderTrigger.ts # LocalTrigger example
|
|
322
|
+
│ └── createMobileLocalTriggerDataSource.ts
|
|
323
|
+
└── data-sources/
|
|
324
|
+
├── reactive/
|
|
325
|
+
│ └── offlineCondition.ts # ReactiveCondition example
|
|
326
|
+
└── banners/ # Legacy banner data sources
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
**Key features:**
|
|
330
|
+
- MMKV storage for high-performance tracking
|
|
331
|
+
- Custom renderers for platform-specific UI (BackupReminder, OfflineBanner)
|
|
332
|
+
- NetInfo integration for offline detection via reactive data source
|
|
333
|
+
|
|
334
|
+
### Extension Integration
|
|
335
|
+
|
|
336
|
+
**Location:** `apps/extension/src/notification-service/`
|
|
337
|
+
|
|
338
|
+
```
|
|
339
|
+
apps/extension/src/notification-service/
|
|
340
|
+
├── ExtensionNotificationServiceManager.tsx # Service manager
|
|
341
|
+
├── ExtensionNotificationService.tsx # Service initialization
|
|
342
|
+
├── createChromeStorageAdapter.ts # Chrome Storage API tracker
|
|
343
|
+
├── notification-renderer/
|
|
344
|
+
│ ├── NotificationContainer.tsx
|
|
345
|
+
│ └── notificationStore.ts
|
|
346
|
+
├── renderers/
|
|
347
|
+
│ ├── AppRatingModalRenderer.tsx
|
|
348
|
+
│ └── StorageWarningModalRenderer.tsx
|
|
349
|
+
├── triggers/
|
|
350
|
+
│ ├── appRatingTrigger.ts
|
|
351
|
+
│ └── createExtensionLocalTriggerDataSource.ts
|
|
352
|
+
└── data-sources/
|
|
353
|
+
└── reactive/
|
|
354
|
+
└── storageWarningCondition.ts # Storage quota monitoring
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
**Key features:**
|
|
358
|
+
- Chrome Storage API for cross-session persistence
|
|
359
|
+
- Storage quota monitoring via reactive data source
|
|
360
|
+
- Special navigation handling (e.g., `unitag://` protocol)
|
|
361
|
+
|
|
362
|
+
## Common Patterns
|
|
363
|
+
|
|
364
|
+
### Creating a Local Trigger
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
import { type TriggerCondition } from '@universe/notifications'
|
|
368
|
+
|
|
369
|
+
export const REMINDER_NOTIFICATION_ID = 'local:my_reminder'
|
|
370
|
+
|
|
371
|
+
interface CreateReminderTriggerContext {
|
|
372
|
+
getState: () => AppState
|
|
373
|
+
dispatch: (action: AnyAction) => void
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export function createReminderTrigger(ctx: CreateReminderTriggerContext): TriggerCondition {
|
|
377
|
+
const { getState, dispatch } = ctx
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
id: REMINDER_NOTIFICATION_ID,
|
|
381
|
+
|
|
382
|
+
shouldShow: async () => {
|
|
383
|
+
const state = getState()
|
|
384
|
+
const lastSeen = selectReminderLastSeen(state)
|
|
385
|
+
return Date.now() - lastSeen > ONE_DAY_MS
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
createNotification: () => new Notification({
|
|
389
|
+
id: REMINDER_NOTIFICATION_ID,
|
|
390
|
+
content: new Content({
|
|
391
|
+
style: ContentStyle.MODAL,
|
|
392
|
+
title: 'Reminder',
|
|
393
|
+
onDismissClick: new OnClick({
|
|
394
|
+
onClick: [OnClickAction.DISMISS, OnClickAction.ACK],
|
|
395
|
+
}),
|
|
396
|
+
}),
|
|
397
|
+
}),
|
|
398
|
+
|
|
399
|
+
onAcknowledge: () => {
|
|
400
|
+
dispatch(setReminderLastSeen(Date.now()))
|
|
401
|
+
},
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### Creating a Reactive Condition
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
import { type ReactiveCondition } from '@universe/notifications'
|
|
410
|
+
|
|
411
|
+
export const STATUS_NOTIFICATION_ID = 'local:session:status'
|
|
412
|
+
|
|
413
|
+
interface StatusState {
|
|
414
|
+
isActive: boolean
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function createStatusCondition(ctx: { getState: () => AppState }): ReactiveCondition<StatusState> {
|
|
418
|
+
return {
|
|
419
|
+
notificationId: STATUS_NOTIFICATION_ID,
|
|
420
|
+
|
|
421
|
+
subscribe: (onStateChange) => {
|
|
422
|
+
// Subscribe to external state changes
|
|
423
|
+
const unsubscribe = someService.addEventListener((event) => {
|
|
424
|
+
onStateChange({ isActive: event.isActive })
|
|
425
|
+
})
|
|
426
|
+
return unsubscribe
|
|
427
|
+
},
|
|
428
|
+
|
|
429
|
+
shouldShow: (state) => state.isActive === false,
|
|
430
|
+
|
|
431
|
+
createNotification: (state) => new Notification({
|
|
432
|
+
id: STATUS_NOTIFICATION_ID,
|
|
433
|
+
content: new Content({
|
|
434
|
+
style: ContentStyle.SYSTEM_BANNER,
|
|
435
|
+
title: 'Service unavailable',
|
|
436
|
+
onDismissClick: new OnClick({
|
|
437
|
+
onClick: [OnClickAction.DISMISS, OnClickAction.ACK],
|
|
438
|
+
}),
|
|
439
|
+
}),
|
|
440
|
+
}),
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### Custom Renderer with Notification Routing
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
import { createNotificationRenderer } from '@universe/notifications'
|
|
449
|
+
|
|
450
|
+
function isMyCustomNotification(notification: InAppNotification): boolean {
|
|
451
|
+
return notification.id === 'local:my_custom'
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const renderer = createNotificationRenderer({
|
|
455
|
+
canRender: (notification) => {
|
|
456
|
+
// Only one modal at a time
|
|
457
|
+
if (notification.content?.style === ContentStyle.MODAL) {
|
|
458
|
+
return !hasActiveModal()
|
|
459
|
+
}
|
|
460
|
+
return true
|
|
461
|
+
},
|
|
462
|
+
|
|
463
|
+
render: (notification) => {
|
|
464
|
+
// Route to appropriate renderer
|
|
465
|
+
if (isMyCustomNotification(notification)) {
|
|
466
|
+
return renderMyCustomModal(notification)
|
|
467
|
+
}
|
|
468
|
+
return renderDefaultNotification(notification)
|
|
469
|
+
},
|
|
470
|
+
})
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
## Testing
|
|
474
|
+
|
|
475
|
+
### Mock Data Source
|
|
476
|
+
|
|
477
|
+
```typescript
|
|
478
|
+
function createMockDataSource(): NotificationDataSource & {
|
|
479
|
+
emit: (notifications: InAppNotification[]) => void
|
|
480
|
+
} {
|
|
481
|
+
let callback: ((notifications: InAppNotification[], source: string) => void) | null = null
|
|
482
|
+
|
|
483
|
+
return {
|
|
484
|
+
start: (onNotifications) => { callback = onNotifications },
|
|
485
|
+
stop: async () => { callback = null },
|
|
486
|
+
emit: (notifications) => callback?.(notifications, 'mock'),
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// In tests
|
|
491
|
+
const mockDataSource = createMockDataSource()
|
|
492
|
+
const service = createNotificationService({
|
|
493
|
+
dataSources: [mockDataSource],
|
|
494
|
+
// ...
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
await service.initialize()
|
|
498
|
+
mockDataSource.emit([testNotification])
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
### Mock Tracker
|
|
502
|
+
|
|
503
|
+
```typescript
|
|
504
|
+
function createMockTracker(): NotificationTracker {
|
|
505
|
+
const processed = new Set<string>()
|
|
506
|
+
|
|
507
|
+
return {
|
|
508
|
+
isProcessed: async (id) => processed.has(id),
|
|
509
|
+
getProcessedIds: async () => processed,
|
|
510
|
+
track: async (id) => { processed.add(id) },
|
|
511
|
+
cleanup: async () => { processed.clear() },
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### Mock Renderer
|
|
517
|
+
|
|
518
|
+
```typescript
|
|
519
|
+
function createMockRenderer(): NotificationRenderer & {
|
|
520
|
+
rendered: InAppNotification[]
|
|
521
|
+
} {
|
|
522
|
+
const rendered: InAppNotification[] = []
|
|
523
|
+
|
|
524
|
+
return {
|
|
525
|
+
rendered,
|
|
526
|
+
canRender: () => true,
|
|
527
|
+
render: (notification) => {
|
|
528
|
+
rendered.push(notification)
|
|
529
|
+
return () => {
|
|
530
|
+
const index = rendered.indexOf(notification)
|
|
531
|
+
if (index > -1) rendered.splice(index, 1)
|
|
532
|
+
}
|
|
533
|
+
},
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
## Troubleshooting
|
|
539
|
+
|
|
540
|
+
| Issue | Cause | Solution |
|
|
541
|
+
|-------|-------|----------|
|
|
542
|
+
| Notification re-appears after dismiss | Using `DISMISS` without `ACK` | Add `ACK` to the onClick array |
|
|
543
|
+
| Notification never shows | Already tracked as processed | Clear storage or use a new notification ID |
|
|
544
|
+
| Chained notification not showing | Target not in chained map | Ensure API returns all chain members in same response |
|
|
545
|
+
| Multiple modals appearing | `canRender` not checking active modals | Implement modal count check in renderer |
|
|
546
|
+
| Session notification persists | Using `local:` instead of `local:session:` | Use `local:session:` prefix for session-scoped notifications |
|
|
547
|
+
| Reactive condition not updating | Not calling `onStateChange` | Ensure subscribe callback is invoked on state changes |
|
|
548
|
+
|