@b3dotfun/sdk 0.0.47 → 0.0.48-alpha.0

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 (151) hide show
  1. package/README.md +225 -6
  2. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +5 -3
  3. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  4. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +15 -64
  5. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  6. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.js +26 -21
  7. package/dist/cjs/global-account/react/components/B3Provider/types.d.ts +2 -0
  8. package/dist/cjs/global-account/react/components/B3Provider/types.js +2 -0
  9. package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  10. package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
  11. package/dist/cjs/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
  12. package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +2 -2
  13. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3.js +1 -1
  14. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  15. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +6 -22
  16. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  17. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +3 -2
  18. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  19. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +10 -45
  20. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  21. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +2 -1
  22. package/dist/cjs/global-account/react/components/custom/ManageAccountButton.js +2 -1
  23. package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +51 -2
  24. package/dist/cjs/global-account/react/hooks/useAuthentication.js +141 -81
  25. package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  26. package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.js +2 -1
  27. package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +58 -0
  28. package/dist/cjs/global-account/react/hooks/useUserQuery.js +86 -0
  29. package/dist/cjs/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  30. package/dist/cjs/global-account/react/hooks/useWagmiConfig.js +42 -0
  31. package/dist/cjs/global-account/react/stores/useModalStore.d.ts +0 -2
  32. package/dist/cjs/notifications/index.d.ts +3 -0
  33. package/dist/cjs/notifications/index.js +25 -0
  34. package/dist/cjs/notifications/react/hooks/index.d.ts +1 -0
  35. package/dist/cjs/notifications/react/hooks/index.js +17 -0
  36. package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +42 -0
  37. package/dist/cjs/notifications/react/hooks/useNotifications.js +148 -0
  38. package/dist/cjs/notifications/react/index.d.ts +1 -0
  39. package/dist/cjs/notifications/react/index.js +17 -0
  40. package/dist/cjs/notifications/services/api.d.ts +67 -0
  41. package/dist/cjs/notifications/services/api.js +184 -0
  42. package/dist/cjs/notifications/services/index.d.ts +1 -0
  43. package/dist/cjs/notifications/services/index.js +17 -0
  44. package/dist/cjs/notifications/types/index.d.ts +51 -0
  45. package/dist/cjs/notifications/types/index.js +2 -0
  46. package/dist/cjs/shared/utils/auth-token.d.ts +7 -0
  47. package/dist/cjs/shared/utils/auth-token.js +17 -0
  48. package/dist/cjs/shared/utils/index.d.ts +1 -0
  49. package/dist/cjs/shared/utils/index.js +1 -0
  50. package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -3
  51. package/dist/esm/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  52. package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +19 -68
  53. package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  54. package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.js +24 -19
  55. package/dist/esm/global-account/react/components/B3Provider/types.d.ts +2 -0
  56. package/dist/esm/global-account/react/components/B3Provider/types.js +2 -0
  57. package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  58. package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
  59. package/dist/esm/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
  60. package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +2 -2
  61. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3.js +2 -2
  62. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  63. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +7 -23
  64. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  65. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +4 -3
  66. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  67. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +11 -46
  68. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  69. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +3 -2
  70. package/dist/esm/global-account/react/components/custom/ManageAccountButton.js +3 -2
  71. package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +51 -2
  72. package/dist/esm/global-account/react/hooks/useAuthentication.js +144 -84
  73. package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  74. package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.js +3 -2
  75. package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +58 -0
  76. package/dist/esm/global-account/react/hooks/useUserQuery.js +83 -0
  77. package/dist/esm/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  78. package/dist/esm/global-account/react/hooks/useWagmiConfig.js +39 -0
  79. package/dist/esm/global-account/react/stores/useModalStore.d.ts +0 -2
  80. package/dist/esm/notifications/index.d.ts +3 -0
  81. package/dist/esm/notifications/index.js +7 -0
  82. package/dist/esm/notifications/react/hooks/index.d.ts +1 -0
  83. package/dist/esm/notifications/react/hooks/index.js +1 -0
  84. package/dist/esm/notifications/react/hooks/useNotifications.d.ts +42 -0
  85. package/dist/esm/notifications/react/hooks/useNotifications.js +145 -0
  86. package/dist/esm/notifications/react/index.d.ts +1 -0
  87. package/dist/esm/notifications/react/index.js +1 -0
  88. package/dist/esm/notifications/services/api.d.ts +67 -0
  89. package/dist/esm/notifications/services/api.js +179 -0
  90. package/dist/esm/notifications/services/index.d.ts +1 -0
  91. package/dist/esm/notifications/services/index.js +1 -0
  92. package/dist/esm/notifications/types/index.d.ts +51 -0
  93. package/dist/esm/shared/utils/auth-token.d.ts +7 -0
  94. package/dist/esm/shared/utils/auth-token.js +11 -0
  95. package/dist/esm/shared/utils/index.d.ts +1 -0
  96. package/dist/esm/shared/utils/index.js +1 -0
  97. package/dist/types/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  98. package/dist/types/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  99. package/dist/types/global-account/react/components/B3Provider/types.d.ts +2 -0
  100. package/dist/types/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  101. package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  102. package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  103. package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  104. package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  105. package/dist/types/global-account/react/hooks/useAuthentication.d.ts +51 -2
  106. package/dist/types/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  107. package/dist/types/global-account/react/hooks/useUserQuery.d.ts +58 -0
  108. package/dist/types/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  109. package/dist/types/global-account/react/stores/useModalStore.d.ts +0 -2
  110. package/dist/types/notifications/index.d.ts +3 -0
  111. package/dist/types/notifications/react/hooks/index.d.ts +1 -0
  112. package/dist/types/notifications/react/hooks/useNotifications.d.ts +42 -0
  113. package/dist/types/notifications/react/index.d.ts +1 -0
  114. package/dist/types/notifications/services/api.d.ts +67 -0
  115. package/dist/types/notifications/services/index.d.ts +1 -0
  116. package/dist/types/notifications/types/index.d.ts +51 -0
  117. package/dist/types/shared/utils/auth-token.d.ts +7 -0
  118. package/dist/types/shared/utils/index.d.ts +1 -0
  119. package/package.json +23 -3
  120. package/src/anyspend/react/components/AnySpendCustom.tsx +5 -3
  121. package/src/global-account/react/components/B3Provider/B3Provider.native.tsx +51 -35
  122. package/src/global-account/react/components/B3Provider/B3Provider.tsx +28 -72
  123. package/src/global-account/react/components/B3Provider/types.ts +4 -0
  124. package/src/global-account/react/components/ManageAccount/BalanceContent.tsx +2 -3
  125. package/src/global-account/react/components/ManageAccount/ManageAccount.tsx +1 -1
  126. package/src/global-account/react/components/SignInWithB3/SignIn.tsx +2 -2
  127. package/src/global-account/react/components/SignInWithB3/SignInWithB3.tsx +2 -2
  128. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +4 -23
  129. package/src/global-account/react/components/SignInWithB3/SignInWithB3Privy.tsx +4 -3
  130. package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +8 -46
  131. package/src/global-account/react/components/SignInWithB3/steps/LoginStepCustom.tsx +2 -2
  132. package/src/global-account/react/components/custom/ManageAccountButton.tsx +3 -2
  133. package/src/global-account/react/hooks/useAuthentication.ts +170 -89
  134. package/src/global-account/react/hooks/useHandleConnectWithPrivy.tsx +3 -2
  135. package/src/global-account/react/hooks/useUserQuery.ts +95 -0
  136. package/src/global-account/react/hooks/useWagmiConfig.tsx +44 -0
  137. package/src/global-account/react/stores/useModalStore.ts +0 -2
  138. package/src/notifications/index.ts +9 -0
  139. package/src/notifications/react/hooks/index.ts +1 -0
  140. package/src/notifications/react/hooks/useNotifications.ts +153 -0
  141. package/src/notifications/react/index.ts +1 -0
  142. package/src/notifications/services/api.ts +217 -0
  143. package/src/notifications/services/index.ts +1 -0
  144. package/src/notifications/types/index.ts +58 -0
  145. package/src/shared/utils/auth-token.ts +13 -0
  146. package/src/shared/utils/index.ts +1 -0
  147. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -245
  148. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  149. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -243
  150. package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  151. /package/dist/{cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts → esm/notifications/types/index.js} +0 -0
