@b3dotfun/sdk 0.0.44-alpha.1 → 0.0.45

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 (60) hide show
  1. package/README.md +215 -3
  2. package/dist/cjs/notifications/index.d.ts +4 -0
  3. package/dist/cjs/notifications/index.js +26 -0
  4. package/dist/cjs/notifications/react/hooks/index.d.ts +1 -0
  5. package/dist/cjs/notifications/react/hooks/index.js +17 -0
  6. package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +42 -0
  7. package/dist/cjs/notifications/react/hooks/useNotifications.js +148 -0
  8. package/dist/cjs/notifications/react/index.d.ts +1 -0
  9. package/dist/cjs/notifications/react/index.js +17 -0
  10. package/dist/cjs/notifications/services/api.d.ts +65 -0
  11. package/dist/cjs/notifications/services/api.js +178 -0
  12. package/dist/cjs/notifications/services/index.d.ts +1 -0
  13. package/dist/cjs/notifications/services/index.js +17 -0
  14. package/dist/cjs/notifications/types/index.d.ts +50 -0
  15. package/dist/cjs/notifications/types/index.js +2 -0
  16. package/dist/cjs/shared/utils/auth-token.d.ts +7 -0
  17. package/dist/cjs/shared/utils/auth-token.js +17 -0
  18. package/dist/cjs/shared/utils/index.d.ts +1 -0
  19. package/dist/cjs/shared/utils/index.js +1 -0
  20. package/dist/esm/notifications/index.d.ts +4 -0
  21. package/dist/esm/notifications/index.js +8 -0
  22. package/dist/esm/notifications/react/hooks/index.d.ts +1 -0
  23. package/dist/esm/notifications/react/hooks/index.js +1 -0
  24. package/dist/esm/notifications/react/hooks/useNotifications.d.ts +42 -0
  25. package/dist/esm/notifications/react/hooks/useNotifications.js +145 -0
  26. package/dist/esm/notifications/react/index.d.ts +1 -0
  27. package/dist/esm/notifications/react/index.js +1 -0
  28. package/dist/esm/notifications/services/api.d.ts +65 -0
  29. package/dist/esm/notifications/services/api.js +173 -0
  30. package/dist/esm/notifications/services/index.d.ts +1 -0
  31. package/dist/esm/notifications/services/index.js +1 -0
  32. package/dist/esm/notifications/types/index.d.ts +50 -0
  33. package/dist/esm/shared/utils/auth-token.d.ts +7 -0
  34. package/dist/esm/shared/utils/auth-token.js +11 -0
  35. package/dist/esm/shared/utils/index.d.ts +1 -0
  36. package/dist/esm/shared/utils/index.js +1 -0
  37. package/dist/types/notifications/index.d.ts +4 -0
  38. package/dist/types/notifications/react/hooks/index.d.ts +1 -0
  39. package/dist/types/notifications/react/hooks/useNotifications.d.ts +42 -0
  40. package/dist/types/notifications/react/index.d.ts +1 -0
  41. package/dist/types/notifications/services/api.d.ts +65 -0
  42. package/dist/types/notifications/services/index.d.ts +1 -0
  43. package/dist/types/notifications/types/index.d.ts +50 -0
  44. package/dist/types/shared/utils/auth-token.d.ts +7 -0
  45. package/dist/types/shared/utils/index.d.ts +1 -0
  46. package/package.json +21 -1
  47. package/src/notifications/index.ts +11 -0
  48. package/src/notifications/react/hooks/index.ts +1 -0
  49. package/src/notifications/react/hooks/useNotifications.ts +153 -0
  50. package/src/notifications/react/index.ts +1 -0
  51. package/src/notifications/services/api.ts +211 -0
  52. package/src/notifications/services/index.ts +1 -0
  53. package/src/notifications/types/index.ts +57 -0
  54. package/src/shared/utils/auth-token.ts +13 -0
  55. package/src/shared/utils/index.ts +1 -0
  56. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -245
  57. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  58. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -243
  59. package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  60. /package/dist/{cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts → esm/notifications/types/index.js} +0 -0
