@mezo-org/passport 0.4.0-dev.0 → 0.4.0-dev.10
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/dist/src/api/auth.d.ts +78 -0
- package/dist/src/api/auth.d.ts.map +1 -0
- package/dist/src/api/auth.js +77 -0
- package/dist/src/api/auth.js.map +1 -0
- package/dist/src/api/client.d.ts +24 -0
- package/dist/src/api/client.d.ts.map +1 -0
- package/dist/src/api/client.js +54 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/fetch-error.d.ts +5 -0
- package/dist/src/api/fetch-error.d.ts.map +1 -0
- package/dist/src/api/fetch-error.js +8 -0
- package/dist/src/api/fetch-error.js.map +1 -0
- package/dist/src/api/index.d.ts +3 -0
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/src/api/index.js +3 -0
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/api/portal.d.ts +32 -0
- package/dist/src/api/portal.d.ts.map +1 -0
- package/dist/src/api/portal.js +23 -0
- package/dist/src/api/portal.js.map +1 -0
- package/dist/src/assets/DefaultAvatar.d.ts +5 -0
- package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
- package/dist/src/assets/DefaultAvatar.js +21 -0
- package/dist/src/assets/DefaultAvatar.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.js +66 -0
- package/dist/src/components/Dropdown/AccountAddress.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts +14 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.js +44 -0
- package/dist/src/components/Dropdown/AccountAssets.js.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts +7 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.js +18 -0
- package/dist/src/components/Dropdown/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js +30 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Content.d.ts +9 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +69 -0
- package/dist/src/components/Dropdown/Content.js.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts +20 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +64 -0
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts +8 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js +44 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/index.d.ts +3 -0
- package/dist/src/components/Dropdown/index.d.ts.map +1 -0
- package/dist/src/components/Dropdown/index.js +2 -0
- package/dist/src/components/Dropdown/index.js.map +1 -0
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/hooks/constants.d.ts +7 -0
- package/dist/src/hooks/constants.d.ts.map +1 -0
- package/dist/src/hooks/constants.js +7 -0
- package/dist/src/hooks/constants.js.map +1 -0
- package/dist/src/hooks/index.d.ts +10 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +10 -0
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
- package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
- package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
- package/dist/src/hooks/useAuthApiClient.d.ts +2 -0
- package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -0
- package/dist/src/hooks/useAuthApiClient.js +6 -0
- package/dist/src/hooks/useAuthApiClient.js.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +108 -0
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -0
- package/dist/src/hooks/useCreateAccount.js +21 -0
- package/dist/src/hooks/useCreateAccount.js.map +1 -0
- package/dist/src/hooks/useCreateSession.d.ts +76 -0
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -0
- package/dist/src/hooks/useCreateSession.js +29 -0
- package/dist/src/hooks/useCreateSession.js.map +1 -0
- package/dist/src/hooks/useDropdownData.d.ts +45 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +74 -0
- package/dist/src/hooks/useDropdownData.js.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +4 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.js +17 -0
- package/dist/src/hooks/useGetAccountByAddress.js.map +1 -0
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +4 -0
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -0
- package/dist/src/hooks/useGetAccountByMezoId.js +17 -0
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -0
- package/dist/src/hooks/useGetCurrentAccount.d.ts +18 -0
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -0
- package/dist/src/hooks/useGetCurrentAccount.js +37 -0
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -0
- package/dist/src/hooks/useGetSession.d.ts +4 -0
- package/dist/src/hooks/useGetSession.d.ts.map +1 -0
- package/dist/src/hooks/useGetSession.js +15 -0
- package/dist/src/hooks/useGetSession.js.map +1 -0
- package/dist/src/hooks/useLinkAccount.d.ts +72 -0
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -0
- package/dist/src/hooks/useLinkAccount.js +19 -0
- package/dist/src/hooks/useLinkAccount.js.map +1 -0
- package/dist/src/hooks/usePassportContext.d.ts +3 -0
- package/dist/src/hooks/usePassportContext.d.ts.map +1 -0
- package/dist/src/hooks/usePassportContext.js +10 -0
- package/dist/src/hooks/usePassportContext.js.map +1 -0
- package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
- package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
- package/dist/src/hooks/usePortalApiClient.js +6 -0
- package/dist/src/hooks/usePortalApiClient.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +73 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.js +25 -0
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -0
- package/dist/src/hooks/useSignInWithWallet.d.ts +73 -0
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useSignInWithWallet.js +11 -0
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -0
- package/dist/src/hooks/useSignOut.d.ts +72 -0
- package/dist/src/hooks/useSignOut.d.ts.map +1 -0
- package/dist/src/hooks/useSignOut.js +22 -0
- package/dist/src/hooks/useSignOut.js.map +1 -0
- package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useSignUpWithWallet.js +11 -0
- package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts +108 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.js +19 -0
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -0
- package/dist/src/hooks/useWalletAccount.d.ts +12 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +28 -0
- package/dist/src/hooks/useWalletAccount.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/provider.d.ts +17 -0
- package/dist/src/provider.d.ts.map +1 -0
- package/dist/src/provider.js +16 -0
- package/dist/src/provider.js.map +1 -0
- package/dist/src/utils/address.d.ts +15 -0
- package/dist/src/utils/address.d.ts.map +1 -0
- package/dist/src/utils/address.js +35 -0
- package/dist/src/utils/address.js.map +1 -0
- package/dist/src/utils/address.test.d.ts +2 -0
- package/dist/src/utils/address.test.d.ts.map +1 -0
- package/dist/src/utils/address.test.js +32 -0
- package/dist/src/utils/address.test.js.map +1 -0
- package/dist/src/utils/cryptoAssets.d.ts +28 -0
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.js +73 -0
- package/dist/src/utils/cryptoAssets.js.map +1 -0
- package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
- package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.test.js +50 -0
- package/dist/src/utils/cryptoAssets.test.js.map +1 -0
- package/dist/src/utils/currency.d.ts +14 -0
- package/dist/src/utils/currency.d.ts.map +1 -0
- package/dist/src/utils/currency.js +27 -0
- package/dist/src/utils/currency.js.map +1 -0
- package/dist/src/utils/currency.test.d.ts +2 -0
- package/dist/src/utils/currency.test.d.ts.map +1 -0
- package/dist/src/utils/currency.test.js +34 -0
- package/dist/src/utils/currency.test.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +26 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +40 -0
- package/dist/src/utils/numbers.js.map +1 -0
- package/dist/src/utils/numbers.test.d.ts +2 -0
- package/dist/src/utils/numbers.test.d.ts.map +1 -0
- package/dist/src/utils/numbers.test.js +54 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/dist/src/utils/siww.d.ts +4 -0
- package/dist/src/utils/siww.d.ts.map +1 -0
- package/dist/src/utils/siww.js +19 -0
- package/dist/src/utils/siww.js.map +1 -0
- package/dist/src/utils/time.d.ts +5 -0
- package/dist/src/utils/time.d.ts.map +1 -0
- package/dist/src/utils/time.js +5 -0
- package/dist/src/utils/time.js.map +1 -0
- package/package.json +13 -5
- package/src/api/auth.ts +178 -0
- package/src/api/client.ts +78 -0
- package/src/api/fetch-error.ts +8 -0
- package/src/api/index.ts +2 -0
- package/src/api/portal.ts +56 -0
- package/src/assets/DefaultAvatar.tsx +74 -0
- package/src/components/Dropdown/AccountAddress.tsx +130 -0
- package/src/components/Dropdown/AccountAssets.tsx +110 -0
- package/src/components/Dropdown/AccountBalance.tsx +38 -0
- package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
- package/src/components/Dropdown/Content.tsx +148 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
- package/src/components/Dropdown/Dropdown.tsx +111 -0
- package/src/components/Dropdown/README.md +51 -0
- package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
- package/src/components/Dropdown/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/hooks/constants.ts +6 -0
- package/src/hooks/index.ts +10 -0
- package/src/hooks/useAssetsUSDConversion.ts +31 -0
- package/src/hooks/useAuthApiClient.ts +6 -0
- package/src/hooks/useAuthenticateWithWallet.ts +98 -0
- package/src/hooks/useCreateAccount.ts +36 -0
- package/src/hooks/useCreateSession.ts +44 -0
- package/src/hooks/useDropdownData.ts +131 -0
- package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
- package/src/hooks/useGetAccountByAddress.ts +25 -0
- package/src/hooks/useGetAccountByMezoId.ts +25 -0
- package/src/hooks/useGetCurrentAccount.ts +62 -0
- package/src/hooks/useGetSession.ts +22 -0
- package/src/hooks/useLinkAccount.ts +33 -0
- package/src/hooks/usePassportContext.ts +11 -0
- package/src/hooks/usePortalApiClient.ts +6 -0
- package/src/hooks/useSignInWithDiscord.ts +35 -0
- package/src/hooks/useSignInWithWallet.ts +21 -0
- package/src/hooks/useSignOut.ts +35 -0
- package/src/hooks/useSignUpWithWallet.ts +21 -0
- package/src/hooks/useUpdateMezoId.ts +33 -0
- package/src/hooks/useWalletAccount.ts +53 -0
- package/src/index.ts +2 -0
- package/src/provider.ts +50 -0
- package/src/utils/address.test.ts +38 -0
- package/src/utils/address.ts +43 -0
- package/src/utils/cryptoAssets.test.ts +61 -0
- package/src/utils/cryptoAssets.ts +93 -0
- package/src/utils/currency.test.ts +38 -0
- package/src/utils/currency.ts +32 -0
- package/src/utils/numbers.test.ts +73 -0
- package/src/utils/numbers.ts +55 -0
- package/src/utils/siww.ts +31 -0
- package/src/utils/time.ts +4 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Block,
|
|
3
|
+
HeadingSmall,
|
|
4
|
+
LabelSmall,
|
|
5
|
+
Mats,
|
|
6
|
+
useStyletron,
|
|
7
|
+
} from "@mezo-org/mezo-clay"
|
|
8
|
+
import React from "react"
|
|
9
|
+
import DefaultAvatar from "../../assets/DefaultAvatar"
|
|
10
|
+
import { formatHumanReadableNumber } from "../../utils/numbers"
|
|
11
|
+
|
|
12
|
+
const DOT_DELIMITER_REGEX = /(?=\.)/
|
|
13
|
+
|
|
14
|
+
type WelcomeBlockProps = {
|
|
15
|
+
mezoId: string
|
|
16
|
+
matsBalance: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default function WelcomeBlock(props: WelcomeBlockProps) {
|
|
20
|
+
const { mezoId, matsBalance } = props
|
|
21
|
+
|
|
22
|
+
const [name, nameSuffix] = mezoId.split(DOT_DELIMITER_REGEX)
|
|
23
|
+
const [, theme] = useStyletron()
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Block
|
|
27
|
+
display="flex"
|
|
28
|
+
alignItems="center"
|
|
29
|
+
padding={`${theme.sizing.scale600} 0`}
|
|
30
|
+
marginBottom={theme.sizing.scale300}
|
|
31
|
+
overrides={{
|
|
32
|
+
Block: {
|
|
33
|
+
style: {
|
|
34
|
+
gap: theme.sizing.scale600,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
}}
|
|
38
|
+
>
|
|
39
|
+
<Block
|
|
40
|
+
as={DefaultAvatar}
|
|
41
|
+
width={theme.sizing.scale1200}
|
|
42
|
+
height={theme.sizing.scale1200}
|
|
43
|
+
overrides={{
|
|
44
|
+
Block: {
|
|
45
|
+
style: {
|
|
46
|
+
borderRadius: "100%",
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
}}
|
|
50
|
+
/>
|
|
51
|
+
|
|
52
|
+
<Block
|
|
53
|
+
display="flex"
|
|
54
|
+
flexDirection="column"
|
|
55
|
+
overrides={{
|
|
56
|
+
Block: {
|
|
57
|
+
style: {
|
|
58
|
+
gap: theme.sizing.scale100,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
<HeadingSmall as="span">
|
|
64
|
+
<Block as="span">{name}</Block>
|
|
65
|
+
<Block as="span" color={theme.colors.gray500}>
|
|
66
|
+
{nameSuffix}
|
|
67
|
+
</Block>
|
|
68
|
+
</HeadingSmall>
|
|
69
|
+
|
|
70
|
+
<LabelSmall
|
|
71
|
+
display="flex"
|
|
72
|
+
alignItems="center"
|
|
73
|
+
margin={0}
|
|
74
|
+
color={theme.colors.contentTertiary}
|
|
75
|
+
overrides={{
|
|
76
|
+
Block: {
|
|
77
|
+
style: {
|
|
78
|
+
gap: theme.sizing.scale300,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
}}
|
|
82
|
+
>
|
|
83
|
+
<Mats
|
|
84
|
+
color={theme.colors.contentPrimary}
|
|
85
|
+
size={theme.sizing.scale600}
|
|
86
|
+
/>
|
|
87
|
+
{formatHumanReadableNumber(matsBalance, 2)}
|
|
88
|
+
</LabelSmall>
|
|
89
|
+
</Block>
|
|
90
|
+
</Block>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Dropdown, DropdownProps } from "./Dropdown"
|
package/src/hooks/index.ts
CHANGED
|
@@ -6,3 +6,13 @@ export {
|
|
|
6
6
|
useSubscribeToConnectorEvent,
|
|
7
7
|
useSubscribeToWalletNetworkDoesNotMatchProviderChain,
|
|
8
8
|
} from "@mezo-org/orangekit"
|
|
9
|
+
export * from "./useGetAccountByAddress"
|
|
10
|
+
export * from "./useGetAccountByMezoId"
|
|
11
|
+
export * from "./useGetCurrentAccount"
|
|
12
|
+
export * from "./useGetSession"
|
|
13
|
+
export * from "./useLinkAccount"
|
|
14
|
+
export * from "./useSignInWithDiscord"
|
|
15
|
+
export * from "./useSignInWithWallet"
|
|
16
|
+
export * from "./useSignOut"
|
|
17
|
+
export * from "./useSignUpWithWallet"
|
|
18
|
+
export * from "./useUpdateMezoId"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { UseBaseQueryOptions, useQuery } from "@tanstack/react-query"
|
|
2
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { usePortalApiClient } from "./usePortalApiClient"
|
|
5
|
+
|
|
6
|
+
type AssetsUsdConversion = {
|
|
7
|
+
btcUsd: string
|
|
8
|
+
ethUsd: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useAssetsUsdConversion(
|
|
12
|
+
useQueryOptions: Partial<UseBaseQueryOptions<AssetsUsdConversion>> = {},
|
|
13
|
+
) {
|
|
14
|
+
const portalApiClient = usePortalApiClient()
|
|
15
|
+
|
|
16
|
+
return useQuery({
|
|
17
|
+
queryKey: [QUERY_KEYS.ASSETS_USD_CONVERSION],
|
|
18
|
+
queryFn: async () => {
|
|
19
|
+
const { currentUsdPerBtc, currentUsdPerEth } =
|
|
20
|
+
await portalApiClient.getPortalStatistics()
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
btcUsd: currentUsdPerBtc,
|
|
24
|
+
ethUsd: currentUsdPerEth,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
staleTime: 30 * ONE_MINUTE_MS,
|
|
28
|
+
retry: 1,
|
|
29
|
+
...useQueryOptions,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { useSignMessage } from "wagmi"
|
|
2
|
+
import { MutationOptions, useMutation } from "@tanstack/react-query"
|
|
3
|
+
import { useCreateAccount } from "./useCreateAccount"
|
|
4
|
+
import { useCreateSession } from "./useCreateSession"
|
|
5
|
+
import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
|
|
6
|
+
import { createSignInWithWalletMessage } from "../utils/siww"
|
|
7
|
+
import { useGetAccountByAddress } from "./useGetAccountByAddress"
|
|
8
|
+
import type { Session } from "../api"
|
|
9
|
+
import useWalletAccount from "./useWalletAccount"
|
|
10
|
+
|
|
11
|
+
function useAuthenticateWithWallet(
|
|
12
|
+
shouldCreateAccount = false,
|
|
13
|
+
mutationOptions: Omit<
|
|
14
|
+
MutationOptions<Session>,
|
|
15
|
+
"mutationFn" | "mutationKey"
|
|
16
|
+
> = {},
|
|
17
|
+
) {
|
|
18
|
+
const { address, chainId, connector, networkFamily } = useWalletAccount()
|
|
19
|
+
const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
|
|
20
|
+
const { createSessionAsync } = useCreateSession()
|
|
21
|
+
const { createAccountAsync } = useCreateAccount()
|
|
22
|
+
const { signMessageAsync } = useSignMessage()
|
|
23
|
+
|
|
24
|
+
const { refetch: getAccountByAddress } = useGetAccountByAddress(address, {
|
|
25
|
+
enabled: false,
|
|
26
|
+
retry: false,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
30
|
+
mutationFn: async () => {
|
|
31
|
+
if (!address) {
|
|
32
|
+
throw new Error("Sign in error: Wallet not connected!")
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const nonce = await ensureNoSessionAndFetchNonce()
|
|
36
|
+
|
|
37
|
+
const messageResult = createSignInWithWalletMessage(
|
|
38
|
+
address,
|
|
39
|
+
nonce,
|
|
40
|
+
networkFamily,
|
|
41
|
+
chainId,
|
|
42
|
+
)
|
|
43
|
+
const signatureResult = await signMessageAsync({
|
|
44
|
+
message: messageResult,
|
|
45
|
+
connector,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
if (shouldCreateAccount) {
|
|
49
|
+
const getAccountByAddressResult = await getAccountByAddress()
|
|
50
|
+
|
|
51
|
+
if (getAccountByAddressResult.data) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
"Wallet authentication failed: Account creation failed. Account already exists.",
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (getAccountByAddressResult.error) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`Wallet authentication failed: Account creation failed. Could not verify if account already exists: ${getAccountByAddressResult.error}`,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Fetch was successful, but no account was found
|
|
64
|
+
try {
|
|
65
|
+
const createAccountResult = await createAccountAsync({
|
|
66
|
+
type: "wallet",
|
|
67
|
+
message: messageResult,
|
|
68
|
+
signature: signatureResult,
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
if (!createAccountResult) {
|
|
72
|
+
throw new Error("Failed to create new account")
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Wallet authentication failed: Account creation failed: ${error}`,
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return (await createSessionAsync({
|
|
82
|
+
type: "wallet",
|
|
83
|
+
message: messageResult,
|
|
84
|
+
signature: signatureResult,
|
|
85
|
+
})) as Session
|
|
86
|
+
// ^ By passing type: "wallet" we know for sure the return type is Session
|
|
87
|
+
},
|
|
88
|
+
...mutationOptions,
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
authenticateWithWallet: mutate,
|
|
93
|
+
authenticateWithWalletAsync: mutateAsync,
|
|
94
|
+
...signInMutationRestParameters,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { useAuthenticateWithWallet }
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useQueryClient,
|
|
3
|
+
useMutation,
|
|
4
|
+
MutationOptions,
|
|
5
|
+
DefaultError,
|
|
6
|
+
} from "@tanstack/react-query"
|
|
7
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
8
|
+
import { QUERY_KEYS } from "./constants"
|
|
9
|
+
import type { CreateAccountRequest, CreateAccountResponse } from "../api"
|
|
10
|
+
|
|
11
|
+
export function useCreateAccount(
|
|
12
|
+
mutationOptions: Omit<
|
|
13
|
+
MutationOptions<CreateAccountResponse, DefaultError, CreateAccountRequest>,
|
|
14
|
+
"mutationFn" | "mutationKey"
|
|
15
|
+
> = {},
|
|
16
|
+
) {
|
|
17
|
+
const queryClient = useQueryClient()
|
|
18
|
+
const authApiClient = useAuthApiClient()
|
|
19
|
+
|
|
20
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
|
+
|
|
22
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
23
|
+
mutationFn: (createAccountRequest: CreateAccountRequest) =>
|
|
24
|
+
authApiClient.createAccount(createAccountRequest),
|
|
25
|
+
onSuccess: (data, variables, context) => {
|
|
26
|
+
queryClient.resetQueries({
|
|
27
|
+
queryKey: [QUERY_KEYS.ACCOUNT],
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
31
|
+
},
|
|
32
|
+
...restMutationOptions,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return { createAccount: mutate, createAccountAsync: mutateAsync, ...rest }
|
|
36
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useQueryClient,
|
|
3
|
+
useMutation,
|
|
4
|
+
MutationOptions,
|
|
5
|
+
DefaultError,
|
|
6
|
+
} from "@tanstack/react-query"
|
|
7
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
8
|
+
import type { CreateSessionRequest, CreateSessionResponse } from "../api"
|
|
9
|
+
import { QUERY_KEYS } from "./constants"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* This hook is not exposed for external consumers. For creating session they
|
|
13
|
+
* should use `useSignIn` hook related to the authentication method.
|
|
14
|
+
*/
|
|
15
|
+
export function useCreateSession(
|
|
16
|
+
mutationOptions: Omit<
|
|
17
|
+
MutationOptions<CreateSessionResponse, DefaultError, CreateSessionRequest>,
|
|
18
|
+
"mutationFn" | "mutationKey"
|
|
19
|
+
> = {},
|
|
20
|
+
) {
|
|
21
|
+
const queryClient = useQueryClient()
|
|
22
|
+
const authApiClient = useAuthApiClient()
|
|
23
|
+
|
|
24
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
25
|
+
|
|
26
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
27
|
+
mutationFn: (createSessionRequest: CreateSessionRequest) =>
|
|
28
|
+
authApiClient.createSession(createSessionRequest),
|
|
29
|
+
onSuccess: (data, variables, context) => {
|
|
30
|
+
// Since `getSession` query also has code in its query keys we can't set
|
|
31
|
+
// query data for a specific query, because we don't know if the code was
|
|
32
|
+
// used or not. Because of that we just reset all getSession queries
|
|
33
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
|
|
34
|
+
queryClient.resetQueries({
|
|
35
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
39
|
+
},
|
|
40
|
+
...restMutationOptions,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
return { createSession: mutate, createSessionAsync: mutateAsync, ...rest }
|
|
44
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useBitcoinAccount } from "@mezo-org/orangekit"
|
|
2
|
+
import { useGetCurrentAccount } from "."
|
|
3
|
+
import {
|
|
4
|
+
CryptoAssetKey,
|
|
5
|
+
getCryptoAsset,
|
|
6
|
+
isBitcoinLikeCryptoAsset,
|
|
7
|
+
isUsdLikeCryptoAsset,
|
|
8
|
+
} from "../utils/cryptoAssets"
|
|
9
|
+
import { fromFixedPoint } from "../utils/numbers"
|
|
10
|
+
import { useAssetsUsdConversion } from "./useAssetsUSDConversion"
|
|
11
|
+
import useWalletAccount from "./useWalletAccount"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Formats crypto asset to balance details (balance in USD and decimals)
|
|
15
|
+
* @param type The type of crypto asset
|
|
16
|
+
* @param balance The balance of crypto asset
|
|
17
|
+
* @param btcUsdConversionRate The USD conversion rate
|
|
18
|
+
* @returns The balance details
|
|
19
|
+
*/
|
|
20
|
+
function formatCryptoAsset(
|
|
21
|
+
type: CryptoAssetKey,
|
|
22
|
+
balance: bigint,
|
|
23
|
+
btcUsdConversionRate: number,
|
|
24
|
+
) {
|
|
25
|
+
const { decimals } = getCryptoAsset(type)
|
|
26
|
+
|
|
27
|
+
const nativeBalance = fromFixedPoint(balance, decimals, 4)
|
|
28
|
+
let balanceInUsd = 0
|
|
29
|
+
if (isBitcoinLikeCryptoAsset(type)) {
|
|
30
|
+
balanceInUsd = nativeBalance * btcUsdConversionRate
|
|
31
|
+
}
|
|
32
|
+
if (isUsdLikeCryptoAsset(type)) {
|
|
33
|
+
balanceInUsd = nativeBalance
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
type,
|
|
38
|
+
balance: nativeBalance,
|
|
39
|
+
balanceInUsd,
|
|
40
|
+
decimals,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type DropdownCryptoAsset<T = CryptoAssetKey> = {
|
|
45
|
+
type: T
|
|
46
|
+
balance: bigint
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type DropdownDetailedCryptoAsset = ReturnType<typeof formatCryptoAsset>
|
|
50
|
+
|
|
51
|
+
export type DropdownData = {
|
|
52
|
+
mezoId: string
|
|
53
|
+
address: string
|
|
54
|
+
walletType: "bitcoin" | "evm"
|
|
55
|
+
totalBalanceInUsd: number
|
|
56
|
+
formattedNativeAssets: DropdownDetailedCryptoAsset[]
|
|
57
|
+
matsnet: {
|
|
58
|
+
mats: number
|
|
59
|
+
formattedAssets: DropdownDetailedCryptoAsset[]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type UseDropdownDataOptions = {
|
|
64
|
+
// This is workaround. It should be read from contracts or fetched from API.
|
|
65
|
+
evmNativeAssets?: DropdownCryptoAsset[]
|
|
66
|
+
matsnetAssets?: DropdownCryptoAsset[]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Aggregates dropdown data for Dropdown component.
|
|
71
|
+
* @param options - Options for the dropdown data.
|
|
72
|
+
* @param options.evmNativeAssets - Native assets for EVM wallet.
|
|
73
|
+
* @param options.matsnetAssets - Matsnet tokens. Rendered as separate section.
|
|
74
|
+
* @returns Dropdown data.
|
|
75
|
+
* @dev This hook is for internal use only.
|
|
76
|
+
*/
|
|
77
|
+
export default function useDropdownData(
|
|
78
|
+
options?: UseDropdownDataOptions,
|
|
79
|
+
): DropdownData | null {
|
|
80
|
+
const { evmNativeAssets = [], matsnetAssets = [] } = options || {}
|
|
81
|
+
|
|
82
|
+
const { btcBalance } = useBitcoinAccount()
|
|
83
|
+
const { address, networkFamily, isConnected } = useWalletAccount()
|
|
84
|
+
const { data: passportAccount } = useGetCurrentAccount()
|
|
85
|
+
const { data: assetsUsdConversion } = useAssetsUsdConversion()
|
|
86
|
+
|
|
87
|
+
if (!address || !isConnected || !passportAccount) {
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const mezoId = passportAccount.mezoId!
|
|
92
|
+
|
|
93
|
+
const nativeAssets: DropdownCryptoAsset[] =
|
|
94
|
+
networkFamily === "bitcoin"
|
|
95
|
+
? [
|
|
96
|
+
{
|
|
97
|
+
type: "btc",
|
|
98
|
+
balance: BigInt(btcBalance?.total ?? 0),
|
|
99
|
+
},
|
|
100
|
+
]
|
|
101
|
+
: evmNativeAssets
|
|
102
|
+
|
|
103
|
+
const btcUsdConversionRate = Number(assetsUsdConversion?.btcUsd ?? 0)
|
|
104
|
+
|
|
105
|
+
const formattedNativeAssets = nativeAssets.map((asset) =>
|
|
106
|
+
formatCryptoAsset(asset.type, asset.balance, btcUsdConversionRate),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
const totalBalanceInUsd = formattedNativeAssets.reduce(
|
|
110
|
+
(sum, value) => sum + value.balanceInUsd,
|
|
111
|
+
0,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
const matsBalance = passportAccount.mats.totalMats
|
|
115
|
+
|
|
116
|
+
const matsnetFormattedAssets = matsnetAssets.map((asset) =>
|
|
117
|
+
formatCryptoAsset(asset.type, asset.balance, btcUsdConversionRate),
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
mezoId,
|
|
122
|
+
address,
|
|
123
|
+
walletType: networkFamily,
|
|
124
|
+
totalBalanceInUsd,
|
|
125
|
+
formattedNativeAssets,
|
|
126
|
+
matsnet: {
|
|
127
|
+
mats: matsBalance,
|
|
128
|
+
formattedAssets: matsnetFormattedAssets,
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useCallback } from "react"
|
|
2
|
+
import { useGetSession } from "./useGetSession"
|
|
3
|
+
import { useSignOut } from "./useSignOut"
|
|
4
|
+
|
|
5
|
+
function useEnsureNoSessionAndFetchNonce() {
|
|
6
|
+
const { refetch: getSession } = useGetSession(undefined, {
|
|
7
|
+
enabled: false,
|
|
8
|
+
retry: false,
|
|
9
|
+
})
|
|
10
|
+
const { signOutAsync } = useSignOut()
|
|
11
|
+
|
|
12
|
+
const getAndValidateSession = useCallback(async () => {
|
|
13
|
+
const getSessionResult = await getSession()
|
|
14
|
+
|
|
15
|
+
// TODO: We should create a separate endpoint that will always return nonce
|
|
16
|
+
if (getSessionResult.error) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`Sign in error: Failed to fetch the current session: ${getSessionResult.error}`,
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
if (!getSessionResult.data) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
"Sign in error: Failed to fetch the current session; no data returned.",
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return getSessionResult.data
|
|
28
|
+
}, [getSession])
|
|
29
|
+
|
|
30
|
+
const ensureNoSessionAndFetchNonce = useCallback(async () => {
|
|
31
|
+
let getSessionResult = await getAndValidateSession()
|
|
32
|
+
|
|
33
|
+
if (!("nonce" in getSessionResult)) {
|
|
34
|
+
// If there is active session we are terminating it
|
|
35
|
+
await signOutAsync()
|
|
36
|
+
// Because we've signed out we can assume nonce will be returned here
|
|
37
|
+
getSessionResult = (await getAndValidateSession()) as { nonce: string }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return getSessionResult.nonce
|
|
41
|
+
}, [getAndValidateSession, signOutAsync])
|
|
42
|
+
|
|
43
|
+
return { ensureNoSessionAndFetchNonce }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { useEnsureNoSessionAndFetchNonce }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import type { GetAccountByMezoIdOrAddressResponse } from "../api"
|
|
6
|
+
|
|
7
|
+
export function useGetAccountByAddress(
|
|
8
|
+
address?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse | null>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
14
|
+
const authApiClient = useAuthApiClient()
|
|
15
|
+
|
|
16
|
+
return useQuery({
|
|
17
|
+
queryKey: [QUERY_KEYS.ACCOUNT, address],
|
|
18
|
+
queryFn: address
|
|
19
|
+
? () => authApiClient.getAccountByMezoIdOrAddress(address)
|
|
20
|
+
: skipToken,
|
|
21
|
+
staleTime: ONE_MINUTE_MS,
|
|
22
|
+
retry: 1,
|
|
23
|
+
...queryOptions,
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import type { GetAccountByMezoIdOrAddressResponse } from "../api"
|
|
6
|
+
|
|
7
|
+
export function useGetAccountByMezoId(
|
|
8
|
+
mezoId?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse | null>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
14
|
+
const authApiClient = useAuthApiClient()
|
|
15
|
+
|
|
16
|
+
return useQuery({
|
|
17
|
+
queryKey: [QUERY_KEYS.ACCOUNT, mezoId],
|
|
18
|
+
queryFn: mezoId
|
|
19
|
+
? () => authApiClient.getAccountByMezoIdOrAddress(mezoId)
|
|
20
|
+
: skipToken,
|
|
21
|
+
staleTime: ONE_MINUTE_MS,
|
|
22
|
+
retry: 1,
|
|
23
|
+
...queryOptions,
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { UseBaseQueryOptions, useQuery } from "@tanstack/react-query"
|
|
2
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
5
|
+
import { usePortalApiClient } from "./usePortalApiClient"
|
|
6
|
+
import type { GetCurrentAccountResponse } from "../api"
|
|
7
|
+
|
|
8
|
+
type GetCurrentAccountResponseWithMats = GetCurrentAccountResponse & {
|
|
9
|
+
mats: {
|
|
10
|
+
totalMats: number
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function useGetCurrentAccount(
|
|
15
|
+
queryOptions: Omit<
|
|
16
|
+
UseBaseQueryOptions<GetCurrentAccountResponseWithMats>,
|
|
17
|
+
"queryKey" | "queryFn"
|
|
18
|
+
> = {},
|
|
19
|
+
) {
|
|
20
|
+
const authApiClient = useAuthApiClient()
|
|
21
|
+
const portalApiClient = usePortalApiClient()
|
|
22
|
+
|
|
23
|
+
return useQuery({
|
|
24
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
25
|
+
queryFn: async () => {
|
|
26
|
+
const currentAccount = await authApiClient.getCurrentAccount()
|
|
27
|
+
const linkedWallets = currentAccount?.linkedAccounts?.filter(
|
|
28
|
+
(account) => account.type === "wallet",
|
|
29
|
+
)
|
|
30
|
+
if (!linkedWallets || linkedWallets.length === 0) {
|
|
31
|
+
return {
|
|
32
|
+
...currentAccount,
|
|
33
|
+
mats: {
|
|
34
|
+
totalMats: 0,
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const addresses = linkedWallets.map(
|
|
40
|
+
(account) => account.btcAddress || account.evmAddress,
|
|
41
|
+
)
|
|
42
|
+
const mats = await Promise.all(
|
|
43
|
+
addresses.map((address) => portalApiClient.getPortalMats(address)),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
const totalMats = mats.reduce(
|
|
47
|
+
(sumMats, currentMats) => sumMats + currentMats.totalMats,
|
|
48
|
+
0,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
...currentAccount,
|
|
53
|
+
mats: {
|
|
54
|
+
totalMats,
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
staleTime: ONE_MINUTE_MS,
|
|
59
|
+
retry: 1,
|
|
60
|
+
...queryOptions,
|
|
61
|
+
})
|
|
62
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useQuery, UseBaseQueryOptions } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import type { GetSessionResponse } from "../api"
|
|
6
|
+
|
|
7
|
+
export function useGetSession(
|
|
8
|
+
code?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetSessionResponse>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
14
|
+
const authApiClient = useAuthApiClient()
|
|
15
|
+
return useQuery({
|
|
16
|
+
queryKey: [QUERY_KEYS.SESSION, code],
|
|
17
|
+
queryFn: () => authApiClient.getSession(code),
|
|
18
|
+
staleTime: ONE_MINUTE_MS,
|
|
19
|
+
retry: 1,
|
|
20
|
+
...queryOptions,
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useQueryClient,
|
|
3
|
+
useMutation,
|
|
4
|
+
MutationOptions,
|
|
5
|
+
DefaultError,
|
|
6
|
+
} from "@tanstack/react-query"
|
|
7
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
8
|
+
import type { LinkAccountRequest, LinkAccountResponse } from "../api"
|
|
9
|
+
import { QUERY_KEYS } from "./constants"
|
|
10
|
+
|
|
11
|
+
export function useLinkAccount(
|
|
12
|
+
mutationOptions: Omit<
|
|
13
|
+
MutationOptions<LinkAccountResponse, DefaultError, LinkAccountRequest>,
|
|
14
|
+
"mutationFn" | "mutationKey"
|
|
15
|
+
> = {},
|
|
16
|
+
) {
|
|
17
|
+
const queryClient = useQueryClient()
|
|
18
|
+
const authApiClient = useAuthApiClient()
|
|
19
|
+
|
|
20
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
|
+
|
|
22
|
+
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
23
|
+
mutationFn: (linkAccountRequest: LinkAccountRequest) =>
|
|
24
|
+
authApiClient.linkAccount(linkAccountRequest),
|
|
25
|
+
onSuccess: (data, variables, context) => {
|
|
26
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
|
|
27
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
28
|
+
},
|
|
29
|
+
...restMutationOptions,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
return { linkAccount: mutate, linkAccountAsync: mutateAsync, ...rest }
|
|
33
|
+
}
|