@b3dotfun/sdk 0.0.47 → 0.0.48

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
@@ -4,7 +4,7 @@ import { Account } from "thirdweb/wallets";
4
4
  * This essentially wraps our useConnect hook to handle the Privy auth flow.
5
5
  * Currently, this is for the basement-privy strategy
6
6
  */
7
- export declare function useHandleConnectWithPrivy(partnerId: string, chain?: Chain, onSuccess?: (account: Account) => void): {
7
+ export declare function useHandleConnectWithPrivy(chain?: Chain, onSuccess?: (account: Account) => void): {
8
8
  connectTw: () => Promise<import("thirdweb/wallets").Wallet | null | undefined>;
9
9
  isLoading: boolean;
10
10
  fullToken: string | null;
@@ -1,4 +1,4 @@
1
- import { useConnect } from "../../../global-account/react/index.js";
1
+ import { useB3, useConnect } from "../../../global-account/react/index.js";
2
2
  import { debugB3React } from "../../../shared/utils/debug.js";
3
3
  import { useIdentityToken, usePrivy } from "@privy-io/react-auth";
4
4
  import { useCallback, useRef, useState } from "react";
@@ -7,7 +7,8 @@ const debug = debugB3React("@@b3:useHandleConnectWithPrivy");
7
7
  * This essentially wraps our useConnect hook to handle the Privy auth flow.
8
8
  * Currently, this is for the basement-privy strategy
9
9
  */
10
- export function useHandleConnectWithPrivy(partnerId, chain, onSuccess) {
10
+ export function useHandleConnectWithPrivy(chain, onSuccess) {
11
+ const { partnerId } = useB3();
11
12
  if (!chain) {
12
13
  throw new Error("Chain is required");
13
14
  }
@@ -0,0 +1,58 @@
1
+ import { Users } from "@b3dotfun/b3-api";
2
+ /**
3
+ * NOTE: THIS IS ONLY MEANT FOR INTERNAL USE, from useOnConnect
4
+ *
5
+ * Custom hook to manage user state with react-query
6
+ * This allows for invalidation and refetching of user data
7
+ */
8
+ export declare function useUserQuery(): {
9
+ user: {
10
+ email?: string | undefined;
11
+ username?: string | undefined;
12
+ telNumber?: string | undefined;
13
+ ens?: string | undefined;
14
+ avatar?: string | undefined;
15
+ preferences?: {} | undefined;
16
+ referredBy?: string | {} | undefined;
17
+ sourceApp?: string | undefined;
18
+ referralCode?: string | undefined;
19
+ userGroups?: number[] | undefined;
20
+ isMigratedFromBSMNT?: boolean | undefined;
21
+ privyLinkedAccounts?: {
22
+ name?: string | undefined;
23
+ address?: string | undefined;
24
+ email?: string | undefined;
25
+ chain_type?: string | undefined;
26
+ lv?: number | undefined;
27
+ wallet_client_type?: string | undefined;
28
+ smart_wallet_type?: string | undefined;
29
+ subject?: string | undefined;
30
+ type: string;
31
+ }[] | undefined;
32
+ twProfiles?: {
33
+ type: string;
34
+ details: {
35
+ id?: string | undefined;
36
+ name?: string | undefined;
37
+ address?: string | undefined;
38
+ email?: string | undefined;
39
+ phone?: string | undefined;
40
+ username?: string | undefined;
41
+ fid?: string | undefined;
42
+ };
43
+ }[] | undefined;
44
+ _id: string | {};
45
+ userId: string;
46
+ smartAccountAddress: string;
47
+ createdAt: number;
48
+ updatedAt: number;
49
+ partnerIds: {
50
+ privyId?: string | undefined;
51
+ thirdwebId?: string | undefined;
52
+ };
53
+ } | undefined;
54
+ setUser: (newUser?: Users) => void;
55
+ refetchUser: () => Promise<void>;
56
+ clearUser: () => void;
57
+ queryKey: string[];
58
+ };
@@ -0,0 +1,83 @@
1
+ import { debugB3React } from "../../../shared/utils/debug.js";
2
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
3
+ const debug = debugB3React("useUserQuery");
4
+ const USER_QUERY_KEY = ["b3-user"];
5
+ /**
6
+ * Retrieves the user from localStorage
7
+ */
8
+ function getUserFromStorage() {
9
+ if (typeof window === "undefined") {
10
+ return null;
11
+ }
12
+ try {
13
+ const storedUser = localStorage.getItem("b3-user");
14
+ return storedUser ? JSON.parse(storedUser) : null;
15
+ }
16
+ catch (error) {
17
+ console.warn("Failed to restore user from localStorage:", error);
18
+ return null;
19
+ }
20
+ }
21
+ /**
22
+ * Saves user to localStorage
23
+ */
24
+ function saveUserToStorage(user) {
25
+ if (typeof window === "undefined") {
26
+ return;
27
+ }
28
+ if (user) {
29
+ localStorage.setItem("b3-user", JSON.stringify(user));
30
+ }
31
+ else {
32
+ localStorage.removeItem("b3-user");
33
+ }
34
+ }
35
+ /**
36
+ * NOTE: THIS IS ONLY MEANT FOR INTERNAL USE, from useOnConnect
37
+ *
38
+ * Custom hook to manage user state with react-query
39
+ * This allows for invalidation and refetching of user data
40
+ */
41
+ export function useUserQuery() {
42
+ const queryClient = useQueryClient();
43
+ // Query to get user data (primarily from cache/localStorage)
44
+ const { data: user } = useQuery({
45
+ queryKey: USER_QUERY_KEY,
46
+ queryFn: getUserFromStorage,
47
+ staleTime: Infinity, // User data doesn't go stale automatically
48
+ gcTime: Infinity, // Keep in cache indefinitely
49
+ initialData: getUserFromStorage,
50
+ });
51
+ // Mutation to update user
52
+ const setUserMutation = useMutation({
53
+ mutationFn: async (newUser) => {
54
+ const userToSave = newUser ?? null;
55
+ saveUserToStorage(userToSave);
56
+ return userToSave;
57
+ },
58
+ onSuccess: data => {
59
+ queryClient.setQueryData(USER_QUERY_KEY, data);
60
+ debug("User updated", data);
61
+ },
62
+ });
63
+ // Helper function to set user (maintains backward compatibility)
64
+ const setUser = (newUser) => {
65
+ setUserMutation.mutate(newUser);
66
+ };
67
+ // Helper function to invalidate and refetch user
68
+ const refetchUser = async () => {
69
+ await queryClient.invalidateQueries({ queryKey: USER_QUERY_KEY });
70
+ return queryClient.refetchQueries({ queryKey: USER_QUERY_KEY });
71
+ };
72
+ // Helper function to clear user
73
+ const clearUser = () => {
74
+ setUser(undefined);
75
+ };
76
+ return {
77
+ user: user ?? undefined,
78
+ setUser,
79
+ refetchUser,
80
+ clearUser,
81
+ queryKey: USER_QUERY_KEY,
82
+ };
83
+ }
@@ -0,0 +1,13 @@
1
+ export declare function useWagmiConfig(partnerId: string, rpcUrls?: Record<number, string>): import("wagmi").Config<readonly [import("viem").Chain, ...import("viem").Chain[]], {
2
+ [k: string]: import("viem").HttpTransport<undefined, false>;
3
+ }, readonly [import("wagmi").CreateConnectorFn<import("thirdweb/dist/types/adapters/eip1193").EIP1193Provider | undefined, {
4
+ connect<withCapabilities extends boolean = false>(parameters?: import("@thirdweb-dev/wagmi-adapter").ConnectionOptions<withCapabilities> | undefined): Promise<{
5
+ accounts: withCapabilities extends true ? readonly {
6
+ address: `0x${string}`;
7
+ capabilities: Record<string, unknown>;
8
+ }[] : readonly `0x${string}`[];
9
+ chainId: number;
10
+ }>;
11
+ }, {
12
+ "thirdweb:lastChainId": number;
13
+ }>]>;
@@ -0,0 +1,39 @@
1
+ import { ecosystemWalletId } from "../../../shared/constants/index.js";
2
+ import { supportedChains } from "../../../shared/constants/chains/supported.js";
3
+ import { client } from "../../../shared/utils/thirdweb.js";
4
+ import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
5
+ import { useMemo } from "react";
6
+ import { http } from "viem";
7
+ import { createConfig } from "wagmi";
8
+ export function useWagmiConfig(partnerId, rpcUrls) {
9
+ // Stringify rpcUrls for stable comparison to prevent wagmiConfig recreation
10
+ // when parent component passes new object references with same content
11
+ const rpcUrlsString = useMemo(() => (rpcUrls ? JSON.stringify(rpcUrls) : undefined), [rpcUrls]);
12
+ const ecocystemConfig = useMemo(() => {
13
+ return {
14
+ ecosystemId: ecosystemWalletId,
15
+ partnerId: partnerId,
16
+ client,
17
+ };
18
+ }, [partnerId]);
19
+ /**
20
+ * Creates wagmi config with optional custom RPC URLs
21
+ * @param rpcUrls - Optional mapping of chain IDs to RPC URLs
22
+ */
23
+ const wagmiConfig = useMemo(() => {
24
+ const parsedRpcUrls = rpcUrlsString ? JSON.parse(rpcUrlsString) : undefined;
25
+ return createConfig({
26
+ chains: [supportedChains[0], ...supportedChains.slice(1)],
27
+ transports: Object.fromEntries(supportedChains.map(chain => [chain.id, http(parsedRpcUrls?.[chain.id])])),
28
+ connectors: [
29
+ inAppWalletConnector({
30
+ ...ecocystemConfig,
31
+ client,
32
+ }),
33
+ // injected(),
34
+ // coinbaseWallet({ appName: "HypeDuel" }),
35
+ ],
36
+ });
37
+ }, [rpcUrlsString, ecocystemConfig]);
38
+ return wagmiConfig;
39
+ }
@@ -32,8 +32,6 @@ export interface SignInWithB3ModalProps extends BaseModalProps {
32
32
  sessionKeyAddress?: Address;
33
33
  /** Unique identifier for the partner application */
34
34
  partnerId: string;
35
- /** Whether to authenticate with Sign In With Ethereum */
36
- loginWithSiwe?: boolean;
37
35
  /** Whether to close the modal after successful login */
38
36
  closeAfterLogin?: boolean;
39
37
  /** Source of the sign-in request */
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./services";
3
+ export { getAuthToken } from "../shared/utils/auth-token";
@@ -0,0 +1,7 @@
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";
@@ -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,67 @@
1
+ import type { NotificationHistory, NotificationPreferences, SendNotificationRequest, TelegramLinkResponse, TelegramStatusResponse, UserData } from "../types";
2
+ export declare function setApiUrl(url: string): void;
3
+ export declare function getApiUrl(): string;
4
+ export declare const notificationsAPI: {
5
+ /**
6
+ * Register the current user (userId extracted from JWT)
7
+ */
8
+ registerUser(): Promise<any>;
9
+ /**
10
+ * Get current user's profile and preferences
11
+ */
12
+ getUser(): Promise<UserData>;
13
+ /**
14
+ * Get current user's notification history
15
+ */
16
+ getHistory(appId?: string, limit?: number): Promise<NotificationHistory[]>;
17
+ /**
18
+ * Add a notification channel for current user
19
+ */
20
+ addChannel(channelType: string, channelIdentifier: string, metadata?: Record<string, any>): Promise<any>;
21
+ /**
22
+ * Connect email for current user
23
+ */
24
+ connectEmail(email: string): Promise<any>;
25
+ /**
26
+ * Update a notification channel
27
+ */
28
+ updateChannel(channelId: string, updates: {
29
+ enabled?: boolean;
30
+ channelIdentifier?: string;
31
+ metadata?: Record<string, any>;
32
+ }): Promise<any>;
33
+ /**
34
+ * Delete a notification channel
35
+ */
36
+ deleteChannel(channelId: string): Promise<any>;
37
+ /**
38
+ * Get Telegram deep link for current user
39
+ */
40
+ getTelegramLink(): Promise<TelegramLinkResponse>;
41
+ /**
42
+ * Check current user's Telegram connection status
43
+ */
44
+ checkTelegramStatus(): Promise<TelegramStatusResponse>;
45
+ /**
46
+ * Save notification preferences for an app
47
+ * @param appId - The application ID
48
+ * @param settings - Notification preferences including channels, type, and enabled status (defaults to true)
49
+ */
50
+ savePreferences(appId: string, settings: NotificationPreferences): Promise<any>;
51
+ /**
52
+ * Get notification settings for an app
53
+ */
54
+ getAppSettings(appId: string): Promise<any>;
55
+ /**
56
+ * Get current user's in-app notifications
57
+ */
58
+ getInAppNotifications(): Promise<any>;
59
+ /**
60
+ * Mark a notification as read
61
+ */
62
+ markNotificationAsRead(notificationId: string): Promise<any>;
63
+ /**
64
+ * Send a notification (requires auth)
65
+ */
66
+ sendNotification(data: SendNotificationRequest): Promise<any>;
67
+ };