@@ -0,0 +1,173 @@
1
+ import { getAuthToken } from "../../shared/utils/auth-token.js";
2
+ const DEFAULT_API_URL = "https://notifications.b3.fun";
3
+ let apiUrl = DEFAULT_API_URL;
4
+ export function setApiUrl(url) {
5
+ apiUrl = url;
6
+ }
7
+ export function getApiUrl() {
8
+ return apiUrl;
9
+ }
10
+ function getHeaders(includeAuth = false) {
11
+ const headers = {
12
+ "Content-Type": "application/json",
13
+ };
14
+ if (includeAuth) {
15
+ const token = getAuthToken();
16
+ if (token) {
17
+ headers["Authorization"] = `Bearer ${token}`;
18
+ }
19
+ }
20
+ return headers;
21
+ }
22
+ export const notificationsAPI = {
23
+ // ===== USER MANAGEMENT =====
24
+ /**
25
+ * Register the current user (userId extracted from JWT)
26
+ */
27
+ async registerUser() {
28
+ const res = await fetch(`${apiUrl}/users`, {
29
+ method: "POST",
30
+ headers: getHeaders(true),
31
+ });
32
+ return res.json();
33
+ },
34
+ /**
35
+ * Get current user's profile and preferences
36
+ */
37
+ async getUser() {
38
+ const res = await fetch(`${apiUrl}/users/me`, {
39
+ headers: getHeaders(true),
40
+ });
41
+ return res.json();
42
+ },
43
+ /**
44
+ * Get current user's notification history
45
+ */
46
+ async getHistory(appId, limit = 100) {
47
+ const params = new URLSearchParams();
48
+ if (appId)
49
+ params.append("appId", appId);
50
+ params.append("limit", limit.toString());
51
+ const res = await fetch(`${apiUrl}/users/me/history?${params}`, {
52
+ headers: getHeaders(true),
53
+ });
54
+ return res.json();
55
+ },
56
+ // ===== CHANNELS =====
57
+ /**
58
+ * Add a notification channel for current user
59
+ */
60
+ async addChannel(channelType, channelIdentifier, metadata) {
61
+ const res = await fetch(`${apiUrl}/users/me/channels`, {
62
+ method: "POST",
63
+ headers: getHeaders(true),
64
+ body: JSON.stringify({
65
+ channelType,
66
+ channelIdentifier,
67
+ enabled: true,
68
+ metadata,
69
+ }),
70
+ });
71
+ return res.json();
72
+ },
73
+ /**
74
+ * Connect email for current user
75
+ */
76
+ async connectEmail(email) {
77
+ return this.addChannel("email", email);
78
+ },
79
+ /**
80
+ * Update a notification channel
81
+ */
82
+ async updateChannel(channelId, updates) {
83
+ const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
84
+ method: "PUT",
85
+ headers: getHeaders(true),
86
+ body: JSON.stringify(updates),
87
+ });
88
+ return res.json();
89
+ },
90
+ /**
91
+ * Delete a notification channel
92
+ */
93
+ async deleteChannel(channelId) {
94
+ const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
95
+ method: "DELETE",
96
+ headers: getHeaders(true),
97
+ });
98
+ return res.json();
99
+ },
100
+ // ===== TELEGRAM =====
101
+ /**
102
+ * Get Telegram deep link for current user
103
+ */
104
+ async getTelegramLink() {
105
+ const res = await fetch(`${apiUrl}/telegram/request-link`, {
106
+ method: "POST",
107
+ headers: getHeaders(true),
108
+ });
109
+ return res.json();
110
+ },
111
+ /**
112
+ * Check current user's Telegram connection status
113
+ */
114
+ async checkTelegramStatus() {
115
+ const res = await fetch(`${apiUrl}/telegram/status/me`, {
116
+ headers: getHeaders(true),
117
+ });
118
+ return res.json();
119
+ },
120
+ // ===== APP PREFERENCES =====
121
+ /**
122
+ * Save notification preferences for an app
123
+ */
124
+ async savePreferences(appId, settings) {
125
+ const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
126
+ method: "POST",
127
+ headers: getHeaders(true),
128
+ body: JSON.stringify({ ...settings, enabled: true }),
129
+ });
130
+ return res.json();
131
+ },
132
+ /**
133
+ * Get notification settings for an app
134
+ */
135
+ async getAppSettings(appId) {
136
+ const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
137
+ headers: getHeaders(true),
138
+ });
139
+ return res.json();
140
+ },
141
+ // ===== IN-APP NOTIFICATIONS =====
142
+ /**
143
+ * Get current user's in-app notifications
144
+ */
145
+ async getInAppNotifications() {
146
+ const res = await fetch(`${apiUrl}/users/me/notifications`, {
147
+ headers: getHeaders(true),
148
+ });
149
+ return res.json();
150
+ },
151
+ /**
152
+ * Mark a notification as read
153
+ */
154
+ async markNotificationAsRead(notificationId) {
155
+ const res = await fetch(`${apiUrl}/users/me/notifications/${notificationId}/read`, {
156
+ method: "PUT",
157
+ headers: getHeaders(true),
158
+ });
159
+ return res.json();
160
+ },
161
+ // ===== SENDING NOTIFICATIONS =====
162
+ /**
163
+ * Send a notification (requires auth)
164
+ */
165
+ async sendNotification(data) {
166
+ const res = await fetch(`${apiUrl}/send`, {
167
+ method: "POST",
168
+ headers: getHeaders(true),
169
+ body: JSON.stringify(data),
170
+ });
171
+ return res.json();
172
+ },
173
+ };
@@ -0,0 +1 @@
1
+ export * from "./api";
@@ -0,0 +1 @@
1
+ export * from "./api.js";
@@ -0,0 +1,50 @@
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
+ }
43
+ export interface SendNotificationRequest {
44
+ userId: string;
45
+ appId: string;
46
+ notificationType: string;
47
+ message: string;
48
+ title?: string;
49
+ data?: Record<string, any>;
50
+ }
@@ -0,0 +1,7 @@
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;
@@ -0,0 +1,11 @@
1
+ import Cookies from "js-cookie";
2
+ const B3_AUTH_COOKIE_NAME = "b3-auth";
3
+ /**
4
+ * Get the authentication token from the B3 auth cookie
5
+ * This token is managed by the B3 Global Account authentication system
6
+ *
7
+ * @returns The JWT token string or null if not found
8
+ */
9
+ export function getAuthToken() {
10
+ return Cookies.get(B3_AUTH_COOKIE_NAME) || null;
11
+ }
@@ -1,3 +1,4 @@
1
+ export * from "./auth-token";
1
2
  export * from "./cn";