@@ -0,0 +1,95 @@
1
+ import { Users } from "@b3dotfun/b3-api";
2
+ import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
3
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
4
+
5
+ const debug = debugB3React("useUserQuery");
6
+
7
+ const USER_QUERY_KEY = ["b3-user"];
8
+
9
+ /**
10
+ * Retrieves the user from localStorage
11
+ */
12
+ function getUserFromStorage(): Users | null {
13
+ if (typeof window === "undefined") {
14
+ return null;
15
+ }
16
+
17
+ try {
18
+ const storedUser = localStorage.getItem("b3-user");
19
+ return storedUser ? JSON.parse(storedUser) : null;
20
+ } catch (error) {
21
+ console.warn("Failed to restore user from localStorage:", error);
22
+ return null;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Saves user to localStorage
28
+ */
29
+ function saveUserToStorage(user: Users | null) {
30
+ if (typeof window === "undefined") {
31
+ return;
32
+ }
33
+
34
+ if (user) {
35
+ localStorage.setItem("b3-user", JSON.stringify(user));
36
+ } else {
37
+ localStorage.removeItem("b3-user");
38
+ }
39
+ }
40
+
41
+ /**
42
+ * NOTE: THIS IS ONLY MEANT FOR INTERNAL USE, from useOnConnect
43
+ *
44
+ * Custom hook to manage user state with react-query
45
+ * This allows for invalidation and refetching of user data
46
+ */
47
+ export function useUserQuery() {
48
+ const queryClient = useQueryClient();
49
+
50
+ // Query to get user data (primarily from cache/localStorage)
51
+ const { data: user } = useQuery<Users | null>({
52
+ queryKey: USER_QUERY_KEY,
53
+ queryFn: getUserFromStorage,
54
+ staleTime: Infinity, // User data doesn't go stale automatically
55
+ gcTime: Infinity, // Keep in cache indefinitely
56
+ initialData: getUserFromStorage,
57
+ });
58
+
59
+ // Mutation to update user
60
+ const setUserMutation = useMutation({
61
+ mutationFn: async (newUser: Users | undefined) => {
62
+ const userToSave = newUser ?? null;
63
+ saveUserToStorage(userToSave);
64
+ return userToSave;
65
+ },
66
+ onSuccess: data => {
67
+ queryClient.setQueryData(USER_QUERY_KEY, data);
68
+ debug("User updated", data);
69
+ },
70
+ });
71
+
72
+ // Helper function to set user (maintains backward compatibility)
73
+ const setUser = (newUser?: Users) => {
74
+ setUserMutation.mutate(newUser);
75
+ };
76
+
77
+ // Helper function to invalidate and refetch user
78
+ const refetchUser = async () => {
79
+ await queryClient.invalidateQueries({ queryKey: USER_QUERY_KEY });
80
+ return queryClient.refetchQueries({ queryKey: USER_QUERY_KEY });
81
+ };
82
+
83
+ // Helper function to clear user
84
+ const clearUser = () => {
85
+ setUser(undefined);
86
+ };
87
+
88
+ return {
89
+ user: user ?? undefined,
90
+ setUser,
91
+ refetchUser,
92
+ clearUser,
93
+ queryKey: USER_QUERY_KEY,
94
+ };
95
+ }
@@ -0,0 +1,44 @@
1
+ import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
2
+ import { supportedChains } from "@b3dotfun/sdk/shared/constants/chains/supported";
3
+ import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
4
+ import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
5
+ import { useMemo } from "react";
6
+ import { http } from "viem";
7
+ import { createConfig } from "wagmi";
8
+
9
+ export function useWagmiConfig(partnerId: string, rpcUrls?: Record<number, string>) {
10
+ // Stringify rpcUrls for stable comparison to prevent wagmiConfig recreation
11
+ // when parent component passes new object references with same content
12
+ const rpcUrlsString = useMemo(() => (rpcUrls ? JSON.stringify(rpcUrls) : undefined), [rpcUrls]);
13
+
14
+ const ecocystemConfig = useMemo(() => {
15
+ return {
16
+ ecosystemId: ecosystemWalletId,
17
+ partnerId: partnerId,
18
+ client,
19
+ };
20
+ }, [partnerId]);
21
+
22
+ /**
23
+ * Creates wagmi config with optional custom RPC URLs
24
+ * @param rpcUrls - Optional mapping of chain IDs to RPC URLs
25
+ */
26
+ const wagmiConfig = useMemo(() => {
27
+ const parsedRpcUrls = rpcUrlsString ? JSON.parse(rpcUrlsString) : undefined;
28
+
29
+ return createConfig({
30
+ chains: [supportedChains[0], ...supportedChains.slice(1)],
31
+ transports: Object.fromEntries(supportedChains.map(chain => [chain.id, http(parsedRpcUrls?.[chain.id])])),
32
+ connectors: [
33
+ inAppWalletConnector({
34
+ ...ecocystemConfig,
35
+ client,
36
+ }),
37
+ // injected(),
38
+ // coinbaseWallet({ appName: "HypeDuel" }),
39
+ ],
40
+ });
41
+ }, [rpcUrlsString, ecocystemConfig]);
42
+
43
+ return wagmiConfig;
44
+ }
@@ -35,8 +35,6 @@ export interface SignInWithB3ModalProps extends BaseModalProps {
35
35
  sessionKeyAddress?: Address;
36
36
  /** Unique identifier for the partner application */
37
37
  partnerId: string;
38
- /** Whether to authenticate with Sign In With Ethereum */
39
- loginWithSiwe?: boolean;
40
38
  /** Whether to close the modal after successful login */
41
39
  closeAfterLogin?: boolean;
42
40
  /** Source of the sign-in request */
@@ -0,0 +1,9 @@
1
+ // Export types
2
+ export * from "./types";
3
+
4
+ // Export services
5
+ export * from "./services";
6
+
7
+ // Re-export auth token getter from shared for convenience
8
+ // Note: Auth token is managed by B3 Global Account authentication
9
+ 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";
@@ -0,0 +1,217 @@
1
+ import { getAuthToken } from "@b3dotfun/sdk/shared/utils/auth-token";
2
+ import type {
3
+ NotificationHistory,
4
+ NotificationPreferences,
5
+ SendNotificationRequest,
6
+ TelegramLinkResponse,
7
+ TelegramStatusResponse,
8
+ UserData,
9
+ } from "../types";
10
+
11
+ const DEFAULT_API_URL = "https://notifications.b3.fun";
12
+
13
+ let apiUrl: string = DEFAULT_API_URL;
14
+
15
+ export function setApiUrl(url: string) {
16
+ apiUrl = url;
17
+ }
18
+
19
+ export function getApiUrl(): string {
20
+ return apiUrl;
21
+ }
22
+
23
+ function getHeaders(includeAuth = false): HeadersInit {
24
+ const headers: HeadersInit = {
25
+ "Content-Type": "application/json",
26
+ };
27
+
28
+ if (includeAuth) {
29
+ const token = getAuthToken();
30
+ if (token) {
31
+ headers["Authorization"] = `Bearer ${token}`;
32
+ }
33
+ }
34
+
35
+ return headers;
36
+ }
37
+
38
+ export const notificationsAPI = {
39
+ // ===== USER MANAGEMENT =====
40
+
41
+ /**
42
+ * Register the current user (userId extracted from JWT)
43
+ */
44
+ async registerUser() {
45
+ const res = await fetch(`${apiUrl}/users`, {
46
+ method: "POST",
47
+ headers: getHeaders(true),
48
+ });
49
+ if (!res.ok) {
50
+ const errorBody = await res.text().catch(() => "Could not read error body");
51
+ throw new Error(`API Error: ${res.status} ${res.statusText} - ${errorBody}`);
52
+ }
53
+ return res.json();
54
+ },
55
+
56
+ /**
57
+ * Get current user's profile and preferences
58
+ */
59
+ async getUser(): Promise<UserData> {
60
+ const res = await fetch(`${apiUrl}/users/me`, {
61
+ headers: getHeaders(true),
62
+ });
63
+ return res.json();
64
+ },
65
+
66
+ /**
67
+ * Get current user's notification history
68
+ */
69
+ async getHistory(appId?: string, limit = 100): Promise<NotificationHistory[]> {
70
+ const params = new URLSearchParams();
71
+ if (appId) params.append("appId", appId);
72
+ params.append("limit", limit.toString());
73
+
74
+ const res = await fetch(`${apiUrl}/users/me/history?${params}`, {
75
+ headers: getHeaders(true),
76
+ });
77
+ return res.json();
78
+ },
79
+
80
+ // ===== CHANNELS =====
81
+
82
+ /**
83
+ * Add a notification channel for current user
84
+ */
85
+ async addChannel(channelType: string, channelIdentifier: string, metadata?: Record<string, any>) {
86
+ const res = await fetch(`${apiUrl}/users/me/channels`, {
87
+ method: "POST",
88
+ headers: getHeaders(true),
89
+ body: JSON.stringify({
90
+ channelType,
91
+ channelIdentifier,
92
+ enabled: true,
93
+ metadata,
94
+ }),
95
+ });
96
+ return res.json();
97
+ },
98
+
99
+ /**
100
+ * Connect email for current user
101
+ */
102
+ async connectEmail(email: string) {
103
+ return this.addChannel("email", email);
104
+ },
105
+
106
+ /**
107
+ * Update a notification channel
108
+ */
109
+ async updateChannel(
110
+ channelId: string,
111
+ updates: { enabled?: boolean; channelIdentifier?: string; metadata?: Record<string, any> },
112
+ ) {
113
+ const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
114
+ method: "PUT",
115
+ headers: getHeaders(true),
116
+ body: JSON.stringify(updates),
117
+ });
118
+ return res.json();
119
+ },
120
+
121
+ /**
122
+ * Delete a notification channel
123
+ */
124
+ async deleteChannel(channelId: string) {
125
+ const res = await fetch(`${apiUrl}/users/me/channels/${channelId}`, {
126
+ method: "DELETE",
127
+ headers: getHeaders(true),
128
+ });
129
+ return res.json();
130
+ },
131
+
132
+ // ===== TELEGRAM =====
133
+
134
+ /**
135
+ * Get Telegram deep link for current user
136
+ */
137
+ async getTelegramLink(): Promise<TelegramLinkResponse> {
138
+ const res = await fetch(`${apiUrl}/telegram/request-link`, {
139
+ method: "POST",
140
+ headers: getHeaders(true),
141
+ });
142
+ return res.json();
143
+ },
144
+
145
+ /**
146
+ * Check current user's Telegram connection status
147
+ */
148
+ async checkTelegramStatus(): Promise<TelegramStatusResponse> {
149
+ const res = await fetch(`${apiUrl}/telegram/status/me`, {
150
+ headers: getHeaders(true),
151
+ });
152
+ return res.json();
153
+ },
154
+
155
+ // ===== APP PREFERENCES =====
156
+
157
+ /**
158
+ * Save notification preferences for an app
159
+ * @param appId - The application ID
160
+ * @param settings - Notification preferences including channels, type, and enabled status (defaults to true)
161
+ */
162
+ async savePreferences(appId: string, settings: NotificationPreferences) {
163
+ const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
164
+ method: "POST",
165
+ headers: getHeaders(true),
166
+ body: JSON.stringify({ enabled: true, ...settings }),
167
+ });
168
+ return res.json();
169
+ },
170
+
171
+ /**
172
+ * Get notification settings for an app
173
+ */
174
+ async getAppSettings(appId: string) {
175
+ const res = await fetch(`${apiUrl}/users/me/apps/${appId}/settings`, {
176
+ headers: getHeaders(true),
177
+ });
178
+ return res.json();
179
+ },
180
+
181
+ // ===== IN-APP NOTIFICATIONS =====
182
+
183
+ /**
184
+ * Get current user's in-app notifications
185
+ */
186
+ async getInAppNotifications() {
187
+ const res = await fetch(`${apiUrl}/users/me/notifications`, {
188
+ headers: getHeaders(true),
189
+ });
190
+ return res.json();
191
+ },
192
+
193
+ /**
194
+ * Mark a notification as read
195
+ */
196
+ async markNotificationAsRead(notificationId: string) {
197
+ const res = await fetch(`${apiUrl}/users/me/notifications/${notificationId}/read`, {
198
+ method: "PUT",
199
+ headers: getHeaders(true),
200
+ });
201
+ return res.json();
202
+ },
203
+
204
+ // ===== SENDING NOTIFICATIONS =====
205
+
206
+ /**
207
+ * Send a notification (requires auth)
208
+ */
209
+ async sendNotification(data: SendNotificationRequest) {
210
+ const res = await fetch(`${apiUrl}/send`, {
211
+ method: "POST",
212
+ headers: getHeaders(true),
213
+ body: JSON.stringify(data),
214
+ });
215
+ return res.json();
216
+ },
217
+ };
@@ -0,0 +1 @@
1
+ export * from "./api";
@@ -0,0 +1,58 @@
1
+ export type ChannelType = "email" | "telegram" | "discord" | "sms" | "whatsapp" | "in_app";
2
+
3
+ export interface NotificationChannel {
4
+ id: number;
5
+ channel_type: ChannelType;
6
+ enabled: number;
7
+ channel_identifier: string;
8
+ }
9
+
10
+ export interface UserData {
11
+ user: {
12
+ id: number;
13
+ user_id: string;
14
+ };
15
+ channels: NotificationChannel[];
16
+ appSettings: Array<{
17
+ app_id: string;
18
+ notification_type: string;
19
+ enabled: number;
20
+ channels: string;
21
+ }>;
22
+ }
23
+
24
+ export interface NotificationHistory {
25
+ id: string;
26
+ app_id: string;
27
+ notification_type: string;
28
+ title: string;
29
+ message: string;
30
+ created_at: string;
31
+ read: boolean;
32
+ }
33
+
34
+ export interface TelegramLinkResponse {
35
+ deepLink: string;
36
+ verificationCode: string;
37
+ botUsername: string;
38
+ }
39
+
40
+ export interface TelegramStatusResponse {
41
+ connected: boolean;
42
+ chatId?: string;
43
+ }
44
+
45
+ export interface NotificationPreferences {
46
+ notificationType: string;
47
+ channels: string[];
48
+ enabled?: boolean;
49
+ }
50
+
51
+ export interface SendNotificationRequest {
52
+ userId: string;
53
+ appId: string;
54
+ notificationType: string;
55
+ message: string;
56
+ title?: string;
57
+ data?: Record<string, any>;
58
+ }
@@ -0,0 +1,13 @@
1
+ import Cookies from "js-cookie";
2
+
3
+ const B3_AUTH_COOKIE_NAME = "b3-auth";
4
+
5
+ /**
6
+ * Get the authentication token from the B3 auth cookie
7
+ * This token is managed by the B3 Global Account authentication system
8
+ *
9
+ * @returns The JWT token string or null if not found
10
+ */
11
+ export function getAuthToken(): string | null {
12
+ return Cookies.get(B3_AUTH_COOKIE_NAME) || null;
13
+ }
@@ -1,4 +1,5 @@
1
1
  // Export utility functions
2
+ export * from "./auth-token";
2
3
  export * from "./cn";
3
4
  export * from "./formatNumber";
4
5
  export * from "./formatUsername";