Skip to content

Commit

Permalink
feat: move session listener to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
simodrws committed Aug 11, 2023
1 parent 2e9fd31 commit c99d215
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
50 changes: 27 additions & 23 deletions src/refactor/masa/use-session.ts
@@ -1,15 +1,18 @@
import { useAsync, useAsyncFn } from 'react-use';
import {
// useAsync,
useAsyncFn,
} from 'react-use';
import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import { useMasaClient } from '../masa-client/use-masa-client';
import { useMasaQueryClient } from '../masa-client/use-masa-query-client';
// import { useMasaQueryClient } from '../masa-client/use-masa-query-client';
import { useWallet } from '../wallet-client/wallet/use-wallet';
import { useSessionConnect } from './use-session-connect';
import { MasaQueryClientContext } from '../masa-client/masa-query-client-context';

export const useSession = () => {
const { address } = useWallet();
const queryClient = useMasaQueryClient();
// const queryClient = useMasaQueryClient();
const { sdk: masa, masaAddress } = useMasaClient();
const { isLoggingIn, isLoggingOut, loginSessionAsync, logoutSession } =
useSessionConnect();
Expand Down Expand Up @@ -73,26 +76,27 @@ export const useSession = () => {
return undefined;
}, [session, address, masaAddress]);

useAsync(async () => {
if (!!sessionAddress && sessionAddress !== masaAddress && hasSession) {
await Promise.all([
queryClient.setQueryData(
['session-new-check', { masaAddress, persist: true }],
false
),
]);

await logoutSession();
await checkLogin();
}
}, [
queryClient,
masaAddress,
sessionAddress,
hasSession,
logoutSession,
checkLogin,
]);
// // session-listens
// useAsync(async () => {
// if (!!sessionAddress && sessionAddress !== masaAddress && hasSession) {
// await Promise.all([
// queryClient.setQueryData(
// ['session-new-check', { masaAddress, persist: true }],
// false
// ),
// ]);

// await logoutSession();
// await checkLogin();
// }
// }, [
// queryClient,
// masaAddress,
// sessionAddress,
// hasSession,
// logoutSession,
// checkLogin,
// ]);

const isLoadingSession = useMemo(
() => isFetchingSession || isCheckingLogin || isLoggingIn || isLoggingOut,
Expand Down
42 changes: 38 additions & 4 deletions src/refactor/provider/masa-client-provider.tsx
@@ -1,11 +1,14 @@
import React, { ReactNode, createContext, useContext, useMemo } from 'react';
import { useAsync } from 'react-use';
import { useMasaClient } from '../masa-client/use-masa-client';

import { useIdentity } from '../masa/use-identity';
import { useSession } from '../masa/use-session';
import { useCreditScores } from '../masa/use-credit-scores';
import { useSoulNames } from '../masa/use-soulnames';
import { useGreen } from '../masa/use-green';
import { useIdentityListen } from '../masa';
import { useMasaQueryClient } from '../masa-client/use-masa-query-client';

export interface MasaClientProviderValue {
masa?: ReturnType<typeof useMasaClient>['sdk'];
Expand All @@ -19,15 +22,46 @@ export interface MasaClientProviderValue {
export const MasaClientContext = createContext({} as MasaClientProviderValue);

export const MasaClientProvider = ({ children }: { children: ReactNode }) => {
const { masa } = useMasaClient();
const { session, loginSession, logoutSession, sessionAddress } = useSession();
const { identity } = useIdentity();
const { masa, masaAddress } = useMasaClient();
const queryClient = useMasaQueryClient();
const {
session,
hasSession,
checkLogin,
loginSession,
logoutSession,
sessionAddress,
} = useSession();
const { identity, getIdentity } = useIdentity();
const { creditScores } = useCreditScores();
const { soulnames } = useSoulNames();
const { greens } = useGreen();

useIdentityListen({ identity, getIdentity, sessionAddress });
// useSessionListen();

// session-listen
useAsync(async () => {
if (!!sessionAddress && sessionAddress !== masaAddress && hasSession) {
await Promise.all([
queryClient.setQueryData(
['session-new-check', { masaAddress, persist: true }],
false
),
]);

await logoutSession();
await checkLogin();
}
}, [
queryClient,
masaAddress,
sessionAddress,
hasSession,
logoutSession,
checkLogin,
]);

const masaClientProviderValue: MasaClientProviderValue = useMemo(
() =>
({
Expand All @@ -40,7 +74,7 @@ export const MasaClientProvider = ({ children }: { children: ReactNode }) => {
soulnames,
creditScores,
greens,
} as MasaClientProviderValue),
}) as MasaClientProviderValue,
[
masa,
identity,
Expand Down

0 comments on commit c99d215

Please sign in to comment.