Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor masa session, update other entities
- Loading branch information
simodrws
committed
Jun 22, 2023
1 parent
c6d999e
commit 4552260
Showing
7 changed files
with
289 additions
and
404 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,81 @@ | ||
import { useAsyncFn } from 'react-use'; | ||
import type { ISession } from '@masa-finance/masa-sdk'; | ||
import { useWallet } from '../wallet-client/wallet/use-wallet'; | ||
import { useMasaClient } from '../masa-client/use-masa-client'; | ||
import { useMasaQueryClient } from '../masa-client/use-masa-query-client'; | ||
|
||
export const useSessionConnect = () => { | ||
const { address, isDisconnected } = useWallet(); | ||
const { sdk: masa, masaAddress } = useMasaClient(); | ||
const queryClient = useMasaQueryClient(); | ||
|
||
const [{ loading: isLoggingIn }, loginSessionAsync] = useAsyncFn(async () => { | ||
if (!masa) return null; | ||
if (!masaAddress) return null; | ||
if (masaAddress !== address) return null; | ||
if (isDisconnected) return null; | ||
|
||
const loginObj = await masa.session.login(); | ||
|
||
await queryClient.invalidateQueries([ | ||
'session-new-check', | ||
{ masaAddress, persist: true }, | ||
]); | ||
await queryClient.invalidateQueries([ | ||
'session-new', | ||
{ masaAddress, persist: false }, | ||
]); | ||
|
||
if (!loginObj) { | ||
return null; | ||
} | ||
|
||
await queryClient.fetchQuery([ | ||
'session-new-check', | ||
{ persist: true, masaAddress }, | ||
]); | ||
await queryClient.fetchQuery([ | ||
'session-new', | ||
{ persist: false, masaAddress }, | ||
]); | ||
|
||
return loginObj as unknown as ISession & { | ||
userId: string; | ||
address: string; | ||
}; | ||
}, [queryClient, masa, address, masaAddress, isDisconnected]); | ||
|
||
const [{ loading: isLoggingOut }, logoutSession] = useAsyncFn(async () => { | ||
// if (!masa) return null; | ||
// if (!masaAddress) return null; | ||
// if (masaAddress !== address) return null; | ||
// if (isDisconnected) return null; | ||
|
||
const result = await masa?.session.logout(); | ||
await Promise.all([ | ||
queryClient.invalidateQueries([ | ||
'session-new-check', | ||
{ masaAddress, persist: true }, | ||
]), | ||
queryClient.invalidateQueries([ | ||
'session-new', | ||
{ masaAddress, persist: false }, | ||
]), | ||
]); | ||
|
||
queryClient.setQueryData(['session-new-check', { masaAddress }], false); | ||
queryClient.setQueryData( | ||
['session-new', { masaAddress, persist: false }], | ||
null | ||
); | ||
|
||
return result; | ||
}, [masa, queryClient, masaAddress]); | ||
|
||
return { | ||
loginSessionAsync, | ||
logoutSession, | ||
isLoggingIn, | ||
isLoggingOut, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,52 +1,71 @@ | ||
import { useAsync } from 'react-use'; | ||
import { useSession } from './use-session'; | ||
import { useWallet } from '../wallet-client/wallet/use-wallet'; | ||
import { useState } from 'react'; | ||
import { useMasaClient } from '../masa-client/use-masa-client'; | ||
import { useMasaQueryClient } from '../masa-client/use-masa-query-client'; | ||
import type { useSession } from './use-session'; | ||
|
||
export const useSessionListen = () => { | ||
export const useSessionListen = ({ | ||
sessionAddress, | ||
hasSession, | ||
logoutSession, | ||
checkLogin, | ||
}: { | ||
hasSession?: boolean | null; | ||
sessionAddress?: string; | ||
logoutSession: ReturnType<typeof useSession>['logoutSession']; | ||
checkLogin: ReturnType<typeof useSession>['checkLogin']; | ||
}) => { | ||
const [isUpdatingSession, setUpdating] = useState(false); | ||
const { masaAddress } = useMasaClient(); | ||
const { | ||
session, | ||
sessionAddress, | ||
isLoadingSession, | ||
logoutSession, | ||
hasSession, | ||
} = useSession(); | ||
const { isDisconnected } = useWallet(); | ||
const queryClient = useMasaQueryClient(); | ||
|
||
// * useEffect to handle account switches and disconnect | ||
useAsync(async () => { | ||
if (isLoadingSession) return; | ||
|
||
if ( | ||
session && | ||
masaAddress && | ||
masaAddress === session?.user.address && | ||
hasSession | ||
) { | ||
return; | ||
} | ||
if (isUpdatingSession) return; | ||
|
||
if (isDisconnected) { | ||
await logoutSession(); | ||
return; | ||
if (!isUpdatingSession) { | ||
setUpdating(true); | ||
} | ||
|
||
if ( | ||
hasSession && | ||
sessionAddress && | ||
masaAddress && | ||
sessionAddress !== masaAddress | ||
) { | ||
if (!!sessionAddress && sessionAddress !== masaAddress && hasSession) { | ||
await Promise.all([ | ||
// queryClient.setQueryData( | ||
// ['session-new', { masaAddress, persist: false }], | ||
// null | ||
// ), | ||
queryClient.setQueryData( | ||
['session-new-check', { masaAddress, persist: true }], | ||
false | ||
), | ||
]); | ||
|
||
await queryClient.invalidateQueries([ | ||
'session-new-check', | ||
{ masaAddress: sessionAddress, persist: true }, | ||
]); | ||
await queryClient.invalidateQueries([ | ||
'session-new', | ||
{ masaAddress: sessionAddress, persist: false }, | ||
]); | ||
|
||
await logoutSession(); | ||
|
||
await checkLogin(); | ||
} | ||
|
||
setUpdating(false); | ||
}, [ | ||
isLoadingSession, | ||
sessionAddress, | ||
isUpdatingSession, | ||
setUpdating, | ||
queryClient, | ||
masaAddress, | ||
isDisconnected, | ||
logoutSession, | ||
sessionAddress, | ||
hasSession, | ||
session, | ||
logoutSession, | ||
checkLogin, | ||
]); | ||
|
||
return { | ||
isUpdatingSession, | ||
}; | ||
}; |
Oops, something went wrong.