2
3
  export * from "./formatNumber";
3
4
  export * from "./formatUsername";
@@ -1,4 +1,5 @@
1
1
  // Export utility functions
2
+ export * from "./auth-token.js";
2
3
  export * from "./cn.js";
3
4
  export * from "./formatNumber.js";
4
5
  export * from "./formatUsername.js";
@@ -0,0 +1,4 @@
1
+ export declare const VERSION = "1.0.0";
2
+ export * from "./types";
3
+ export * from "./services";
4
+ export { getAuthToken } from "../shared/utils/auth-token";
@@ -0,0 +1 @@
1
+ export * from "./useNotifications";
@@ -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 '@b3dotfun/sdk/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 @@
1
+ export * from "./hooks";
@@ -0,0 +1,65 @@
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
+ */
48
+ savePreferences(appId: string, settings: NotificationPreferences): Promise<any>;
49
+ /**
50
+ * Get notification settings for an app
51
+ */
52
+ getAppSettings(appId: string): Promise<any>;
53
+ /**
54
+ * Get current user's in-app notifications
55
+ */
56
+ getInAppNotifications(): Promise<any>;
57
+ /**
58
+ * Mark a notification as read
59
+ */
60
+ markNotificationAsRead(notificationId: string): Promise<any>;
61
+ /**
62
+ * Send a notification (requires auth)
63
+ */
64
+ sendNotification(data: SendNotificationRequest): Promise<any>;
65
+ };
@@ -0,0 +1 @@
1
+ export * from "./api";
@@ -0,0 +1,50 @@
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
+ }
43
+ export interface SendNotificationRequest {
44
+ userId: string;
45
+ appId: string;
46
+ notificationType: string;
47
+ message: string;
48
+ title?: string;
49
+ data?: Record<string, any>;
50
+ }
@@ -0,0 +1,7 @@
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,3 +1,4 @@
1
+ export * from "./auth-token";
1
2
  export * from "./cn";
