@b3dotfun/sdk 0.0.44 → 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.
- package/README.md +215 -3
- package/dist/cjs/anyspend/react/components/common/OrderDetails.js +1 -6
- package/dist/cjs/anyspend/react/components/common/OrderHistoryItem.js +1 -6
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.d.ts +1 -0
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +3 -3
- package/dist/cjs/anyspend/react/hooks/useAnyspendOrderAndTransactions.d.ts +2 -0
- package/dist/cjs/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +36 -0
- package/dist/cjs/anyspend/types/api.d.ts +13 -10
- package/dist/cjs/anyspend/utils/orderPayload.js +0 -4
- package/dist/cjs/notifications/index.d.ts +4 -0
- package/dist/cjs/notifications/index.js +26 -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 +65 -0
- package/dist/cjs/notifications/services/api.js +178 -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 +50 -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/common/OrderDetails.js +1 -6
- package/dist/esm/anyspend/react/components/common/OrderHistoryItem.js +1 -6
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.d.ts +1 -0
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +3 -3
- package/dist/esm/anyspend/react/hooks/useAnyspendOrderAndTransactions.d.ts +2 -0
- package/dist/esm/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +36 -0
- package/dist/esm/anyspend/types/api.d.ts +13 -10
- package/dist/esm/anyspend/utils/orderPayload.js +0 -4
- package/dist/esm/notifications/index.d.ts +4 -0
- package/dist/esm/notifications/index.js +8 -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 +65 -0
- package/dist/esm/notifications/services/api.js +173 -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 +50 -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/anyspend/react/hooks/useAnyspendFlow.d.ts +1 -0
- package/dist/types/anyspend/react/hooks/useAnyspendOrderAndTransactions.d.ts +2 -0
- package/dist/types/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +36 -0
- package/dist/types/anyspend/types/api.d.ts +13 -10
- package/dist/types/notifications/index.d.ts +4 -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 +65 -0
- package/dist/types/notifications/services/index.d.ts +1 -0
- package/dist/types/notifications/types/index.d.ts +50 -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/common/OrderDetails.tsx +1 -7
- package/src/anyspend/react/components/common/OrderHistoryItem.tsx +1 -7
- package/src/anyspend/react/hooks/useAnyspendFlow.ts +3 -3
- package/src/anyspend/types/api.ts +13 -10
- package/src/anyspend/utils/orderPayload.ts +0 -4
- package/src/notifications/index.ts +11 -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 +211 -0
- package/src/notifications/services/index.ts +1 -0
- package/src/notifications/types/index.ts +57 -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
|
@@ -18,6 +18,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
18
18
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
19
19
|
oneClickBuyUrl: string | null;
|
|
20
20
|
stripePaymentIntentId: string | null;
|
|
21
|
+
settlement: {
|
|
22
|
+
actualDstAmount: string | null;
|
|
23
|
+
} | null;
|
|
21
24
|
} & {
|
|
22
25
|
type: "swap";
|
|
23
26
|
payload: import("../..").components["schemas"]["SwapPayload"];
|
|
@@ -41,6 +44,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
41
44
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
42
45
|
oneClickBuyUrl: string | null;
|
|
43
46
|
stripePaymentIntentId: string | null;
|
|
47
|
+
settlement: {
|
|
48
|
+
actualDstAmount: string | null;
|
|
49
|
+
} | null;
|
|
44
50
|
} & {
|
|
45
51
|
type: "hype_duel";
|
|
46
52
|
payload: import("../..").components["schemas"]["HypeDuelPayload"];
|
|
@@ -64,6 +70,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
64
70
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
65
71
|
oneClickBuyUrl: string | null;
|
|
66
72
|
stripePaymentIntentId: string | null;
|
|
73
|
+
settlement: {
|
|
74
|
+
actualDstAmount: string | null;
|
|
75
|
+
} | null;
|
|
67
76
|
} & {
|
|
68
77
|
type: "custom";
|
|
69
78
|
payload: import("../..").components["schemas"]["CustomPayload"];
|
|
@@ -87,6 +96,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
87
96
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
88
97
|
oneClickBuyUrl: string | null;
|
|
89
98
|
stripePaymentIntentId: string | null;
|
|
99
|
+
settlement: {
|
|
100
|
+
actualDstAmount: string | null;
|
|
101
|
+
} | null;
|
|
90
102
|
} & {
|
|
91
103
|
type: "mint_nft";
|
|
92
104
|
payload: import("../..").components["schemas"]["MintNftPayload"];
|
|
@@ -110,6 +122,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
110
122
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
111
123
|
oneClickBuyUrl: string | null;
|
|
112
124
|
stripePaymentIntentId: string | null;
|
|
125
|
+
settlement: {
|
|
126
|
+
actualDstAmount: string | null;
|
|
127
|
+
} | null;
|
|
113
128
|
} & {
|
|
114
129
|
type: "join_tournament";
|
|
115
130
|
payload: import("../..").components["schemas"]["JoinTournamentPayload"];
|
|
@@ -133,6 +148,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
133
148
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
134
149
|
oneClickBuyUrl: string | null;
|
|
135
150
|
stripePaymentIntentId: string | null;
|
|
151
|
+
settlement: {
|
|
152
|
+
actualDstAmount: string | null;
|
|
153
|
+
} | null;
|
|
136
154
|
} & {
|
|
137
155
|
type: "fund_tournament";
|
|
138
156
|
payload: import("../..").components["schemas"]["FundTournamentPayload"];
|
|
@@ -159,6 +177,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
159
177
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
160
178
|
oneClickBuyUrl: string | null;
|
|
161
179
|
stripePaymentIntentId: string | null;
|
|
180
|
+
settlement: {
|
|
181
|
+
actualDstAmount: string | null;
|
|
182
|
+
} | null;
|
|
162
183
|
} & {
|
|
163
184
|
type: "swap";
|
|
164
185
|
payload: import("../..").components["schemas"]["SwapPayload"];
|
|
@@ -182,6 +203,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
182
203
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
183
204
|
oneClickBuyUrl: string | null;
|
|
184
205
|
stripePaymentIntentId: string | null;
|
|
206
|
+
settlement: {
|
|
207
|
+
actualDstAmount: string | null;
|
|
208
|
+
} | null;
|
|
185
209
|
} & {
|
|
186
210
|
type: "hype_duel";
|
|
187
211
|
payload: import("../..").components["schemas"]["HypeDuelPayload"];
|
|
@@ -205,6 +229,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
205
229
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
206
230
|
oneClickBuyUrl: string | null;
|
|
207
231
|
stripePaymentIntentId: string | null;
|
|
232
|
+
settlement: {
|
|
233
|
+
actualDstAmount: string | null;
|
|
234
|
+
} | null;
|
|
208
235
|
} & {
|
|
209
236
|
type: "custom";
|
|
210
237
|
payload: import("../..").components["schemas"]["CustomPayload"];
|
|
@@ -228,6 +255,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
228
255
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
229
256
|
oneClickBuyUrl: string | null;
|
|
230
257
|
stripePaymentIntentId: string | null;
|
|
258
|
+
settlement: {
|
|
259
|
+
actualDstAmount: string | null;
|
|
260
|
+
} | null;
|
|
231
261
|
} & {
|
|
232
262
|
type: "mint_nft";
|
|
233
263
|
payload: import("../..").components["schemas"]["MintNftPayload"];
|
|
@@ -251,6 +281,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
251
281
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
252
282
|
oneClickBuyUrl: string | null;
|
|
253
283
|
stripePaymentIntentId: string | null;
|
|
284
|
+
settlement: {
|
|
285
|
+
actualDstAmount: string | null;
|
|
286
|
+
} | null;
|
|
254
287
|
} & {
|
|
255
288
|
type: "join_tournament";
|
|
256
289
|
payload: import("../..").components["schemas"]["JoinTournamentPayload"];
|
|
@@ -274,6 +307,9 @@ export declare function useAnyspendOrderHistory(creatorAddress: string | undefin
|
|
|
274
307
|
onrampMetadata: import("../..").components["schemas"]["OnrampMetadata"] | null;
|
|
275
308
|
oneClickBuyUrl: string | null;
|
|
276
309
|
stripePaymentIntentId: string | null;
|
|
310
|
+
settlement: {
|
|
311
|
+
actualDstAmount: string | null;
|
|
312
|
+
} | null;
|
|
277
313
|
} & {
|
|
278
314
|
type: "fund_tournament";
|
|
279
315
|
payload: import("../..").components["schemas"]["FundTournamentPayload"];
|
|
@@ -434,6 +434,11 @@ export interface paths {
|
|
|
434
434
|
executeTx: components["schemas"]["ExecuteTx"] | null;
|
|
435
435
|
/** @description Refund transactions if order failed */
|
|
436
436
|
refundTxs: components["schemas"]["RefundTx"][];
|
|
437
|
+
/**
|
|
438
|
+
* @description Points awarded for this order (only present when order status is executed)
|
|
439
|
+
* @example 100
|
|
440
|
+
*/
|
|
441
|
+
points: number | null;
|
|
437
442
|
};
|
|
438
443
|
/** @example 200 */
|
|
439
444
|
statusCode: number;
|
|
@@ -1112,11 +1117,6 @@ export interface components {
|
|
|
1112
1117
|
* @example 990000
|
|
1113
1118
|
*/
|
|
1114
1119
|
expectedDstAmount: string;
|
|
1115
|
-
/**
|
|
1116
|
-
* @description Actual received amount (null for new orders)
|
|
1117
|
-
* @example 990000
|
|
1118
|
-
*/
|
|
1119
|
-
actualDstAmount: string | null;
|
|
1120
1120
|
};
|
|
1121
1121
|
/** @description HypeDuel-specific payload */
|
|
1122
1122
|
HypeDuelPayload: {
|
|
@@ -1125,11 +1125,6 @@ export interface components {
|
|
|
1125
1125
|
* @example 990000
|
|
1126
1126
|
*/
|
|
1127
1127
|
expectedDstAmount: string;
|
|
1128
|
-
/**
|
|
1129
|
-
* @description Actual received amount (null for new orders)
|
|
1130
|
-
* @example 990000
|
|
1131
|
-
*/
|
|
1132
|
-
actualDstAmount: string | null;
|
|
1133
1128
|
};
|
|
1134
1129
|
/** @description Custom execution payload */
|
|
1135
1130
|
CustomPayload: {
|
|
@@ -1315,6 +1310,14 @@ export interface components {
|
|
|
1315
1310
|
* @example pi_3Rko0sJnoDg53PsP0PDLsHkR
|
|
1316
1311
|
*/
|
|
1317
1312
|
stripePaymentIntentId: string | null;
|
|
1313
|
+
/** @description Settlement information for executed orders */
|
|
1314
|
+
settlement: {
|
|
1315
|
+
/**
|
|
1316
|
+
* @description Actual received amount after execution
|
|
1317
|
+
* @example 990000
|
|
1318
|
+
*/
|
|
1319
|
+
actualDstAmount: string | null;
|
|
1320
|
+
} | null;
|
|
1318
1321
|
};
|
|
1319
1322
|
SwapOrder: components["schemas"]["BaseOrder"] & {
|
|
1320
1323
|
/**
|
|
@@ -5,8 +5,6 @@ export const buildPayload = (orderType, params) => {
|
|
|
5
5
|
case "swap":
|
|
6
6
|
return {
|
|
7
7
|
expectedDstAmount,
|
|
8
|
-
actualDstAmount: null,
|
|
9
|
-
amountInAfterFee: null,
|
|
10
8
|
};
|
|
11
9
|
case "mint_nft":
|
|
12
10
|
if (nft?.type === "erc1155") {
|
|
@@ -43,8 +41,6 @@ export const buildPayload = (orderType, params) => {
|
|
|
43
41
|
case "hype_duel":
|
|
44
42
|
return {
|
|
45
43
|
expectedDstAmount,
|
|
46
|
-
actualDstAmount: null,
|
|
47
|
-
amountInAfterFee: null,
|
|
48
44
|
};
|
|
49
45
|
default:
|
|
50
46
|
throw new Error(`Invalid order type: ${orderType}`);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const VERSION = "1.0.0";
|
|
2
|
+
// Export types
|
|
3
|
+
export * from "./types/index.js";
|
|
4
|
+
// Export services
|
|
5
|
+
export * from "./services/index.js";
|
|
6
|
+
// Re-export auth token getter from shared for convenience
|
|
7
|
+
// Note: Auth token is managed by B3 Global Account authentication
|
|
8
|
+
export { getAuthToken } from "../shared/utils/auth-token.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useNotifications";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useNotifications.js";
|
|
@@ -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,145 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./hooks";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./hooks/index.js";
|
|
@@ -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,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";
|