@b3dotfun/sdk 0.0.46 → 0.0.47-alpha.1

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.
Files changed (89) hide show
  1. package/README.md +3 -222
  2. package/dist/cjs/anyspend/react/components/AnySpend.js +13 -14
  3. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +5 -3
  4. package/dist/cjs/anyspend/react/components/AnyspendDepositHype.js +14 -15
  5. package/dist/cjs/anyspend/react/components/common/OrderHistory.js +2 -2
  6. package/dist/cjs/anyspend/react/components/common/OrderHistoryItem.js +6 -3
  7. package/dist/cjs/anyspend/types/api.d.ts +176 -0
  8. package/dist/cjs/anyspend/utils/chain.js +1 -1
  9. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +245 -0
  10. package/dist/cjs/shared/utils/index.d.ts +0 -1
  11. package/dist/cjs/shared/utils/index.js +0 -1
  12. package/dist/esm/anyspend/react/components/AnySpend.js +14 -15
  13. package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -3
  14. package/dist/esm/anyspend/react/components/AnyspendDepositHype.js +15 -16
  15. package/dist/esm/anyspend/react/components/common/OrderHistory.js +2 -2
  16. package/dist/esm/anyspend/react/components/common/OrderHistoryItem.js +8 -5
  17. package/dist/esm/anyspend/types/api.d.ts +176 -0
  18. package/dist/esm/anyspend/utils/chain.js +1 -1
  19. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +1 -0
  20. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +243 -0
  21. package/dist/esm/shared/utils/index.d.ts +0 -1
  22. package/dist/esm/shared/utils/index.js +0 -1
  23. package/dist/styles/index.css +1 -1
  24. package/dist/types/anyspend/types/api.d.ts +176 -0
  25. package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +1 -0
  26. package/dist/types/shared/utils/index.d.ts +0 -1
  27. package/package.json +2 -21
  28. package/src/anyspend/react/components/AnySpend.tsx +21 -19
  29. package/src/anyspend/react/components/AnySpendCustom.tsx +5 -3
  30. package/src/anyspend/react/components/AnyspendDepositHype.tsx +21 -21
  31. package/src/anyspend/react/components/common/OrderHistory.tsx +11 -11
  32. package/src/anyspend/react/components/common/OrderHistoryItem.tsx +124 -129
  33. package/src/anyspend/types/api.ts +176 -0
  34. package/src/anyspend/utils/chain.ts +1 -4
  35. package/src/shared/utils/index.ts +0 -1
  36. package/dist/cjs/anyspend/react/components/common/ErrorSection.d.ts +0 -6
  37. package/dist/cjs/anyspend/react/components/common/ErrorSection.js +0 -12
  38. package/dist/cjs/notifications/index.d.ts +0 -3
  39. package/dist/cjs/notifications/index.js +0 -25
  40. package/dist/cjs/notifications/react/hooks/index.d.ts +0 -1
  41. package/dist/cjs/notifications/react/hooks/index.js +0 -17
  42. package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +0 -42
  43. package/dist/cjs/notifications/react/hooks/useNotifications.js +0 -148
  44. package/dist/cjs/notifications/react/index.d.ts +0 -1
  45. package/dist/cjs/notifications/react/index.js +0 -17
  46. package/dist/cjs/notifications/services/api.d.ts +0 -67
  47. package/dist/cjs/notifications/services/api.js +0 -184
  48. package/dist/cjs/notifications/services/index.d.ts +0 -1
  49. package/dist/cjs/notifications/services/index.js +0 -17
  50. package/dist/cjs/notifications/types/index.d.ts +0 -51
  51. package/dist/cjs/notifications/types/index.js +0 -2
  52. package/dist/cjs/shared/utils/auth-token.d.ts +0 -7
  53. package/dist/cjs/shared/utils/auth-token.js +0 -17
  54. package/dist/esm/anyspend/react/components/common/ErrorSection.d.ts +0 -6
  55. package/dist/esm/anyspend/react/components/common/ErrorSection.js +0 -9
  56. package/dist/esm/notifications/index.d.ts +0 -3
  57. package/dist/esm/notifications/index.js +0 -7
  58. package/dist/esm/notifications/react/hooks/index.d.ts +0 -1
  59. package/dist/esm/notifications/react/hooks/index.js +0 -1
  60. package/dist/esm/notifications/react/hooks/useNotifications.d.ts +0 -42
  61. package/dist/esm/notifications/react/hooks/useNotifications.js +0 -145
  62. package/dist/esm/notifications/react/index.d.ts +0 -1
  63. package/dist/esm/notifications/react/index.js +0 -1
  64. package/dist/esm/notifications/services/api.d.ts +0 -67
  65. package/dist/esm/notifications/services/api.js +0 -179
  66. package/dist/esm/notifications/services/index.d.ts +0 -1
  67. package/dist/esm/notifications/services/index.js +0 -1
  68. package/dist/esm/notifications/types/index.d.ts +0 -51
  69. package/dist/esm/shared/utils/auth-token.d.ts +0 -7
  70. package/dist/esm/shared/utils/auth-token.js +0 -11
  71. package/dist/types/anyspend/react/components/common/ErrorSection.d.ts +0 -6
  72. package/dist/types/notifications/index.d.ts +0 -3
  73. package/dist/types/notifications/react/hooks/index.d.ts +0 -1
  74. package/dist/types/notifications/react/hooks/useNotifications.d.ts +0 -42
  75. package/dist/types/notifications/react/index.d.ts +0 -1
  76. package/dist/types/notifications/services/api.d.ts +0 -67
  77. package/dist/types/notifications/services/index.d.ts +0 -1
  78. package/dist/types/notifications/types/index.d.ts +0 -51
  79. package/dist/types/shared/utils/auth-token.d.ts +0 -7
  80. package/src/anyspend/react/components/common/ErrorSection.tsx +0 -21
  81. package/src/notifications/index.ts +0 -9
  82. package/src/notifications/react/hooks/index.ts +0 -1
  83. package/src/notifications/react/hooks/useNotifications.ts +0 -153
  84. package/src/notifications/react/index.ts +0 -1
  85. package/src/notifications/services/api.ts +0 -217
  86. package/src/notifications/services/index.ts +0 -1
  87. package/src/notifications/types/index.ts +0 -58
  88. package/src/shared/utils/auth-token.ts +0 -13
  89. /package/dist/{esm/notifications/types/index.js → cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts} +0 -0
