@frak-labs/react-sdk 0.0.3
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/LICENSE +674 -0
- package/README.md +8 -0
- package/dist/index.cjs +389 -0
- package/dist/index.d.cts +298 -0
- package/dist/index.d.ts +298 -0
- package/dist/index.js +389 -0
- package/package.json +76 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { PropsWithChildren, CSSProperties, ReactNode } from 'react';
|
|
3
|
+
import * as _frak_labs_core_sdk from '@frak-labs/core-sdk';
|
|
4
|
+
import { FrakWalletSdkConfig, FrakClient, WalletStatusReturnType, SendTransactionReturnType, FrakRpcError, SiweAuthenticateReturnType, DisplayModalParamsType, ModalStepTypes, SendInteractionReturnType, SendInteractionParamsType, ModalRpcStepsResultType, OpenSsoParamsType, GetProductInformationReturnType } from '@frak-labs/core-sdk';
|
|
5
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
6
|
+
import { UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';
|
|
7
|
+
import { SendTransactionParams, SiweAuthenticateModalParams, ProcessReferralOptions } from '@frak-labs/core-sdk/actions';
|
|
8
|
+
import { Hex } from 'viem';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The context that will keep the Frak Wallet SDK configuration
|
|
12
|
+
* @ignore
|
|
13
|
+
*/
|
|
14
|
+
declare const FrakConfigContext: react.Context<FrakWalletSdkConfig | undefined>;
|
|
15
|
+
/**
|
|
16
|
+
* Props to instantiate the Frak Wallet SDK configuration provider
|
|
17
|
+
*
|
|
18
|
+
* @group provider
|
|
19
|
+
*/
|
|
20
|
+
type FrakConfigProviderProps = {
|
|
21
|
+
/**
|
|
22
|
+
* The wanted Frak configuration
|
|
23
|
+
* @see {@link @frak-labs/core-sdk!index.FrakWalletSdkConfig | FrakWalletSdkConfig}
|
|
24
|
+
*/
|
|
25
|
+
config: FrakWalletSdkConfig;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Simple config provider for the Frak Wallet SDK
|
|
29
|
+
*
|
|
30
|
+
* Should be wrapped within a {@link @tanstack/react-query!QueryClientProvider | `QueryClientProvider`}
|
|
31
|
+
*
|
|
32
|
+
* @group provider
|
|
33
|
+
*
|
|
34
|
+
* @param parameters
|
|
35
|
+
*/
|
|
36
|
+
declare function FrakConfigProvider(parameters: PropsWithChildren<FrakConfigProviderProps>): react.FunctionComponentElement<react.ProviderProps<FrakWalletSdkConfig | undefined>>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The context that will keep the Frak Wallet SDK client
|
|
40
|
+
* @ignore
|
|
41
|
+
*/
|
|
42
|
+
declare const FrakIFrameClientContext: react.Context<FrakClient | undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* Props to instantiate the Frak Wallet SDK configuration provider
|
|
45
|
+
*
|
|
46
|
+
* @group provider
|
|
47
|
+
*/
|
|
48
|
+
type FrakIFrameClientProps = {
|
|
49
|
+
config: FrakWalletSdkConfig;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* IFrame client provider for the Frak Wallet SDK
|
|
53
|
+
* It will automatically create the frak wallet iFrame (required for the wallet to communicate with the SDK securely), and provide it in the context
|
|
54
|
+
*
|
|
55
|
+
* @group provider
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* This provider must be wrapped within a {@link FrakConfigProvider} to work properly
|
|
59
|
+
*
|
|
60
|
+
* @param args
|
|
61
|
+
* @param args.style - Some custom styles to apply to the iFrame
|
|
62
|
+
* @param args.children - Descedant components that will have access to the Frak Client
|
|
63
|
+
*/
|
|
64
|
+
declare function FrakIFrameClientProvider({ style, children, }: {
|
|
65
|
+
style?: CSSProperties;
|
|
66
|
+
children?: ReactNode;
|
|
67
|
+
}): react.FunctionComponentElement<{
|
|
68
|
+
children?: ReactNode | undefined;
|
|
69
|
+
}>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get the current Frak config
|
|
73
|
+
* @throws {FrakRpcError} if the config is not found (only if this hooks is used outside a FrakConfigProvider)
|
|
74
|
+
* @group hooks
|
|
75
|
+
*
|
|
76
|
+
* @see {@link @frak-labs/react-sdk!FrakConfigProvider | FrakConfigProvider} for the config provider
|
|
77
|
+
* @see {@link @frak-labs/core-sdk!index.FrakWalletSdkConfig | FrakWalletSdkConfig} for the config type
|
|
78
|
+
*/
|
|
79
|
+
declare function useFrakConfig(): _frak_labs_core_sdk.FrakWalletSdkConfig;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get the current Frak client
|
|
83
|
+
*
|
|
84
|
+
* @group hooks
|
|
85
|
+
*/
|
|
86
|
+
declare function useFrakClient(): _frak_labs_core_sdk.FrakClient | undefined;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Hook that return a query helping to get the current wallet status.
|
|
90
|
+
*
|
|
91
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.watchWalletStatus | `watchWalletStatus()`} action
|
|
92
|
+
*
|
|
93
|
+
* @group hooks
|
|
94
|
+
*
|
|
95
|
+
* @returns
|
|
96
|
+
* The query hook wrapping the `watchWalletStatus()` action
|
|
97
|
+
* The `data` result is a {@link @frak-labs/core-sdk!index.WalletStatusReturnType | `WalletStatusReturnType`}
|
|
98
|
+
*
|
|
99
|
+
* @see {@link @frak-labs/core-sdk!actions.watchWalletStatus | `watchWalletStatus()`} for more info about the underlying action
|
|
100
|
+
* @see {@link @tanstack/react-query!useQuery | `useQuery()`} for more info about the useQuery response
|
|
101
|
+
*/
|
|
102
|
+
declare function useWalletStatus(): _tanstack_react_query.UseQueryResult<WalletStatusReturnType, Error>;
|
|
103
|
+
|
|
104
|
+
/** @ignore */
|
|
105
|
+
type MutationOptions$4 = Omit<UseMutationOptions<SendTransactionReturnType, FrakRpcError, SendTransactionParams>, "mutationFn" | "mutationKey">;
|
|
106
|
+
/** @inline */
|
|
107
|
+
interface UseSendTransactionParams {
|
|
108
|
+
/**
|
|
109
|
+
* Optional mutation options, see {@link @tanstack/react-query!useMutation | `useMutation()`} for more infos
|
|
110
|
+
*/
|
|
111
|
+
mutations?: MutationOptions$4;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Hook that return a mutation helping to send a transaction
|
|
115
|
+
*
|
|
116
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.sendTransaction | `sendTransaction()`} action
|
|
117
|
+
*
|
|
118
|
+
* @param args
|
|
119
|
+
*
|
|
120
|
+
* @group hooks
|
|
121
|
+
*
|
|
122
|
+
* @returns
|
|
123
|
+
* The mutation hook wrapping the `sendTransaction()` action
|
|
124
|
+
* The `mutate` and `mutateAsync` argument is of type {@link @frak-labs/core-sdk!actions.SendTransactionParams | `SendTransactionParams`}
|
|
125
|
+
* The `data` result is a {@link @frak-labs/core-sdk!index.SendTransactionReturnType | `SendTransactionReturnType`}
|
|
126
|
+
*
|
|
127
|
+
* @see {@link @frak-labs/core-sdk!actions.sendTransaction | `sendTransaction()`} for more info about the underlying action
|
|
128
|
+
* @see {@link @tanstack/react-query!useMutation | `useMutation()`} for more info about the mutation options and response
|
|
129
|
+
*/
|
|
130
|
+
declare function useSendTransactionAction({ mutations, }?: UseSendTransactionParams): _tanstack_react_query.UseMutationResult<SendTransactionReturnType, FrakRpcError<undefined>, SendTransactionParams, unknown>;
|
|
131
|
+
|
|
132
|
+
/** @inline */
|
|
133
|
+
type MutationOptions$3 = Omit<UseMutationOptions<SiweAuthenticateReturnType, FrakRpcError, SiweAuthenticateModalParams>, "mutationFn" | "mutationKey">;
|
|
134
|
+
/** @ignore */
|
|
135
|
+
interface UseSiweAuthenticateParams {
|
|
136
|
+
/**
|
|
137
|
+
* Optional mutation options, see {@link @tanstack/react-query!useMutation | `useMutation()`} for more infos
|
|
138
|
+
*/
|
|
139
|
+
mutations?: MutationOptions$3;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Hook that return a mutation helping to send perform a SIWE authentication
|
|
143
|
+
*
|
|
144
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.sendTransaction | `sendTransaction()`} action
|
|
145
|
+
*
|
|
146
|
+
* @param args
|
|
147
|
+
*
|
|
148
|
+
* @group hooks
|
|
149
|
+
*
|
|
150
|
+
* @returns
|
|
151
|
+
* The mutation hook wrapping the `siweAuthenticate()` action
|
|
152
|
+
* The `mutate` and `mutateAsync` argument is of type {@link @frak-labs/core-sdk!actions.SiweAuthenticateModalParams | `SiweAuthenticateModalParams`}
|
|
153
|
+
* The `data` result is a {@link @frak-labs/core-sdk!index.SiweAuthenticateReturnType | `SiweAuthenticateReturnType`}
|
|
154
|
+
*
|
|
155
|
+
* @see {@link @frak-labs/core-sdk!actions.siweAuthenticate | `siweAuthenticate()`} for more info about the underlying action
|
|
156
|
+
* @see {@link @tanstack/react-query!useMutation | `useMutation()`} for more info about the mutation options and response
|
|
157
|
+
*/
|
|
158
|
+
declare function useSiweAuthenticate({ mutations, }?: UseSiweAuthenticateParams): _tanstack_react_query.UseMutationResult<SiweAuthenticateReturnType, FrakRpcError<undefined>, SiweAuthenticateModalParams, unknown>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Helper hook to automatically submit a referral interaction when detected
|
|
162
|
+
*
|
|
163
|
+
* @group hooks
|
|
164
|
+
*
|
|
165
|
+
* @param args
|
|
166
|
+
* @param args.productId - The product id to interact with (if not specified will be recomputed from the current domain)
|
|
167
|
+
* @param args.modalConfig - The modal configuration to display if the user is not logged in
|
|
168
|
+
* @param args.options - Some options for the referral interaction
|
|
169
|
+
*
|
|
170
|
+
* @returns The resulting referral state, or a potential error
|
|
171
|
+
*
|
|
172
|
+
* @description This function will automatically handle the referral interaction process
|
|
173
|
+
*
|
|
174
|
+
* @see {@link @frak-labs/core-sdk!actions.processReferral | `processReferral()`} for more details on the automatic referral handling process
|
|
175
|
+
*/
|
|
176
|
+
declare function useReferralInteraction({ productId, modalConfig, options, }?: {
|
|
177
|
+
productId?: Hex;
|
|
178
|
+
modalConfig?: DisplayModalParamsType<ModalStepTypes[]>;
|
|
179
|
+
options?: ProcessReferralOptions;
|
|
180
|
+
}): Error | ("error" | "no-referrer" | "idle" | "processing" | "success" | "no-wallet" | "no-session" | "self-referral");
|
|
181
|
+
|
|
182
|
+
/** @ignore */
|
|
183
|
+
type MutationOptions$2 = Omit<UseMutationOptions<SendInteractionReturnType, FrakRpcError, SendInteractionParamsType>, "mutationFn" | "mutationKey">;
|
|
184
|
+
/** @inline */
|
|
185
|
+
interface UseSendInteractionParams$1 {
|
|
186
|
+
/**
|
|
187
|
+
* Optional mutation options, see {@link @tanstack/react-query!useMutation | `useMutation()`} for more infos
|
|
188
|
+
*/
|
|
189
|
+
mutations?: MutationOptions$2;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Hook that return a mutation helping to send a user interaction
|
|
193
|
+
*
|
|
194
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.sendInteraction | `sendInteraction()`} action
|
|
195
|
+
*
|
|
196
|
+
* @param args
|
|
197
|
+
*
|
|
198
|
+
* @group hooks
|
|
199
|
+
*
|
|
200
|
+
* @returns
|
|
201
|
+
* The mutation hook wrapping the `sendInteraction()` action
|
|
202
|
+
* The `mutate` and `mutateAsync` argument is of type {@link @frak-labs/core-sdk!index.SendInteractionParamsType | `SendInteractionParamsType`}
|
|
203
|
+
* The `data` result is a {@link @frak-labs/core-sdk!index.SendInteractionReturnType | `SendInteractionReturnType`}
|
|
204
|
+
*
|
|
205
|
+
* @see {@link @frak-labs/core-sdk!actions.sendInteraction | `sendInteraction()`} for more info about the underlying action
|
|
206
|
+
* @see {@link @tanstack/react-query!useMutation | `useMutation()`} for more info about the mutation options and response
|
|
207
|
+
*/
|
|
208
|
+
declare function useSendInteraction({ mutations, }?: UseSendInteractionParams$1): _tanstack_react_query.UseMutationResult<SendInteractionReturnType, FrakRpcError<undefined>, SendInteractionParamsType, unknown>;
|
|
209
|
+
|
|
210
|
+
/** @ignore */
|
|
211
|
+
type MutationOptions$1<T extends ModalStepTypes[]> = Omit<UseMutationOptions<ModalRpcStepsResultType<T>, FrakRpcError, DisplayModalParamsType<T>>, "mutationFn" | "mutationKey">;
|
|
212
|
+
/** @inline */
|
|
213
|
+
interface UseDisplayModalParams<T extends ModalStepTypes[] = ModalStepTypes[]> {
|
|
214
|
+
/**
|
|
215
|
+
* Optional mutation options, see {@link @tanstack/react-query!useMutation | `useMutation()`} for more infos
|
|
216
|
+
*/
|
|
217
|
+
mutations?: MutationOptions$1<T>;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Hook that return a mutation helping to display a modal to the user
|
|
221
|
+
*
|
|
222
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.displayModal | `displayModal()`} action
|
|
223
|
+
*
|
|
224
|
+
* @param args
|
|
225
|
+
*
|
|
226
|
+
* @typeParam T
|
|
227
|
+
* The modal steps types to display (the result will correspond to the steps types asked in params)
|
|
228
|
+
* An array of {@link @frak-labs/core-sdk!index.ModalStepTypes | `ModalStepTypes`}
|
|
229
|
+
* If not provided, it will default to a generic array of `ModalStepTypes`
|
|
230
|
+
*
|
|
231
|
+
* @group hooks
|
|
232
|
+
*
|
|
233
|
+
* @returns
|
|
234
|
+
* The mutation hook wrapping the `displayModal()` action
|
|
235
|
+
* The `mutate` and `mutateAsync` argument is of type {@link @frak-labs/core-sdk!index.DisplayModalParamsType | `DisplayModalParamsType<T>`}, with type params `T` being the modal steps types to display
|
|
236
|
+
* The `data` result is a {@link @frak-labs/core-sdk!index.ModalRpcStepsResultType | `ModalRpcStepsResultType`}
|
|
237
|
+
*
|
|
238
|
+
* @see {@link @frak-labs/core-sdk!actions.displayModal | `displayModal()`} for more info about the underlying action
|
|
239
|
+
* @see {@link @tanstack/react-query!useMutation | `useMutation()`} for more info about the mutation options and response
|
|
240
|
+
*/
|
|
241
|
+
declare function useDisplayModal<T extends ModalStepTypes[] = ModalStepTypes[]>({ mutations, }?: UseDisplayModalParams<T>): _tanstack_react_query.UseMutationResult<ModalRpcStepsResultType<T>, FrakRpcError<undefined>, DisplayModalParamsType<T>, unknown>;
|
|
242
|
+
|
|
243
|
+
/** @ignore */
|
|
244
|
+
type MutationOptions = Omit<UseMutationOptions<void, FrakRpcError, OpenSsoParamsType>, "mutationFn" | "mutationKey">;
|
|
245
|
+
/** @inline */
|
|
246
|
+
interface UseSendInteractionParams {
|
|
247
|
+
/**
|
|
248
|
+
* Optional mutation options, see {@link @tanstack/react-query!useMutation | `useMutation()`} for more infos
|
|
249
|
+
*/
|
|
250
|
+
mutations?: MutationOptions;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Hook that return a mutation helping to open the SSO page
|
|
254
|
+
*
|
|
255
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.openSso | `openSso()`} action
|
|
256
|
+
*
|
|
257
|
+
* @param args
|
|
258
|
+
*
|
|
259
|
+
* @group hooks
|
|
260
|
+
*
|
|
261
|
+
* @returns
|
|
262
|
+
* The mutation hook wrapping the `openSso()` action
|
|
263
|
+
* The `mutate` and `mutateAsync` argument is of type {@link @frak-labs/core-sdk!index.OpenSsoParamsType | `OpenSsoParamsType`}
|
|
264
|
+
* The mutation doesn't output any value
|
|
265
|
+
*
|
|
266
|
+
* @see {@link @frak-labs/core-sdk!actions.openSso | `openSso()`} for more info about the underlying action
|
|
267
|
+
* @see {@link @tanstack/react-query!useMutation | `useMutation()`} for more info about the mutation options and response
|
|
268
|
+
*/
|
|
269
|
+
declare function useOpenSso({ mutations }?: UseSendInteractionParams): _tanstack_react_query.UseMutationResult<void, FrakRpcError<undefined>, OpenSsoParamsType, unknown>;
|
|
270
|
+
|
|
271
|
+
/** @ignore */
|
|
272
|
+
type QueryOptions = Omit<UseQueryOptions<GetProductInformationReturnType, FrakRpcError, undefined>, "queryKey" | "queryFn">;
|
|
273
|
+
/** @inline */
|
|
274
|
+
interface UseGetProductInformationParams {
|
|
275
|
+
/**
|
|
276
|
+
* Optional query options, see {@link @tanstack/react-query!useQuery | `useQuery()`} for more infos
|
|
277
|
+
*/
|
|
278
|
+
query?: QueryOptions;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Hook that return a query helping to get the current product information
|
|
282
|
+
*
|
|
283
|
+
* It's a {@link @tanstack/react-query!home | `tanstack`} wrapper around the {@link @frak-labs/core-sdk!actions.getProductInformation | `getProductInformation()`} action
|
|
284
|
+
*
|
|
285
|
+
* @param args
|
|
286
|
+
*
|
|
287
|
+
* @group hooks
|
|
288
|
+
*
|
|
289
|
+
* @returns
|
|
290
|
+
* The query hook wrapping the `getProductInformation()` action
|
|
291
|
+
* The `data` result is a {@link @frak-labs/core-sdk!index.GetProductInformationReturnType | `GetProductInformationReturnType`}
|
|
292
|
+
*
|
|
293
|
+
* @see {@link @frak-labs/core-sdk!actions.getProductInformation | `getProductInformation()`} for more info about the underlying action
|
|
294
|
+
* @see {@link @tanstack/react-query!useQuery | `useQuery()`} for more info about the useQuery options and response
|
|
295
|
+
*/
|
|
296
|
+
declare function useGetProductInformation({ query, }?: UseGetProductInformationParams): _tanstack_react_query.UseQueryResult<undefined, FrakRpcError<undefined>>;
|
|
297
|
+
|
|
298
|
+
export { FrakConfigContext, FrakConfigProvider, type FrakConfigProviderProps, FrakIFrameClientContext, type FrakIFrameClientProps, FrakIFrameClientProvider, useDisplayModal, useFrakClient, useFrakConfig, useGetProductInformation, useOpenSso, useReferralInteraction, useSendInteraction, useSendTransactionAction, useSiweAuthenticate, useWalletStatus };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// src/provider/FrakConfigProvider.ts
|
|
2
|
+
import { createContext, createElement } from "react";
|
|
3
|
+
var FrakConfigContext = createContext(
|
|
4
|
+
void 0
|
|
5
|
+
);
|
|
6
|
+
function FrakConfigProvider(parameters) {
|
|
7
|
+
const { children, config } = parameters;
|
|
8
|
+
return createElement(
|
|
9
|
+
FrakConfigContext.Provider,
|
|
10
|
+
{
|
|
11
|
+
value: {
|
|
12
|
+
...config,
|
|
13
|
+
walletUrl: config.walletUrl ?? "https://wallet.frak.id",
|
|
14
|
+
domain: config.domain ?? (typeof window !== "undefined" ? window?.location?.host : void 0) ?? "not-found"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
children
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/provider/FrakIFrameClientProvider.ts
|
|
22
|
+
import {
|
|
23
|
+
baseIframeProps,
|
|
24
|
+
createIFrameFrakClient
|
|
25
|
+
} from "@frak-labs/core-sdk";
|
|
26
|
+
import {
|
|
27
|
+
Fragment,
|
|
28
|
+
createContext as createContext2,
|
|
29
|
+
createElement as createElement2,
|
|
30
|
+
useState as useState3
|
|
31
|
+
} from "react";
|
|
32
|
+
|
|
33
|
+
// src/hook/useFrakConfig.ts
|
|
34
|
+
import { FrakRpcError, RpcErrorCodes } from "@frak-labs/core-sdk";
|
|
35
|
+
import { useContext } from "react";
|
|
36
|
+
function useFrakConfig() {
|
|
37
|
+
const config = useContext(FrakConfigContext);
|
|
38
|
+
if (!config) {
|
|
39
|
+
throw new FrakRpcError(
|
|
40
|
+
RpcErrorCodes.configError,
|
|
41
|
+
"Frak config not found"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return config;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/hook/useFrakClient.ts
|
|
48
|
+
import { useContext as useContext2 } from "react";
|
|
49
|
+
function useFrakClient() {
|
|
50
|
+
return useContext2(FrakIFrameClientContext);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/hook/useWalletStatus.ts
|
|
54
|
+
import {
|
|
55
|
+
ClientNotFound
|
|
56
|
+
} from "@frak-labs/core-sdk";
|
|
57
|
+
import { watchWalletStatus } from "@frak-labs/core-sdk/actions";
|
|
58
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
59
|
+
import { useCallback } from "react";
|
|
60
|
+
function useWalletStatus() {
|
|
61
|
+
const queryClient = useQueryClient();
|
|
62
|
+
const client = useFrakClient();
|
|
63
|
+
const newStatusUpdated = useCallback(
|
|
64
|
+
(event) => {
|
|
65
|
+
queryClient.setQueryData(
|
|
66
|
+
["frak-sdk", "wallet-status-listener"],
|
|
67
|
+
event
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
[queryClient]
|
|
71
|
+
);
|
|
72
|
+
return useQuery({
|
|
73
|
+
gcTime: 0,
|
|
74
|
+
staleTime: 0,
|
|
75
|
+
queryKey: ["frak-sdk", "wallet-status-listener"],
|
|
76
|
+
queryFn: async () => {
|
|
77
|
+
if (!client) {
|
|
78
|
+
throw new ClientNotFound();
|
|
79
|
+
}
|
|
80
|
+
return watchWalletStatus(client, newStatusUpdated);
|
|
81
|
+
},
|
|
82
|
+
enabled: !!client
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/hook/useSendTransaction.ts
|
|
87
|
+
import {
|
|
88
|
+
ClientNotFound as ClientNotFound2
|
|
89
|
+
} from "@frak-labs/core-sdk";
|
|
90
|
+
import {
|
|
91
|
+
sendTransaction
|
|
92
|
+
} from "@frak-labs/core-sdk/actions";
|
|
93
|
+
import { useMutation } from "@tanstack/react-query";
|
|
94
|
+
function useSendTransactionAction({
|
|
95
|
+
mutations
|
|
96
|
+
} = {}) {
|
|
97
|
+
const client = useFrakClient();
|
|
98
|
+
return useMutation({
|
|
99
|
+
...mutations,
|
|
100
|
+
mutationKey: ["frak-sdk", "send-transaction"],
|
|
101
|
+
mutationFn: async (params) => {
|
|
102
|
+
if (!client) {
|
|
103
|
+
throw new ClientNotFound2();
|
|
104
|
+
}
|
|
105
|
+
return sendTransaction(client, params);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/hook/useSiweAuthenticate.ts
|
|
111
|
+
import {
|
|
112
|
+
ClientNotFound as ClientNotFound3
|
|
113
|
+
} from "@frak-labs/core-sdk";
|
|
114
|
+
import {
|
|
115
|
+
siweAuthenticate
|
|
116
|
+
} from "@frak-labs/core-sdk/actions";
|
|
117
|
+
import { useMutation as useMutation2 } from "@tanstack/react-query";
|
|
118
|
+
function useSiweAuthenticate({
|
|
119
|
+
mutations
|
|
120
|
+
} = {}) {
|
|
121
|
+
const client = useFrakClient();
|
|
122
|
+
return useMutation2({
|
|
123
|
+
...mutations,
|
|
124
|
+
mutationKey: [
|
|
125
|
+
"frak-sdk",
|
|
126
|
+
"siwe-authenticate",
|
|
127
|
+
client?.config.domain ?? "no-domain"
|
|
128
|
+
],
|
|
129
|
+
mutationFn: async (params) => {
|
|
130
|
+
if (!client) {
|
|
131
|
+
throw new ClientNotFound3();
|
|
132
|
+
}
|
|
133
|
+
return siweAuthenticate(client, params);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/hook/helper/useReferralInteraction.ts
|
|
139
|
+
import {
|
|
140
|
+
ClientNotFound as ClientNotFound4
|
|
141
|
+
} from "@frak-labs/core-sdk";
|
|
142
|
+
import {
|
|
143
|
+
processReferral
|
|
144
|
+
} from "@frak-labs/core-sdk/actions";
|
|
145
|
+
import { useQuery as useQuery2 } from "@tanstack/react-query";
|
|
146
|
+
import { useMemo as useMemo3 } from "react";
|
|
147
|
+
|
|
148
|
+
// src/hook/utils/useFrakContext.ts
|
|
149
|
+
import { FrakContextManager } from "@frak-labs/core-sdk";
|
|
150
|
+
import { useCallback as useCallback2, useMemo as useMemo2 } from "react";
|
|
151
|
+
|
|
152
|
+
// src/hook/utils/useWindowLocation.ts
|
|
153
|
+
import { useEffect as useEffect2, useMemo, useState as useState2 } from "react";
|
|
154
|
+
|
|
155
|
+
// src/hook/utils/useMounted.ts
|
|
156
|
+
import { useEffect, useState } from "react";
|
|
157
|
+
function useMounted() {
|
|
158
|
+
const [mounted, setMounted] = useState(false);
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
setMounted(true);
|
|
161
|
+
}, []);
|
|
162
|
+
return mounted;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/hook/utils/useWindowLocation.ts
|
|
166
|
+
function useWindowLocation() {
|
|
167
|
+
const isMounted = useMounted();
|
|
168
|
+
const [location, setLocation] = useState2(
|
|
169
|
+
isMounted ? window.location : void 0
|
|
170
|
+
);
|
|
171
|
+
useEffect2(() => {
|
|
172
|
+
if (!isMounted) return;
|
|
173
|
+
function setWindowLocation() {
|
|
174
|
+
setLocation(window.location);
|
|
175
|
+
}
|
|
176
|
+
if (!location) {
|
|
177
|
+
setWindowLocation();
|
|
178
|
+
}
|
|
179
|
+
window.addEventListener("popstate", setWindowLocation);
|
|
180
|
+
return () => {
|
|
181
|
+
window.removeEventListener("popstate", setWindowLocation);
|
|
182
|
+
};
|
|
183
|
+
}, [isMounted, location]);
|
|
184
|
+
const href = useMemo(() => location?.href, [location?.href]);
|
|
185
|
+
return { location, href };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/hook/utils/useFrakContext.ts
|
|
189
|
+
function useFrakContext() {
|
|
190
|
+
const { location } = useWindowLocation();
|
|
191
|
+
const frakContext = useMemo2(() => {
|
|
192
|
+
if (!location?.href) return null;
|
|
193
|
+
return FrakContextManager.parse({ url: location.href });
|
|
194
|
+
}, [location?.href]);
|
|
195
|
+
const updateContext = useCallback2(
|
|
196
|
+
(newContext) => {
|
|
197
|
+
console.log("Updating context", { newContext });
|
|
198
|
+
FrakContextManager.replaceUrl({
|
|
199
|
+
url: location?.href,
|
|
200
|
+
context: newContext
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
[location?.href]
|
|
204
|
+
);
|
|
205
|
+
return {
|
|
206
|
+
frakContext,
|
|
207
|
+
updateContext
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/hook/helper/useReferralInteraction.ts
|
|
212
|
+
function useReferralInteraction({
|
|
213
|
+
productId,
|
|
214
|
+
modalConfig,
|
|
215
|
+
options
|
|
216
|
+
} = {}) {
|
|
217
|
+
const client = useFrakClient();
|
|
218
|
+
const { frakContext } = useFrakContext();
|
|
219
|
+
const { data: walletStatus } = useWalletStatus();
|
|
220
|
+
const {
|
|
221
|
+
data: referralState,
|
|
222
|
+
error,
|
|
223
|
+
status
|
|
224
|
+
} = useQuery2({
|
|
225
|
+
gcTime: 0,
|
|
226
|
+
staleTime: 0,
|
|
227
|
+
queryKey: [
|
|
228
|
+
"frak-sdk",
|
|
229
|
+
"auto-referral-interaction",
|
|
230
|
+
frakContext?.r ?? "no-referrer",
|
|
231
|
+
walletStatus?.key ?? "no-wallet-status",
|
|
232
|
+
productId ?? "no-product-id"
|
|
233
|
+
],
|
|
234
|
+
queryFn: () => {
|
|
235
|
+
if (!client) {
|
|
236
|
+
throw new ClientNotFound4();
|
|
237
|
+
}
|
|
238
|
+
return processReferral(client, {
|
|
239
|
+
walletStatus,
|
|
240
|
+
frakContext,
|
|
241
|
+
modalConfig,
|
|
242
|
+
productId,
|
|
243
|
+
options
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
enabled: !!walletStatus
|
|
247
|
+
});
|
|
248
|
+
return useMemo3(() => {
|
|
249
|
+
if (status === "pending") return "processing";
|
|
250
|
+
if (status === "error") return error;
|
|
251
|
+
return referralState || "idle";
|
|
252
|
+
}, [referralState, status, error]);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/hook/useSendInteraction.ts
|
|
256
|
+
import {
|
|
257
|
+
ClientNotFound as ClientNotFound5
|
|
258
|
+
} from "@frak-labs/core-sdk";
|
|
259
|
+
import { sendInteraction } from "@frak-labs/core-sdk/actions";
|
|
260
|
+
import { useMutation as useMutation3 } from "@tanstack/react-query";
|
|
261
|
+
function useSendInteraction({
|
|
262
|
+
mutations
|
|
263
|
+
} = {}) {
|
|
264
|
+
const client = useFrakClient();
|
|
265
|
+
return useMutation3({
|
|
266
|
+
...mutations,
|
|
267
|
+
mutationKey: ["frak-sdk", "send-interaction"],
|
|
268
|
+
mutationFn: async (params) => {
|
|
269
|
+
if (!client) {
|
|
270
|
+
throw new ClientNotFound5();
|
|
271
|
+
}
|
|
272
|
+
return sendInteraction(client, params);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/hook/useDisplayModal.ts
|
|
278
|
+
import {
|
|
279
|
+
ClientNotFound as ClientNotFound6
|
|
280
|
+
} from "@frak-labs/core-sdk";
|
|
281
|
+
import { displayModal } from "@frak-labs/core-sdk/actions";
|
|
282
|
+
import { useMutation as useMutation4 } from "@tanstack/react-query";
|
|
283
|
+
function useDisplayModal({
|
|
284
|
+
mutations
|
|
285
|
+
} = {}) {
|
|
286
|
+
const client = useFrakClient();
|
|
287
|
+
return useMutation4({
|
|
288
|
+
...mutations,
|
|
289
|
+
mutationKey: ["frak-sdk", "display-modal"],
|
|
290
|
+
mutationFn: async (args) => {
|
|
291
|
+
if (!client) {
|
|
292
|
+
throw new ClientNotFound6();
|
|
293
|
+
}
|
|
294
|
+
return displayModal(client, args);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/hook/useOpenSso.ts
|
|
300
|
+
import {
|
|
301
|
+
ClientNotFound as ClientNotFound7
|
|
302
|
+
} from "@frak-labs/core-sdk";
|
|
303
|
+
import { openSso } from "@frak-labs/core-sdk/actions";
|
|
304
|
+
import { useMutation as useMutation5 } from "@tanstack/react-query";
|
|
305
|
+
function useOpenSso({ mutations } = {}) {
|
|
306
|
+
const client = useFrakClient();
|
|
307
|
+
return useMutation5({
|
|
308
|
+
...mutations,
|
|
309
|
+
mutationKey: ["frak-sdk", "open-sso"],
|
|
310
|
+
mutationFn: async (params) => {
|
|
311
|
+
if (!client) {
|
|
312
|
+
throw new ClientNotFound7();
|
|
313
|
+
}
|
|
314
|
+
return openSso(client, params);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/hook/useGetProductInformation.ts
|
|
320
|
+
import {
|
|
321
|
+
ClientNotFound as ClientNotFound8
|
|
322
|
+
} from "@frak-labs/core-sdk";
|
|
323
|
+
import { getProductInformation } from "@frak-labs/core-sdk/actions";
|
|
324
|
+
import { useQuery as useQuery3 } from "@tanstack/react-query";
|
|
325
|
+
function useGetProductInformation({
|
|
326
|
+
query
|
|
327
|
+
} = {}) {
|
|
328
|
+
const client = useFrakClient();
|
|
329
|
+
return useQuery3({
|
|
330
|
+
...query,
|
|
331
|
+
queryKey: ["frak-sdk", "get-product-information"],
|
|
332
|
+
queryFn: async () => {
|
|
333
|
+
if (!client) {
|
|
334
|
+
throw new ClientNotFound8();
|
|
335
|
+
}
|
|
336
|
+
return getProductInformation(client);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// src/provider/FrakIFrameClientProvider.ts
|
|
342
|
+
var FrakIFrameClientContext = createContext2(
|
|
343
|
+
void 0
|
|
344
|
+
);
|
|
345
|
+
function FrakIFrameClientProvider({
|
|
346
|
+
style,
|
|
347
|
+
children
|
|
348
|
+
}) {
|
|
349
|
+
const config = useFrakConfig();
|
|
350
|
+
const [client, setClient] = useState3(void 0);
|
|
351
|
+
const iFrame = createElement2("iframe", {
|
|
352
|
+
...baseIframeProps,
|
|
353
|
+
src: `${config.walletUrl}/listener`,
|
|
354
|
+
style: style ?? baseIframeProps.style,
|
|
355
|
+
ref: (iframe) => {
|
|
356
|
+
if (!iframe || client) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
setClient(
|
|
360
|
+
createIFrameFrakClient({
|
|
361
|
+
iframe,
|
|
362
|
+
config
|
|
363
|
+
})
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
const providerComponent = createElement2(
|
|
368
|
+
FrakIFrameClientContext.Provider,
|
|
369
|
+
{ value: client },
|
|
370
|
+
children
|
|
371
|
+
);
|
|
372
|
+
return createElement2(Fragment, null, iFrame, providerComponent);
|
|
373
|
+
}
|
|
374
|
+
export {
|
|
375
|
+
FrakConfigContext,
|
|
376
|
+
FrakConfigProvider,
|
|
377
|
+
FrakIFrameClientContext,
|
|
378
|
+
FrakIFrameClientProvider,
|
|
379
|
+
useDisplayModal,
|
|
380
|
+
useFrakClient,
|
|
381
|
+
useFrakConfig,
|
|
382
|
+
useGetProductInformation,
|
|
383
|
+
useOpenSso,
|
|
384
|
+
useReferralInteraction,
|
|
385
|
+
useSendInteraction,
|
|
386
|
+
useSendTransactionAction,
|
|
387
|
+
useSiweAuthenticate,
|
|
388
|
+
useWalletStatus
|
|
389
|
+
};
|