@b3dotfun/sdk 0.0.47-alpha.0 → 0.0.47-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +222 -3
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +5 -3
- package/dist/cjs/notifications/index.d.ts +3 -0
- package/dist/cjs/notifications/index.js +25 -0
- package/dist/cjs/notifications/react/hooks/index.d.ts +1 -0
- package/dist/cjs/notifications/react/hooks/index.js +17 -0
- package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +42 -0
- package/dist/cjs/notifications/react/hooks/useNotifications.js +148 -0
- package/dist/cjs/notifications/react/index.d.ts +1 -0
- package/dist/cjs/notifications/react/index.js +17 -0
- package/dist/cjs/notifications/services/api.d.ts +67 -0
- package/dist/cjs/notifications/services/api.js +184 -0
- package/dist/cjs/notifications/services/index.d.ts +1 -0
- package/dist/cjs/notifications/services/index.js +17 -0
- package/dist/cjs/notifications/types/index.d.ts +51 -0
- package/dist/cjs/notifications/types/index.js +2 -0
- package/dist/cjs/shared/utils/auth-token.d.ts +7 -0
- package/dist/cjs/shared/utils/auth-token.js +17 -0
- package/dist/cjs/shared/utils/index.d.ts +1 -0
- package/dist/cjs/shared/utils/index.js +1 -0
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -3
- package/dist/esm/notifications/index.d.ts +3 -0
- package/dist/esm/notifications/index.js +7 -0
- package/dist/esm/notifications/react/hooks/index.d.ts +1 -0
- package/dist/esm/notifications/react/hooks/index.js +1 -0
- package/dist/esm/notifications/react/hooks/useNotifications.d.ts +42 -0
- package/dist/esm/notifications/react/hooks/useNotifications.js +145 -0
- package/dist/esm/notifications/react/index.d.ts +1 -0
- package/dist/esm/notifications/react/index.js +1 -0
- package/dist/esm/notifications/services/api.d.ts +67 -0
- package/dist/esm/notifications/services/api.js +179 -0
- package/dist/esm/notifications/services/index.d.ts +1 -0
- package/dist/esm/notifications/services/index.js +1 -0
- package/dist/esm/notifications/types/index.d.ts +51 -0
- package/dist/esm/shared/utils/auth-token.d.ts +7 -0
- package/dist/esm/shared/utils/auth-token.js +11 -0
- package/dist/esm/shared/utils/index.d.ts +1 -0
- package/dist/esm/shared/utils/index.js +1 -0
- package/dist/types/notifications/index.d.ts +3 -0
- package/dist/types/notifications/react/hooks/index.d.ts +1 -0
- package/dist/types/notifications/react/hooks/useNotifications.d.ts +42 -0
- package/dist/types/notifications/react/index.d.ts +1 -0
- package/dist/types/notifications/services/api.d.ts +67 -0
- package/dist/types/notifications/services/index.d.ts +1 -0
- package/dist/types/notifications/types/index.d.ts +51 -0
- package/dist/types/shared/utils/auth-token.d.ts +7 -0
- package/dist/types/shared/utils/index.d.ts +1 -0
- package/package.json +21 -1
- package/src/anyspend/react/components/AnySpendCustom.tsx +5 -3
- package/src/notifications/index.ts +9 -0
- package/src/notifications/react/hooks/index.ts +1 -0
- package/src/notifications/react/hooks/useNotifications.ts +153 -0
- package/src/notifications/react/index.ts +1 -0
- package/src/notifications/services/api.ts +217 -0
- package/src/notifications/services/index.ts +1 -0
- package/src/notifications/types/index.ts +58 -0
- package/src/shared/utils/auth-token.ts +13 -0
- package/src/shared/utils/index.ts +1 -0
- package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -245
- package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
- package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -243
- package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
- /package/dist/{cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts → esm/notifications/types/index.js} +0 -0
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ The SDK is organized into focused modules:
|
|
|
8
8
|
|
|
9
9
|
- **`anyspend/`** - Cross-chain execution engine functionality
|
|
10
10
|
- **`global-account/`** - B3 Global Accounts authentication and user management
|
|
11
|
+
- **`notifications/`** - Multi-channel notification management
|
|
11
12
|
- **`shared/`** - Common utilities, types, and components
|
|
12
13
|
- **`others/`** - Additional features and utilities
|
|
13
14
|
|
|
@@ -25,6 +26,7 @@ Each module contains:
|
|
|
25
26
|
| ----------------- | --------- | ------------ |
|
|
26
27
|
| AnySpend | ✅ | ❌ |
|
|
27
28
|
| Global Accounts | ✅ | ✅ |
|
|
29
|
+
| Notifications | ✅ | ✅ |
|
|
28
30
|
| Headless Services | ✅ | ✅ |
|
|
29
31
|
|
|
30
32
|
## Installation
|
|
@@ -155,9 +157,7 @@ const order = await anyspendService.createOrder({
|
|
|
155
157
|
import { anyspendService } from "@b3dotfun/sdk/anyspend/services/anyspend";
|
|
156
158
|
|
|
157
159
|
// Get order details and transactions
|
|
158
|
-
const orderDetails = await anyspendService.getOrderAndTransactions(
|
|
159
|
-
"order-id",
|
|
160
|
-
);
|
|
160
|
+
const orderDetails = await anyspendService.getOrderAndTransactions("order-id");
|
|
161
161
|
|
|
162
162
|
// Get order history for an address
|
|
163
163
|
const history = await anyspendService.getOrderHistory(
|
|
@@ -323,6 +323,206 @@ import { serverFunction } from "@b3dotfun/sdk/global-account/server";
|
|
|
323
323
|
// (Implementation depends on your specific needs)
|
|
324
324
|
```
|
|
325
325
|
|
|
326
|
+
# Notifications
|
|
327
|
+
|
|
328
|
+
B3 Notifications provides multi-channel notification management for your users with support for email, Telegram, Discord, SMS, WhatsApp, and in-app notifications.
|
|
329
|
+
|
|
330
|
+
## Features
|
|
331
|
+
|
|
332
|
+
- ✅ **Zero Config** - Automatically uses authenticated user's ID from JWT
|
|
333
|
+
- 🎯 **Type Safe** - Full TypeScript support
|
|
334
|
+
- ⚡ **React Hooks** - Easy integration with `useNotifications()` hook
|
|
335
|
+
- 🔐 **Secure** - JWT-based authentication
|
|
336
|
+
- 📱 **Multi-Channel** - Email, Telegram, Discord, SMS, WhatsApp, In-app
|
|
337
|
+
- 🚀 **Lightweight** - Minimal dependencies
|
|
338
|
+
|
|
339
|
+
## React Hook
|
|
340
|
+
|
|
341
|
+
### Basic Usage
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
import { useNotifications } from "@b3dotfun/sdk/notifications/react";
|
|
345
|
+
|
|
346
|
+
function NotificationSettings() {
|
|
347
|
+
const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
|
|
348
|
+
|
|
349
|
+
if (loading) return <div>Loading...</div>;
|
|
350
|
+
|
|
351
|
+
return (
|
|
352
|
+
<div>
|
|
353
|
+
{!isEmailConnected && <button onClick={() => connectEmail("user@example.com")}>Connect Email</button>}
|
|
354
|
+
|
|
355
|
+
<button onClick={connectTelegram}>Connect Telegram</button>
|
|
356
|
+
</div>
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Full Hook API
|
|
362
|
+
|
|
363
|
+
```tsx
|
|
364
|
+
const {
|
|
365
|
+
user, // User data with channels and settings
|
|
366
|
+
loading, // Loading state
|
|
367
|
+
error, // Error if any
|
|
368
|
+
refresh, // Manually refresh user data
|
|
369
|
+
connectEmail, // Connect email channel
|
|
370
|
+
connectTelegram, // Connect Telegram channel
|
|
371
|
+
updateChannel, // Update a channel
|
|
372
|
+
deleteChannel, // Delete a channel
|
|
373
|
+
// Convenience helpers
|
|
374
|
+
isEmailConnected,
|
|
375
|
+
isTelegramConnected,
|
|
376
|
+
isDiscordConnected,
|
|
377
|
+
} = useNotifications();
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Headless API Client
|
|
381
|
+
|
|
382
|
+
### Authentication
|
|
383
|
+
|
|
384
|
+
The notifications API automatically uses the B3 auth token from cookies. The token is managed by the B3 Global Account authentication system - you don't need to set it manually.
|
|
385
|
+
|
|
386
|
+
```typescript
|
|
387
|
+
import { getAuthToken, notificationsAPI } from "@b3dotfun/sdk/notifications";
|
|
388
|
+
// Or import from shared utils
|
|
389
|
+
// import { getAuthToken } from "@b3dotfun/sdk/shared/utils";
|
|
390
|
+
|
|
391
|
+
// Get the current auth token (from b3-auth cookie)
|
|
392
|
+
const token = getAuthToken();
|
|
393
|
+
|
|
394
|
+
// API calls automatically use the auth token
|
|
395
|
+
const userData = await notificationsAPI.getUser();
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### User Management
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
import { notificationsAPI } from "@b3dotfun/sdk/notifications";
|
|
402
|
+
|
|
403
|
+
// Register current user
|
|
404
|
+
await notificationsAPI.registerUser();
|
|
405
|
+
|
|
406
|
+
// Get current user's profile
|
|
407
|
+
const userData = await notificationsAPI.getUser();
|
|
408
|
+
|
|
409
|
+
// Get notification history
|
|
410
|
+
const history = await notificationsAPI.getHistory("my-app-id", 50);
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Channel Management
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
// Add any channel
|
|
417
|
+
await notificationsAPI.addChannel("email", "user@example.com");
|
|
418
|
+
|
|
419
|
+
// Connect email (shorthand)
|
|
420
|
+
await notificationsAPI.connectEmail("user@example.com");
|
|
421
|
+
|
|
422
|
+
// Update channel
|
|
423
|
+
await notificationsAPI.updateChannel("channel-id", {
|
|
424
|
+
enabled: true,
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
// Delete channel
|
|
428
|
+
await notificationsAPI.deleteChannel("channel-id");
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### Telegram Integration
|
|
432
|
+
|
|
433
|
+
```typescript
|
|
434
|
+
// Get Telegram deep link
|
|
435
|
+
const { deepLink, verificationCode, botUsername } = await notificationsAPI.getTelegramLink();
|
|
436
|
+
window.open(deepLink, "_blank");
|
|
437
|
+
|
|
438
|
+
// Check connection status
|
|
439
|
+
const { connected, chatId } = await notificationsAPI.checkTelegramStatus();
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### App Preferences
|
|
443
|
+
|
|
444
|
+
```typescript
|
|
445
|
+
// Save notification preferences for an app (enabled by default)
|
|
446
|
+
await notificationsAPI.savePreferences("my-app", {
|
|
447
|
+
notificationType: "transaction",
|
|
448
|
+
channels: ["email", "telegram", "in_app"],
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// Save disabled preferences
|
|
452
|
+
await notificationsAPI.savePreferences("my-app", {
|
|
453
|
+
notificationType: "transaction",
|
|
454
|
+
channels: ["email", "telegram"],
|
|
455
|
+
enabled: false,
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
// Get app settings
|
|
459
|
+
const settings = await notificationsAPI.getAppSettings("my-app");
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### In-App Notifications
|
|
463
|
+
|
|
464
|
+
```typescript
|
|
465
|
+
// Get in-app notifications
|
|
466
|
+
const { notifications } = await notificationsAPI.getInAppNotifications();
|
|
467
|
+
|
|
468
|
+
// Mark notification as read
|
|
469
|
+
await notificationsAPI.markNotificationAsRead("notification-id");
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Send Notifications
|
|
473
|
+
|
|
474
|
+
```typescript
|
|
475
|
+
// Send a notification
|
|
476
|
+
await notificationsAPI.sendNotification({
|
|
477
|
+
userId: "user-123",
|
|
478
|
+
appId: "my-app",
|
|
479
|
+
notificationType: "transaction",
|
|
480
|
+
message: "Your transaction was successful!",
|
|
481
|
+
title: "Transaction Complete",
|
|
482
|
+
data: { transactionId: "tx-123" },
|
|
483
|
+
});
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
## Next.js Example
|
|
487
|
+
|
|
488
|
+
```tsx
|
|
489
|
+
"use client";
|
|
490
|
+
|
|
491
|
+
import { useNotifications } from "@b3dotfun/sdk/notifications/react";
|
|
492
|
+
|
|
493
|
+
export default function NotificationsPage() {
|
|
494
|
+
const { user, loading, connectEmail, connectTelegram, isEmailConnected, isTelegramConnected } = useNotifications();
|
|
495
|
+
|
|
496
|
+
if (loading) {
|
|
497
|
+
return <div>Loading notification settings...</div>;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return (
|
|
501
|
+
<div className="space-y-4">
|
|
502
|
+
<h1>Notification Settings</h1>
|
|
503
|
+
|
|
504
|
+
<div className="rounded-lg border p-4">
|
|
505
|
+
<h2>Email</h2>
|
|
506
|
+
{isEmailConnected ? (
|
|
507
|
+
<p className="text-green-600">✓ Connected</p>
|
|
508
|
+
) : (
|
|
509
|
+
<button onClick={() => connectEmail("user@example.com")}>Connect Email</button>
|
|
510
|
+
)}
|
|
511
|
+
</div>
|
|
512
|
+
|
|
513
|
+
<div className="rounded-lg border p-4">
|
|
514
|
+
<h2>Telegram</h2>
|
|
515
|
+
{isTelegramConnected ? (
|
|
516
|
+
<p className="text-green-600">✓ Connected</p>
|
|
517
|
+
) : (
|
|
518
|
+
<button onClick={connectTelegram}>Connect Telegram</button>
|
|
519
|
+
)}
|
|
520
|
+
</div>
|
|
521
|
+
</div>
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
326
526
|
# Shared Utilities
|
|
327
527
|
|
|
328
528
|
## Chain Management
|
|
@@ -338,6 +538,25 @@ console.log(supportedChains);
|
|
|
338
538
|
console.log(b3Chain);
|
|
339
539
|
```
|
|
340
540
|
|
|
541
|
+
## Authentication Token Management
|
|
542
|
+
|
|
543
|
+
Read-only access to the B3 authentication token. The token is managed by the B3 Global Account authentication system.
|
|
544
|
+
|
|
545
|
+
```typescript
|
|
546
|
+
import { getAuthToken } from "@b3dotfun/sdk/shared/utils";
|
|
547
|
+
|
|
548
|
+
// Get the current auth token from the b3-auth cookie
|
|
549
|
+
const token = getAuthToken();
|
|
550
|
+
|
|
551
|
+
if (token) {
|
|
552
|
+
console.log("User is authenticated");
|
|
553
|
+
} else {
|
|
554
|
+
console.log("User is not authenticated");
|
|
555
|
+
}
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
The auth token is automatically set by the B3 Global Account authentication system (see the Global Accounts section above). All SDK modules that require authentication will automatically use this token.
|
|
559
|
+
|
|
341
560
|
## Utility Functions
|
|
342
561
|
|
|
343
562
|
```typescript
|
|
@@ -273,11 +273,13 @@ function AnySpendCustomInner({ loadOrder, mode = "modal", activeTab: activeTabPr
|
|
|
273
273
|
(0, react_4.useEffect)(() => {
|
|
274
274
|
if (oat?.data?.order.status === "executed" && !onSuccessCalled.current) {
|
|
275
275
|
console.log("Calling onSuccess");
|
|
276
|
-
const
|
|
276
|
+
const relayTxs = oat?.data?.relayTxs;
|
|
277
|
+
const lastRelayTxHash = relayTxs?.[relayTxs.length - 1]?.txHash;
|
|
278
|
+
const txHash = oat?.data?.executeTx?.txHash || lastRelayTxHash;
|
|
277
279
|
onSuccess?.(txHash);
|
|
278
280
|
onSuccessCalled.current = true;
|
|
279
281
|
}
|
|
280
|
-
}, [oat?.data?.order.status, oat?.data?.executeTx?.txHash, onSuccess]);
|
|
282
|
+
}, [oat?.data?.order.status, oat?.data?.executeTx?.txHash, oat?.data?.relayTxs, onSuccess]);
|
|
281
283
|
// Reset flag when orderId changes
|
|
282
284
|
(0, react_4.useEffect)(() => {
|
|
283
285
|
onSuccessCalled.current = false;
|
|
@@ -467,7 +469,7 @@ function AnySpendCustomInner({ loadOrder, mode = "modal", activeTab: activeTabPr
|
|
|
467
469
|
const historyView = ((0, jsx_runtime_1.jsx)("div", { className: (0, utils_1.cn)("mx-auto flex w-full max-w-2xl flex-col items-center p-5", mode === "modal" && "bg-b3-react-background"), children: (0, jsx_runtime_1.jsx)(OrderHistory_1.OrderHistory, { mode: mode, onBack: () => {
|
|
468
470
|
setActivePanel(PanelView.HISTORY);
|
|
469
471
|
}, onSelectOrder: onSelectOrder }) }));
|
|
470
|
-
const orderDetailsView = ((0, jsx_runtime_1.jsxs)("div", { className: (0, utils_1.cn)("mx-auto flex w-full flex-col items-center gap-4", mode === "modal" && "bg-b3-react-background rounded-xl"), children: [oat && ((0, jsx_runtime_1.jsx)(OrderDetails_1.OrderDetails, { mode: mode, order: oat.data.order, depositTxs: oat.data.depositTxs, relayTxs: oat.data.relayTxs, executeTx: oat.data.executeTx, refundTxs: oat.data.refundTxs, cryptoPaymentMethod: activeTab === "fiat" ? CryptoPaymentMethod_1.CryptoPaymentMethodType.NONE : selectedCryptoPaymentMethod, selectedCryptoPaymentMethod: selectedCryptoPaymentMethod, onPaymentMethodChange: setSelectedCryptoPaymentMethod, onBack: () => {
|
|
472
|
+
const orderDetailsView = ((0, jsx_runtime_1.jsxs)("div", { className: (0, utils_1.cn)("mx-auto flex w-full flex-col items-center gap-4 p-5", mode === "modal" && "bg-b3-react-background rounded-xl"), children: [oat && ((0, jsx_runtime_1.jsx)(OrderDetails_1.OrderDetails, { mode: mode, order: oat.data.order, depositTxs: oat.data.depositTxs, relayTxs: oat.data.relayTxs, executeTx: oat.data.executeTx, refundTxs: oat.data.refundTxs, cryptoPaymentMethod: activeTab === "fiat" ? CryptoPaymentMethod_1.CryptoPaymentMethodType.NONE : selectedCryptoPaymentMethod, selectedCryptoPaymentMethod: selectedCryptoPaymentMethod, onPaymentMethodChange: setSelectedCryptoPaymentMethod, onBack: () => {
|
|
471
473
|
setOrderId(undefined);
|
|
472
474
|
setActivePanel(PanelView.CONFIRM_ORDER);
|
|
473
475
|
// Remove orderId from URL when canceling
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.getAuthToken = void 0;
|
|
18
|
+
// Export types
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
20
|
+
// Export services
|
|
21
|
+
__exportStar(require("./services"), exports);
|
|
22
|
+
// Re-export auth token getter from shared for convenience
|
|
23
|
+
// Note: Auth token is managed by B3 Global Account authentication
|
|
24
|
+
var auth_token_1 = require("../shared/utils/auth-token");
|
|
25
|
+
Object.defineProperty(exports, "getAuthToken", { enumerable: true, get: function () { return auth_token_1.getAuthToken; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useNotifications";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useNotifications"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { UserData } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* React hook for managing B3 notifications
|
|
4
|
+
* Automatically uses the authenticated user's ID from JWT
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* import { useNotifications } from '../../../notifications/react';
|
|
9
|
+
*
|
|
10
|
+
* function NotificationSettings() {
|
|
11
|
+
* const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
|
|
12
|
+
*
|
|
13
|
+
* if (loading) return <div>Loading...</div>;
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <div>
|
|
17
|
+
* {!isEmailConnected && (
|
|
18
|
+
* <button onClick={() => connectEmail('user@example.com')}>
|
|
19
|
+
* Connect Email
|
|
20
|
+
* </button>
|
|
21
|
+
* )}
|
|
22
|
+
* <button onClick={connectTelegram}>Connect Telegram</button>
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function useNotifications(): {
|
|
29
|
+
user: UserData | null;
|
|
30
|
+
loading: boolean;
|
|
31
|
+
error: Error | null;
|
|
32
|
+
refresh: () => Promise<void>;
|
|
33
|
+
connectEmail: (email: string) => Promise<void>;
|
|
34
|
+
connectTelegram: () => Promise<void>;
|
|
35
|
+
updateChannel: (channelId: string, updates: {
|
|
36
|
+
enabled?: boolean;
|
|
37
|
+
}) => Promise<void>;
|
|
38
|
+
deleteChannel: (channelId: string) => Promise<void>;
|
|
39
|
+
isEmailConnected: boolean;
|
|
40
|
+
isTelegramConnected: boolean;
|
|
41
|
+
isDiscordConnected: boolean;
|
|
42
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useNotifications = useNotifications;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const api_1 = require("../../services/api");
|
|
6
|
+
/**
|
|
7
|
+
* React hook for managing B3 notifications
|
|
8
|
+
* Automatically uses the authenticated user's ID from JWT
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* import { useNotifications } from '../../../notifications/react';
|
|
13
|
+
*
|
|
14
|
+
* function NotificationSettings() {
|
|
15
|
+
* const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
|
|
16
|
+
*
|
|
17
|
+
* if (loading) return <div>Loading...</div>;
|
|
18
|
+
*
|
|
19
|
+
* return (
|
|
20
|
+
* <div>
|
|
21
|
+
* {!isEmailConnected && (
|
|
22
|
+
* <button onClick={() => connectEmail('user@example.com')}>
|
|
23
|
+
* Connect Email
|
|
24
|
+
* </button>
|
|
25
|
+
* )}
|
|
26
|
+
* <button onClick={connectTelegram}>Connect Telegram</button>
|
|
27
|
+
* </div>
|
|
28
|
+
* );
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function useNotifications() {
|
|
33
|
+
const [user, setUser] = (0, react_1.useState)(null);
|
|
34
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
35
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
36
|
+
// Refs to track polling timers for cleanup
|
|
37
|
+
const telegramPollIntervalRef = (0, react_1.useRef)(null);
|
|
38
|
+
const telegramPollTimeoutRef = (0, react_1.useRef)(null);
|
|
39
|
+
// Cleanup function for Telegram polling
|
|
40
|
+
const cleanupTelegramPolling = () => {
|
|
41
|
+
if (telegramPollIntervalRef.current) {
|
|
42
|
+
clearInterval(telegramPollIntervalRef.current);
|
|
43
|
+
telegramPollIntervalRef.current = null;
|
|
44
|
+
}
|
|
45
|
+
if (telegramPollTimeoutRef.current) {
|
|
46
|
+
clearTimeout(telegramPollTimeoutRef.current);
|
|
47
|
+
telegramPollTimeoutRef.current = null;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// Load user data on mount
|
|
51
|
+
(0, react_1.useEffect)(() => {
|
|
52
|
+
loadUser();
|
|
53
|
+
}, []);
|
|
54
|
+
// Cleanup polling on unmount
|
|
55
|
+
(0, react_1.useEffect)(() => {
|
|
56
|
+
return () => {
|
|
57
|
+
cleanupTelegramPolling();
|
|
58
|
+
};
|
|
59
|
+
}, []);
|
|
60
|
+
const loadUser = async () => {
|
|
61
|
+
try {
|
|
62
|
+
setLoading(true);
|
|
63
|
+
setError(null);
|
|
64
|
+
const userData = await api_1.notificationsAPI.getUser();
|
|
65
|
+
setUser(userData);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
setError(err instanceof Error ? err : new Error("Failed to load user"));
|
|
69
|
+
console.error("Failed to load user:", err);
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
setLoading(false);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const connectEmail = async (email) => {
|
|
76
|
+
try {
|
|
77
|
+
await api_1.notificationsAPI.connectEmail(email);
|
|
78
|
+
await loadUser(); // Refresh user data
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
console.error("Failed to connect email:", err);
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const connectTelegram = async () => {
|
|
86
|
+
try {
|
|
87
|
+
// Clear any existing polling before starting new one
|
|
88
|
+
cleanupTelegramPolling();
|
|
89
|
+
const { deepLink } = await api_1.notificationsAPI.getTelegramLink();
|
|
90
|
+
window.open(deepLink, "_blank");
|
|
91
|
+
// Poll for connection status
|
|
92
|
+
telegramPollIntervalRef.current = setInterval(async () => {
|
|
93
|
+
try {
|
|
94
|
+
const { connected } = await api_1.notificationsAPI.checkTelegramStatus();
|
|
95
|
+
if (connected) {
|
|
96
|
+
cleanupTelegramPolling();
|
|
97
|
+
await loadUser(); // Refresh user data
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
console.error("Failed to check Telegram status:", err);
|
|
102
|
+
}
|
|
103
|
+
}, 2000);
|
|
104
|
+
// Stop polling after 2 minutes
|
|
105
|
+
telegramPollTimeoutRef.current = setTimeout(() => {
|
|
106
|
+
cleanupTelegramPolling();
|
|
107
|
+
}, 120000);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
console.error("Failed to connect Telegram:", err);
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const updateChannel = async (channelId, updates) => {
|
|
115
|
+
try {
|
|
116
|
+
await api_1.notificationsAPI.updateChannel(channelId, updates);
|
|
117
|
+
await loadUser(); // Refresh user data
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
console.error("Failed to update channel:", err);
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const deleteChannel = async (channelId) => {
|
|
125
|
+
try {
|
|
126
|
+
await api_1.notificationsAPI.deleteChannel(channelId);
|
|
127
|
+
await loadUser(); // Refresh user data
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
console.error("Failed to delete channel:", err);
|
|
131
|
+
throw err;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
user,
|
|
136
|
+
loading,
|
|
137
|
+
error,
|
|
138
|
+
refresh: loadUser,
|
|
139
|
+
connectEmail,
|
|
140
|
+
connectTelegram,
|
|
141
|
+
updateChannel,
|
|
142
|
+
deleteChannel,
|
|
143
|
+
// Convenience helpers
|
|
144
|
+
isEmailConnected: user?.channels?.find(c => c.channel_type === "email")?.enabled === 1,
|
|
145
|
+
isTelegramConnected: user?.channels?.find(c => c.channel_type === "telegram")?.enabled === 1,
|
|
146
|
+
isDiscordConnected: user?.channels?.find(c => c.channel_type === "discord")?.enabled === 1,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./hooks";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./hooks"), exports);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { NotificationHistory, NotificationPreferences, SendNotificationRequest, TelegramLinkResponse, TelegramStatusResponse, UserData } from "../types";
|
|
2
|
+
export declare function setApiUrl(url: string): void;
|
|
3
|
+
export declare function getApiUrl(): string;
|
|
4
|
+
export declare const notificationsAPI: {
|
|
5
|
+
/**
|
|
6
|
+
* Register the current user (userId extracted from JWT)
|
|
7
|
+
*/
|
|
8
|
+
registerUser(): Promise<any>;
|
|
9
|
+
/**
|
|
10
|
+
* Get current user's profile and preferences
|
|
11
|
+
*/
|
|
12
|
+
getUser(): Promise<UserData>;
|
|
13
|
+
/**
|
|
14
|
+
* Get current user's notification history
|
|
15
|
+
*/
|
|
16
|
+
getHistory(appId?: string, limit?: number): Promise<NotificationHistory[]>;
|
|
17
|
+
/**
|
|
18
|
+
* Add a notification channel for current user
|
|
19
|
+
*/
|
|
20
|
+
addChannel(channelType: string, channelIdentifier: string, metadata?: Record<string, any>): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Connect email for current user
|
|
23
|
+
*/
|
|
24
|
+
connectEmail(email: string): Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
* Update a notification channel
|
|
27
|
+
*/
|
|
28
|
+
updateChannel(channelId: string, updates: {
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
channelIdentifier?: string;
|
|
31
|
+
metadata?: Record<string, any>;
|
|
32
|
+
}): Promise<any>;
|
|
33
|
+
/**
|
|
34
|
+
* Delete a notification channel
|
|
35
|
+
*/
|
|
36
|
+
deleteChannel(channelId: string): Promise<any>;
|
|
37
|
+
/**
|
|
38
|
+
* Get Telegram deep link for current user
|
|
39
|
+
*/
|
|
40
|
+
getTelegramLink(): Promise<TelegramLinkResponse>;
|
|
41
|
+
/**
|
|
42
|
+
* Check current user's Telegram connection status
|
|
43
|
+
*/
|
|
44
|
+
checkTelegramStatus(): Promise<TelegramStatusResponse>;
|
|
45
|
+
/**
|
|
46
|
+
* Save notification preferences for an app
|
|
47
|
+
* @param appId - The application ID
|
|
48
|
+
* @param settings - Notification preferences including channels, type, and enabled status (defaults to true)
|
|
49
|
+
*/
|
|
50
|
+
savePreferences(appId: string, settings: NotificationPreferences): Promise<any>;
|
|
51
|
+
/**
|
|
52
|
+
* Get notification settings for an app
|
|
53
|
+
*/
|
|
54
|
+
getAppSettings(appId: string): Promise<any>;
|
|
55
|
+
/**
|
|
56
|
+
* Get current user's in-app notifications
|
|
57
|
+
*/
|
|
58
|
+
getInAppNotifications(): Promise<any>;
|
|
59
|
+
/**
|
|
60
|
+
* Mark a notification as read
|
|
61
|
+
*/
|
|
62
|
+
markNotificationAsRead(notificationId: string): Promise<any>;
|
|
63
|
+
/**
|
|
64
|
+
* Send a notification (requires auth)
|
|
65
|
+
*/
|
|
66
|
+
sendNotification(data: SendNotificationRequest): Promise<any>;
|
|
67
|
+
};
|