2
3
  export * from "./formatNumber";
3
4
  export * from "./formatUsername";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b3dotfun/sdk",
3
- "version": "0.0.44-alpha.1",
3
+ "version": "0.0.45",
4
4
  "source": "src/index.ts",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "react-native": "./dist/cjs/index.native.js",
@@ -170,6 +170,26 @@
170
170
  "import": "./dist/esm/global-account/server.js",
171
171
  "require": "./dist/cjs/global-account/server.js"
172
172
  },
173
+ "./notifications": {
174
+ "types": "./dist/types/notifications/index.d.ts",
175
+ "import": "./dist/esm/notifications/index.js",
176
+ "require": "./dist/cjs/notifications/index.js"
177
+ },
178
+ "./notifications/react": {
179
+ "types": "./dist/types/notifications/react/index.d.ts",
180
+ "import": "./dist/esm/notifications/react/index.js",
181
+ "require": "./dist/cjs/notifications/react/index.js"
182
+ },
183
+ "./notifications/services": {
184
+ "types": "./dist/types/notifications/services/index.d.ts",
185
+ "import": "./dist/esm/notifications/services/index.js",
186
+ "require": "./dist/cjs/notifications/services/index.js"
187
+ },
188
+ "./notifications/types": {
189
+ "types": "./dist/types/notifications/types/index.d.ts",
190
+ "import": "./dist/esm/notifications/types/index.js",
191
+ "require": "./dist/cjs/notifications/types/index.js"
192
+ },
173
193
  "./shared/utils": {
174
194
  "types": "./dist/types/shared/utils/index.d.ts",
175
195
  "import": "./dist/esm/shared/utils/index.js",
@@ -0,0 +1,11 @@
1
+ export const VERSION = "1.0.0";
2
+
3
+ // Export types
4
+ export * from "./types";
5
+
6
+ // Export services
7
+ export * from "./services";
8
+
9
+ // Re-export auth token getter from shared for convenience
10
+ // Note: Auth token is managed by B3 Global Account authentication
11
+ export { getAuthToken } from "../shared/utils/auth-token";
@@ -0,0 +1 @@
1
+ export * from "./useNotifications";
@@ -0,0 +1,153 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ import { notificationsAPI } from "../../services/api";
3
+ import type { UserData } from "../../types";
4
+
5
+ /**
6
+ * React hook for managing B3 notifications
7
+ * Automatically uses the authenticated user's ID from JWT
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * import { useNotifications } from '@b3dotfun/sdk/notifications/react';
12
+ *
13
+ * function NotificationSettings() {
14
+ * const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
15
+ *
16
+ * if (loading) return <div>Loading...</div>;
17
+ *
18
+ * return (
19
+ * <div>
20
+ * {!isEmailConnected && (
21
+ * <button onClick={() => connectEmail('user@example.com')}>
22
+ * Connect Email
23
+ * </button>
24
+ * )}
25
+ * <button onClick={connectTelegram}>Connect Telegram</button>
26
+ * </div>
27
+ * );
28
+ * }
29
+ * ```
30
+ */
31
+ export function useNotifications() {
32
+ const [user, setUser] = useState<UserData | null>(null);
33
+ const [loading, setLoading] = useState(true);
34
+ const [error, setError] = useState<Error | null>(null);
35
+
36
+ // Refs to track polling timers for cleanup
37
+ const telegramPollIntervalRef = useRef<NodeJS.Timeout | null>(null);
38
+ const telegramPollTimeoutRef = useRef<NodeJS.Timeout | null>(null);
39
+
40
+ // Cleanup function for Telegram polling
41
+ const cleanupTelegramPolling = () => {
42
+ if (telegramPollIntervalRef.current) {
43
+ clearInterval(telegramPollIntervalRef.current);
44
+ telegramPollIntervalRef.current = null;
45
+ }
46
+ if (telegramPollTimeoutRef.current) {
47
+ clearTimeout(telegramPollTimeoutRef.current);
48
+ telegramPollTimeoutRef.current = null;
49
+ }
50
+ };
51
+
52
+ // Load user data on mount
53
+ useEffect(() => {
54
+ loadUser();
55
+ }, []);
56
+
57
+ // Cleanup polling on unmount
58
+ useEffect(() => {
59
+ return () => {
60
+ cleanupTelegramPolling();
61
+ };
62
+ }, []);
63
+
64
+ const loadUser = async () => {
65
+ try {
66
+ setLoading(true);
67
+ setError(null);
68
+ const userData = await notificationsAPI.getUser();
69
+ setUser(userData);
70
+ } catch (err) {
71
+ setError(err instanceof Error ? err : new Error("Failed to load user"));
72
+ console.error("Failed to load user:", err);
73
+ } finally {
74
+ setLoading(false);
75
+ }
76
+ };
77
+
78
+ const connectEmail = async (email: string) => {
79
+ try {
80
+ await notificationsAPI.connectEmail(email);
81
+ await loadUser(); // Refresh user data
82
+ } catch (err) {
83
+ console.error("Failed to connect email:", err);
84
+ throw err;
85
+ }
86
+ };
87
+
88
+ const connectTelegram = async () => {
89
+ try {
90
+ // Clear any existing polling before starting new one
91
+ cleanupTelegramPolling();
92
+
93
+ const { deepLink } = await notificationsAPI.getTelegramLink();
94
+ window.open(deepLink, "_blank");
95
+
96
+ // Poll for connection status
97
+ telegramPollIntervalRef.current = setInterval(async () => {
98
+ try {
99
+ const { connected } = await notificationsAPI.checkTelegramStatus();
100
+ if (connected) {
101
+ cleanupTelegramPolling();
102
+ await loadUser(); // Refresh user data
103
+ }
104
+ } catch (err) {
105
+ console.error("Failed to check Telegram status:", err);
106
+ }
107
+ }, 2000);
108
+
109
+ // Stop polling after 2 minutes
110
+ telegramPollTimeoutRef.current = setTimeout(() => {
111
+ cleanupTelegramPolling();
112
+ }, 120000);
113
+ } catch (err) {
114
+ console.error("Failed to connect Telegram:", err);
115
+ throw err;
116
+ }
117
+ };
118
+
119
+ const updateChannel = async (channelId: string, updates: { enabled?: boolean }) => {
120
+ try {
121
+ await notificationsAPI.updateChannel(channelId, updates);
122
+ await loadUser(); // Refresh user data
123
+ } catch (err) {
124
+ console.error("Failed to update channel:", err);
125
+ throw err;
126
+ }
127
+ };
128
+
129
+ const deleteChannel = async (channelId: string) => {
130
+ try {
131
+ await notificationsAPI.deleteChannel(channelId);
132
+ await loadUser(); // Refresh user data
133
+ } catch (err) {
134
+ console.error("Failed to delete channel:", err);
135
+ throw err;
136
+ }
137
+ };
138
+
139
+ return {
140
+ user,
141
+ loading,
142
+ error,
143
+ refresh: loadUser,
144
+ connectEmail,
145
+ connectTelegram,
146
+ updateChannel,
147
+ deleteChannel,
148
+ // Convenience helpers
149
+ isEmailConnected: user?.channels?.find(c => c.channel_type === "email")?.enabled === 1,
150
+ isTelegramConnected: user?.channels?.find(c => c.channel_type === "telegram")?.enabled === 1,
151
+ isDiscordConnected: user?.channels?.find(c => c.channel_type === "discord")?.enabled === 1,
152
+ };
153
+ }
@@ -0,0 +1 @@
1
+ export * from "./hooks";