@@ -1,17 +0,0 @@
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);
@@ -1,67 +0,0 @@
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
- };
@@ -1,184 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.notificationsAPI = void 0;
4
- exports.setApiUrl = setApiUrl;
5
- exports.getApiUrl = getApiUrl;
6
- const auth_token_1 = require("../../shared/utils/auth-token");
7
- const DEFAULT_API_URL = "https://notifications.b3.fun";
8
- let apiUrl = DEFAULT_API_URL;
9
- function setApiUrl(url) {
10
- apiUrl = url;
11
- }
12
- function getApiUrl() {
13
- return apiUrl;
14
- }
15
- function getHeaders(includeAuth = false) {
16
- const headers = {
17
- "Content-Type": "application/json",
18
- };
19
- if (includeAuth) {
20
- const token = (0, auth_token_1.getAuthToken)();
21
- if (token) {
22
- headers["Authorization"] = `Bearer ${token}`;
23
- }
24
- }
25
- return headers;
26
- }
27
- exports.notificationsAPI = {
28
- // ===== USER MANAGEMENT =====
29
- /**
30
- * Register the current user (userId extracted from JWT)
31
- */
32
- async registerUser() {
33
- const res = await fetch(`${apiUrl}/users`, {
34
- method: "POST",
35
- headers: getHeaders(true),
36
- });
37
- if (!res.ok) {
38
- const errorBody = await res.text().catch(() => "Could not read error body");
39
- throw new Error(`API Error: ${res.status} ${res.statusText} - ${errorBody}`);
40
- }
41
- return res.json();
42
- },
43
- /**
44
- * Get current user's profile and preferences
45
- */
46
- async getUser() {
47
- const res = await fetch(`${apiUrl}/users/me`, {
48
- headers: getHeaders(true),
49
- });
50
- return res.json();
51
- },
52
- /**
53
- * Get current user's notification history
54
- */
55
- async getHistory(appId, limit = 100) {
56
- const params = new URLSearchParams();
57
- if (appId)
58
- params.append("appId", appId);
59
- params.append("limit", limit.toString());
60
- const res = await fetch(`${apiUrl}/users/me/history?${params}`, {
61
- headers: getHeaders(true),
62
- });
63
- return res.json();
64
- },
65
- // ===== CHANNELS =====
66
- /**
67
- * Add a notification channel for current user
68
- */
69
- async addChannel(channelType, channelIdentifier, metadata) {
70
- const res = await fetch(`${apiUrl}/users/me/channels`, {
71
- method: "POST",
72
- headers: getHeaders(true),
73
- body: JSON.stringify({
74
- channelType,
75
- channelIdentifier,
76
- enabled: true,
77
- metadata,
78
- }),
79
- });
80
- return res.json();
81
- },
82
- /**
83
- * Connect email for current user
84
- */
85
- async connectEmail(email) {
86
- return this.addChannel("email", email);
87
- },
88
- /**
89
- * Update a notification channel
90
- */
91
- async updateChannel(channelId, updates) {
92
- const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
93
- method: "PUT",
94
- headers: getHeaders(true),
95
- body: JSON.stringify(updates),
96
- });
97
- return res.json();
98
- },
99
- /**
100
- * Delete a notification channel
101
- */
102
- async deleteChannel(channelId) {
103
- const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
104
- method: "DELETE",
105
- headers: getHeaders(true),
106
- });
107
- return res.json();
108
- },
109
- // ===== TELEGRAM =====
110
- /**
111
- * Get Telegram deep link for current user
112
- */
113
- async getTelegramLink() {
114
- const res = await fetch(`${apiUrl}/telegram/request-link`, {
115
- method: "POST",
116
- headers: getHeaders(true),
117
- });
118
- return res.json();
119
- },
120
- /**
121
- * Check current user's Telegram connection status
122
- */
123
- async checkTelegramStatus() {
124
- const res = await fetch(`${apiUrl}/telegram/status/me`, {
125
- headers: getHeaders(true),
126
- });
127
- return res.json();
128
- },
129
- // ===== APP PREFERENCES =====
130
- /**
131
- * Save notification preferences for an app
132
- * @param appId - The application ID
133
- * @param settings - Notification preferences including channels, type, and enabled status (defaults to true)
134
- */
135
- async savePreferences(appId, settings) {
136
- const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
137
- method: "POST",
138
- headers: getHeaders(true),
139
- body: JSON.stringify({ enabled: true, ...settings }),
140
- });
141
- return res.json();
142
- },
143
- /**
144
- * Get notification settings for an app
145
- */
146
- async getAppSettings(appId) {
147
- const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
148
- headers: getHeaders(true),
149
- });
150
- return res.json();
151
- },
152
- // ===== IN-APP NOTIFICATIONS =====
153
- /**
154
- * Get current user's in-app notifications
155
- */
156
- async getInAppNotifications() {
157
- const res = await fetch(`${apiUrl}/users/me/notifications`, {
158
- headers: getHeaders(true),
159
- });
160
- return res.json();
161
- },
162
- /**
163
- * Mark a notification as read
164
- */
165
- async markNotificationAsRead(notificationId) {
166
- const res = await fetch(`${apiUrl}/users/me/notifications/${notificationId}/read`, {
167
- method: "PUT",
168
- headers: getHeaders(true),
169
- });
170
- return res.json();
171
- },
172
- // ===== SENDING NOTIFICATIONS =====
173
- /**
174
- * Send a notification (requires auth)
175
- */
176
- async sendNotification(data) {
177
- const res = await fetch(`${apiUrl}/send`, {
178
- method: "POST",
179
- headers: getHeaders(true),
180
- body: JSON.stringify(data),
181
- });
182
- return res.json();
183
- },
184
- };
@@ -1 +0,0 @@
1
- export * from "./api";
@@ -1,17 +0,0 @@
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("./api"), exports);
@@ -1,51 +0,0 @@
1
- export type ChannelType = "email" | "telegram" | "discord" | "sms" | "whatsapp" | "in_app";
2
- export interface NotificationChannel {
3
- id: number;
4
- channel_type: ChannelType;
5
- enabled: number;
6
- channel_identifier: string;
7
- }
8
- export interface UserData {
9
- user: {
10
- id: number;
11
- user_id: string;
12
- };
13
- channels: NotificationChannel[];
14
- appSettings: Array<{
15
- app_id: string;
16
- notification_type: string;
17
- enabled: number;
18
- channels: string;
19
- }>;
20
- }
21
- export interface NotificationHistory {
22
- id: string;
23
- app_id: string;
24
- notification_type: string;
25
- title: string;
26
- message: string;
27
- created_at: string;
28
- read: boolean;
29
- }
30
- export interface TelegramLinkResponse {
31
- deepLink: string;
32
- verificationCode: string;
33
- botUsername: string;
34
- }
35
- export interface TelegramStatusResponse {
36
- connected: boolean;
37
- chatId?: string;
38
- }
39
- export interface NotificationPreferences {
40
- notificationType: string;
41
- channels: string[];
42
- enabled?: boolean;
43
- }
44
- export interface SendNotificationRequest {
45
- userId: string;
46
- appId: string;
47
- notificationType: string;
48
- message: string;
49
- title?: string;
50
- data?: Record<string, any>;
51
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +0,0 @@
1
- /**
2
- * Get the authentication token from the B3 auth cookie
3
- * This token is managed by the B3 Global Account authentication system
4
- *
5
- * @returns The JWT token string or null if not found
6
- */
7
- export declare function getAuthToken(): string | null;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAuthToken = getAuthToken;
7
- const js_cookie_1 = __importDefault(require("js-cookie"));
8
- const B3_AUTH_COOKIE_NAME = "b3-auth";
9
- /**
10
- * Get the authentication token from the B3 auth cookie
11
- * This token is managed by the B3 Global Account authentication system
12
- *
13
- * @returns The JWT token string or null if not found
14
- */
15
- function getAuthToken() {
16
- return js_cookie_1.default.get(B3_AUTH_COOKIE_NAME) || null;
17
- }
@@ -1,6 +0,0 @@
1
- interface ErrorSectionProps {
2
- error?: Error | null;
3
- message?: string;
4
- }
5
- export declare function ErrorSection({ error, message }: ErrorSectionProps): import("react/jsx-runtime").JSX.Element | null;
6
- export {};
@@ -1,9 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { CircleAlert } from "lucide-react";
3
- export function ErrorSection({ error, message }) {
4
- if (!error && !message) {
5
- return null;
6
- }
7
- const errorMessage = message || error?.message || "An error occurred";
8
- return (_jsxs("div", { className: "error-section bg-as-on-surface-1 flex w-full max-w-[460px] items-center gap-2 rounded-2xl px-4 py-2", children: [_jsx(CircleAlert, { className: "bg-as-red h-4 min-h-4 w-4 min-w-4 rounded-full p-0 text-sm font-medium text-white" }), _jsx("div", { className: "text-as-red text-sm", children: errorMessage })] }));
9
- }
@@ -1,3 +0,0 @@
1
- export * from "./types";
2
- export * from "./services";
3
- export { getAuthToken } from "../shared/utils/auth-token";
@@ -1,7 +0,0 @@
1
- // Export types
2
- export * from "./types/index.js";
3
- // Export services
4
- export * from "./services/index.js";
5
- // Re-export auth token getter from shared for convenience
6
- // Note: Auth token is managed by B3 Global Account authentication
7
- export { getAuthToken } from "../shared/utils/auth-token.js";
@@ -1 +0,0 @@
1
- export * from "./useNotifications";
@@ -1 +0,0 @@
1
- export * from "./useNotifications.js";
@@ -1,42 +0,0 @@
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
- };
@@ -1,145 +0,0 @@
1
- import { useEffect, useRef, useState } from "react";
2
- import { notificationsAPI } from "../../services/api.js";
3
- /**
4
- * React hook for managing B3 notifications
5
- * Automatically uses the authenticated user's ID from JWT
6
- *
7
- * @example
8
- * ```tsx
9
- * import { useNotifications } from '../../../notifications/react/index.js';
10
- *
11
- * function NotificationSettings() {
12
- * const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
13
- *
14
- * if (loading) return <div>Loading...</div>;
15
- *
16
- * return (
17
- * <div>
18
- * {!isEmailConnected && (
19
- * <button onClick={() => connectEmail('user@example.com')}>
20
- * Connect Email
21
- * </button>
22
- * )}
23
- * <button onClick={connectTelegram}>Connect Telegram</button>
24
- * </div>
25
- * );
26
- * }
27
- * ```
28
- */
29
- export function useNotifications() {
30
- const [user, setUser] = useState(null);
31
- const [loading, setLoading] = useState(true);
32
- const [error, setError] = useState(null);
33
- // Refs to track polling timers for cleanup
34
- const telegramPollIntervalRef = useRef(null);
35
- const telegramPollTimeoutRef = useRef(null);
36
- // Cleanup function for Telegram polling
37
- const cleanupTelegramPolling = () => {
38
- if (telegramPollIntervalRef.current) {
39
- clearInterval(telegramPollIntervalRef.current);
40
- telegramPollIntervalRef.current = null;
41
- }
42
- if (telegramPollTimeoutRef.current) {
43
- clearTimeout(telegramPollTimeoutRef.current);
44
- telegramPollTimeoutRef.current = null;
45
- }
46
- };
47
- // Load user data on mount
48
- useEffect(() => {
49
- loadUser();
50
- }, []);
51
- // Cleanup polling on unmount
52
- useEffect(() => {
53
- return () => {
54
- cleanupTelegramPolling();
55
- };
56
- }, []);
57
- const loadUser = async () => {
58
- try {
59
- setLoading(true);
60
- setError(null);
61
- const userData = await notificationsAPI.getUser();
62
- setUser(userData);
63
- }
64
- catch (err) {
65
- setError(err instanceof Error ? err : new Error("Failed to load user"));
66
- console.error("Failed to load user:", err);
67
- }
68
- finally {
69
- setLoading(false);
70
- }
71
- };
72
- const connectEmail = async (email) => {
73
- try {
74
- await notificationsAPI.connectEmail(email);
75
- await loadUser(); // Refresh user data
76
- }
77
- catch (err) {
78
- console.error("Failed to connect email:", err);
79
- throw err;
80
- }
81
- };
82
- const connectTelegram = async () => {
83
- try {
84
- // Clear any existing polling before starting new one
85
- cleanupTelegramPolling();
86
- const { deepLink } = await notificationsAPI.getTelegramLink();
87
- window.open(deepLink, "_blank");
88
- // Poll for connection status
89
- telegramPollIntervalRef.current = setInterval(async () => {
90
- try {
91
- const { connected } = await notificationsAPI.checkTelegramStatus();
92
- if (connected) {
93
- cleanupTelegramPolling();
94
- await loadUser(); // Refresh user data
95
- }
96
- }
97
- catch (err) {
98
- console.error("Failed to check Telegram status:", err);
99
- }
100
- }, 2000);
101
- // Stop polling after 2 minutes
102
- telegramPollTimeoutRef.current = setTimeout(() => {
103
- cleanupTelegramPolling();
104
- }, 120000);
105
- }
106
- catch (err) {
107
- console.error("Failed to connect Telegram:", err);
108
- throw err;
109
- }
110
- };
111
- const updateChannel = async (channelId, updates) => {
112
- try {
113
- await notificationsAPI.updateChannel(channelId, updates);
114
- await loadUser(); // Refresh user data
115
- }
116
- catch (err) {
117
- console.error("Failed to update channel:", err);
118
- throw err;
119
- }
120
- };
121
- const deleteChannel = async (channelId) => {
122
- try {
123
- await notificationsAPI.deleteChannel(channelId);
124
- await loadUser(); // Refresh user data
125
- }
126
- catch (err) {
127
- console.error("Failed to delete channel:", err);
128
- throw err;
129
- }
130
- };
131
- return {
132
- user,
133
- loading,
134
- error,
135
- refresh: loadUser,
136
- connectEmail,
137
- connectTelegram,
138
- updateChannel,
139
- deleteChannel,
140
- // Convenience helpers
141
- isEmailConnected: user?.channels?.find(c => c.channel_type === "email")?.enabled === 1,
142
- isTelegramConnected: user?.channels?.find(c => c.channel_type === "telegram")?.enabled === 1,
143
- isDiscordConnected: user?.channels?.find(c => c.channel_type === "discord")?.enabled === 1,
144
- };
145
- }
@@ -1 +0,0 @@
1
- export * from "./hooks";
@@ -1 +0,0 @@
1
- export * from "./hooks/index.js";
@@ -1,67 +0,0 @@
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
- };