Skip to content

Commit

Permalink
When masa instance is not ready rise loading flag
Browse files Browse the repository at this point in the history
  • Loading branch information
hide-on-bush-x committed Feb 14, 2023
1 parent 7974c4b commit 5b4a0cb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/provider/masa-context-provider.tsx
Expand Up @@ -11,6 +11,7 @@ import {
} from './modules';
import { ethers } from 'ethers';
import { MASA_CONTEXT, MasaShape } from './masa-context';
import { queryClient } from './masa-query-client';

export interface ArweaveConfig {
port?: string;
Expand Down Expand Up @@ -97,6 +98,7 @@ export const MasaContextProvider = ({

const loading = useMemo(() => {
return (
!masaInstance ||
sessionLoading ||
creditScoreLoading ||
identityLoading ||
Expand All @@ -109,8 +111,10 @@ export const MasaContextProvider = ({
identityLoading,
walletLoading,
greenLoading,
masaInstance
]);


useEffect(() => {
if (externalSigner) {
setProvider(externalSigner);
Expand Down Expand Up @@ -146,8 +150,9 @@ export const MasaContextProvider = ({

useEffect(() => {
const loadMasa = async (): Promise<void> => {
if (!provider) return;
const masa: Masa | null = await createNewMasa({
newWallet: noWallet ? null : provider,
newWallet: provider,
environmentName,
arweaveConfig,
verbose,
Expand All @@ -157,6 +162,8 @@ export const MasaContextProvider = ({
};

void loadMasa();

queryClient.invalidateQueries('green');
}, [
provider,
noWallet,
Expand Down
37 changes: 22 additions & 15 deletions src/provider/modules/green/green.ts
Expand Up @@ -31,29 +31,36 @@ export const useGreen = (
isLoading: boolean;
error: unknown;
} => {
const queryKey: string = useMemo(() => {
return `green-${walletAddress}-${masa?.config.network}`;
const queryKey: any[] = useMemo(() => {
return ['green', walletAddress, masa?.config.network];
}, [walletAddress, masa]);

const {
data: greens,
status,
isLoading,
error,
} = useQuery(queryKey, () => masa?.green.list(), {
enabled: !!masa && !!walletAddress,
onSuccess: (
greens: {
tokenId: BigNumber;
tokenUri: string;
metadata?: IGreen | undefined;
}[]
) => {
if (masa?.config.verbose) {
console.log({ greens, network: masa?.config.network });
}
} = useQuery(
queryKey,
async () => {
const list = await masa?.green.list();
return list
},
});
{
enabled: !!masa && !!walletAddress,
onSuccess: (
greens: {
tokenId: BigNumber;
tokenUri: string;
metadata?: IGreen | undefined;
}[]
) => {
if (masa?.config.verbose) {
console.log({ greens, network: masa?.config.network });
}
},
}
);

const handleCreateGreen = useCallback(
async (
Expand Down
6 changes: 3 additions & 3 deletions src/provider/modules/identity/identity.ts
Expand Up @@ -19,8 +19,8 @@ export const useIdentity = (
isLoading: boolean;
error: unknown;
} => {
const queryKey: string = useMemo(() => {
return `identity-${walletAddress}-${masa?.config.network}`;
const queryKey: any[] = useMemo(() => {
return ['identity', walletAddress, masa?.config.network];
}, [walletAddress, masa]);
const {
data: identity,
Expand All @@ -36,7 +36,7 @@ export const useIdentity = (

const handlePurchaseIdentity = useCallback(async () => {
await masa?.identity.create();
await queryClient.invalidateQueries(`identity-${walletAddress}`);
await queryClient.invalidateQueries(`identity`);
}, [masa, walletAddress]);

return { identity, handlePurchaseIdentity, status, isLoading, error };
Expand Down
4 changes: 2 additions & 2 deletions src/provider/modules/wallet/wallet.ts
Expand Up @@ -13,8 +13,8 @@ export const useWallet = (
error: unknown;
chain?: null | ethers.providers.Network;
} => {
const queryKey: string = useMemo(() => {
return `wallet-${masa?.config.network}`;
const queryKey: any[] = useMemo(() => {
return ['wallet', masa?.config.network];
}, [masa]);

const {
Expand Down

0 comments on commit 5b4a0cb

Please sign in to comment.