Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
314 additions
and
29 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
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,61 @@ | ||
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'; | ||
import { Query, QueryClient, QueryKey } from '@tanstack/react-query'; | ||
import type { | ||
PersistedClient, | ||
Persister, | ||
} from '@tanstack/react-query-persist-client'; | ||
import { persistQueryClient } from '@tanstack/react-query-persist-client'; | ||
import { noopStorage } from '@wagmi/core'; | ||
import { createStorage } from 'wagmi'; | ||
|
||
export const createQueryClient = () => { | ||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
cacheTime: 1000 * 60 * 60 * 24, // 24 hours | ||
networkMode: 'offlineFirst', | ||
refetchOnWindowFocus: false, | ||
retry: 0, | ||
}, | ||
mutations: { | ||
networkMode: 'offlineFirst', | ||
}, | ||
}, | ||
}); | ||
|
||
const storage = createStorage({ | ||
storage: | ||
typeof window !== 'undefined' && window.localStorage | ||
? window.localStorage | ||
: noopStorage, | ||
}); | ||
let persister: Persister | undefined; | ||
|
||
if (typeof window !== 'undefined') { | ||
persister = createSyncStoragePersister({ | ||
key: 'masa-cache', | ||
storage, | ||
// Serialization is handled in `storage`. | ||
serialize: (x: unknown) => x as string, | ||
// Deserialization is handled in `storage`. | ||
deserialize: (x: unknown) => x as PersistedClient, | ||
}); | ||
} | ||
|
||
if (persister) | ||
persistQueryClient({ | ||
queryClient, | ||
persister, | ||
dehydrateOptions: { | ||
shouldDehydrateQuery: ( | ||
query: Query<unknown, unknown, unknown, QueryKey> | ||
) => | ||
query.cacheTime !== 0 && | ||
// Note: adding a `persist` flag to a query key will instruct the | ||
// persister whether or not to persist the response of the query. | ||
(query.queryKey[0] as { persist?: boolean }).persist !== false, | ||
}, | ||
}); | ||
|
||
return queryClient; | ||
}; |
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,28 @@ | ||
import { useAsync, useAsyncFn } from 'react-use'; | ||
import { useMasaClient } from '../masa-client/use-masa-client'; | ||
import { useWallet } from '../wallet-client/wallet/use-wallet'; | ||
|
||
export const useIdentity = () => { | ||
const { sdk: masa } = useMasaClient(); | ||
const { address } = useWallet(); | ||
|
||
const [{ value: identity, loading: isLoadingIdentity }, loadIdentity] = | ||
useAsyncFn(async () => { | ||
try { | ||
if (!masa) return undefined; | ||
return await masa.identity.load(address); | ||
} catch (error) { | ||
console.log('IM IN THIS ERROR', { error }); | ||
throw error; | ||
} | ||
}, [address, masa]); | ||
|
||
useAsync(async () => { | ||
await loadIdentity(); | ||
}, [loadIdentity]); | ||
|
||
return { | ||
identity, | ||
isLoadingIdentity, | ||
}; | ||
}; |
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,99 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useAsync, useAsyncFn } from 'react-use'; | ||
import { useWallet } from '../wallet-client/wallet/use-wallet'; | ||
import { useMasaClient } from '../masa-client/use-masa-client'; | ||
|
||
export const useSession = () => { | ||
const { address, isDisconnected } = useWallet(); | ||
const { masa } = useMasaClient(); | ||
|
||
const [{ loading: isLoggingIn }, loginSession] = useAsyncFn(async () => { | ||
if (!masa) return undefined; | ||
if (!address) return undefined; | ||
const loginObj = await masa.session.login(); | ||
|
||
if (!loginObj) { | ||
return undefined; | ||
} | ||
return { | ||
address: loginObj.address, | ||
userId: loginObj.userId, | ||
cookie: loginObj.cookie, | ||
}; | ||
}, [masa, address]); | ||
|
||
const { data: hasSession, refetch: checkLogin } = useQuery({ | ||
queryKey: ['session-check', { address }], | ||
enabled: !!masa && !!address, | ||
|
||
queryFn: async () => { | ||
const hasSesh = await masa?.session.checkLogin(); | ||
if (hasSesh) return hasSesh; | ||
return null; | ||
}, | ||
}); | ||
|
||
const { | ||
data: session, | ||
refetch: getSession, | ||
isPreviousData, | ||
} = useQuery({ | ||
queryKey: ['session', { address, hasSession }], | ||
enabled: !!masa && !!address && hasSession !== undefined, | ||
|
||
queryFn: async () => { | ||
if (hasSession && address !== session?.user?.address) { | ||
await masa?.session.logout(); | ||
} | ||
const sesh = await masa?.session.getSession(); | ||
return sesh | ||
? { | ||
...sesh, | ||
} | ||
: undefined; | ||
}, | ||
}); | ||
|
||
useAsync(async () => { | ||
const { data: refetchSess } = await getSession(); | ||
console.log('DEBUG AM I HERE ?', { | ||
hasSession, | ||
refetchSess, | ||
address, | ||
session, | ||
isPreviousData, | ||
}); | ||
|
||
if (isDisconnected) { | ||
await masa?.session.logout(); | ||
// return; | ||
} | ||
if (session !== address && hasSession) { | ||
// await masa?.session.logout(); | ||
const { data: res } = await getSession(); | ||
} | ||
}, [ | ||
session, | ||
hasSession, | ||
address, | ||
masa, | ||
getSession, | ||
isDisconnected, | ||
isPreviousData, | ||
]); | ||
|
||
const [{ loading: isLoggingOut }, logoutSession] = useAsyncFn(async () => { | ||
await masa?.session.logout(); | ||
}, [masa]); | ||
return { | ||
// checkLogin, | ||
session, | ||
hasSession, | ||
loginSession, | ||
logoutSession, | ||
isLoggingIn, | ||
isLoggingOut, | ||
getSession, | ||
checkLogin, | ||
}; | ||
}; |
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.