Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update queries, add logout function, full flow for wagmi
- Loading branch information
Showing
12 changed files
with
471 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.