/
hooks.ts
116 lines (107 loc) · 2.87 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { useDisconnect } from 'wagmi';
import { queryClient } from './masa-query-client';
import { useAsyncFn } from 'react-use';
import { DependencyList } from 'react';
import {
getIdentityQueryKey,
getSessionDataQueryKey,
getSessionQueryKey,
getWalletQueryKey,
} from './modules';
import { Masa } from '@masa-finance/masa-sdk';
import { Signer } from 'ethers';
const QUERIES = [
'identity',
'wallet',
'session',
'sessionData',
// 'credit-scores',
// 'green',
// 'soulnames',
];
export type QueryKeyRetrievalInput = {
masa?: Masa;
signer?: Signer;
walletAddress?: string;
};
export const getQueryKeys = () => ({
identity: ({ masa, signer, walletAddress }: QueryKeyRetrievalInput) =>
getIdentityQueryKey({ masa, signer, walletAddress }),
session: ({ masa, signer, walletAddress }: QueryKeyRetrievalInput) =>
getSessionQueryKey({ masa, signer, walletAddress }),
sessionData: ({ masa, signer, walletAddress }: QueryKeyRetrievalInput) =>
getSessionDataQueryKey({ masa, signer, walletAddress }),
wallet: ({ masa, signer, walletAddress }: QueryKeyRetrievalInput) =>
getWalletQueryKey({ masa, signer, walletAddress }),
});
export const invalidateAllQueries = async ({
masa,
signer,
walletAddress,
}: QueryKeyRetrievalInput) => {
console.log('invalidate all queries start');
const res = await Promise.all(
QUERIES.map((query: string) =>
queryClient.getQueriesData(
getQueryKeys()[query]({
masa,
signer,
walletAddress,
})
)
)
);
console.log(
'res inv queries',
res.map((r) => r[0])
);
const resTwo = await Promise.all(
QUERIES.map(async (query: string) =>
queryClient.invalidateQueries(
getQueryKeys()[query]({
masa,
signer,
walletAddress,
})
)
)
);
console.log('res after invalidation', resTwo);
};
export const useLogout = (
options: {
masa?: Masa;
signer?: Signer;
walletAddress?: string;
onLogoutStart?: () => void;
onLogoutFinish?: () => void;
},
deps?: DependencyList
) => {
const { onLogoutStart, onLogoutFinish, masa, walletAddress, signer } =
options;
const { disconnectAsync } = useDisconnect();
const [{ value: hasLoggedOut, loading: isLoggingOut, error }, logout] =
useAsyncFn(async () => {
try {
onLogoutStart?.();
await masa?.session.sessionLogout();
console.log('before query');
await invalidateAllQueries({ masa, signer, walletAddress });
console.log('after query');
await disconnectAsync();
return true;
} catch (error: unknown) {
console.error(error);
return false;
} finally {
onLogoutFinish?.();
}
}, [disconnectAsync, onLogoutStart, onLogoutFinish, masa, ...(deps ?? [])]);
return {
hasLoggedOut,
isLoggingOut,
logout,
error,
};
};