Skip to content

Commit

Permalink
Moved scopes to a module and added fullfilment of scopes boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
hide-on-bush-x committed Feb 28, 2023
1 parent 002bb74 commit 4d93a78
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
53 changes: 41 additions & 12 deletions src/provider/masa-context-provider.tsx
Expand Up @@ -14,6 +14,7 @@ import {
import { Signer, Wallet } from 'ethers';
import { MASA_CONTEXT } from './masa-context';
import { MasaShape } from './masa-shape';
import { useScopes } from './modules/scopes/scopes';

export interface ArweaveConfig {
port?: string;
Expand Down Expand Up @@ -51,8 +52,6 @@ export const MasaContextProvider = ({
}: MasaContextProviderProps): JSX.Element => {
// masa
const [masaInstance, setMasaInstance] = useState<Masa | undefined>();
// scope
const [scope, setScope] = useState<string[]>([]);

// provider
const [provider, setProvider] = useState<Wallet | Signer | undefined>(signer);
Expand All @@ -69,18 +68,11 @@ export const MasaContextProvider = ({
// network
const { switchNetwork, network } = useNetwork(provider);

// modal
const { isModalOpen, setModalOpen, setModalCallback, closeModal } = useModal(
masaInstance,
isLoggedIn,
isConnected,
network
);

// identity
const {
identity,
handlePurchaseIdentity,
handlePurchaseIdentityWithSoulname,
isIdentityLoading,
reloadIdentity,
} = useIdentity(masaInstance, walletAddress);
Expand Down Expand Up @@ -109,6 +101,25 @@ export const MasaContextProvider = ({
reloadGreens,
} = useGreen(masaInstance, walletAddress);

// scope
const { scope, setScope, areScopesFullfiled } = useScopes(soulnames ?? []);

// modal
const {
isModalOpen,
setModalOpen,
setModalCallback,
closeModal,
forcedPage,
setForcedPage,
} = useModal(
masaInstance,
isLoggedIn,
isConnected,
network,
areScopesFullfiled
);

// global loading flag
const isLoading = useMemo(() => {
return (
Expand Down Expand Up @@ -138,7 +149,21 @@ export const MasaContextProvider = ({
setModalCallback(() => options?.callback);
}
},
[setModalOpen, setModalCallback]
[setModalOpen, setModalCallback, setScope]
);

const openMintSoulnameModal = useCallback(
(mintCallback?: () => void) => {
setForcedPage?.('createSoulname');
setModalOpen(true);
const cb = () => {
setForcedPage?.(null);
if (mintCallback) mintCallback();
};

setModalCallback(() => cb);
},
[setForcedPage, setModalOpen, setModalCallback]
);

useEffect(() => {
Expand Down Expand Up @@ -177,6 +202,7 @@ export const MasaContextProvider = ({

// general config
scope,
areScopesFullfiled,
company,

// provider handling
Expand All @@ -187,7 +213,9 @@ export const MasaContextProvider = ({
isModalOpen,
setModalOpen,
closeModal,

forcedPage,
setForcedPage,
openMintSoulnameModal,
// wallet
walletAddress,
isWalletLoading,
Expand All @@ -197,6 +225,7 @@ export const MasaContextProvider = ({
identity,
isIdentityLoading,
handlePurchaseIdentity,
handlePurchaseIdentityWithSoulname,
reloadIdentity,

// session
Expand Down
12 changes: 11 additions & 1 deletion src/provider/masa-shape.ts
Expand Up @@ -5,6 +5,7 @@ import {
IGreen,
Masa,
NetworkName,
PaymentMethod,
SoulNameDetails,
VerifyGreenResult,
} from '@masa-finance/masa-sdk';
Expand All @@ -24,6 +25,7 @@ export interface MasaShape {

// general config
scope?: string[];
areScopesFullfiled?: boolean;
company?: string;

// provider
Expand All @@ -33,7 +35,10 @@ export interface MasaShape {
// modal
isModalOpen?: boolean;
setModalOpen?: (val: boolean) => void;
closeModal?: () => void;
closeModal?: (forceCallback?: boolean) => void;
forcedPage?: string | null;
setForcedPage?: (page: string | null) => void;
openMintSoulnameModal?: (callback?: () => void) => void;

// wallet
walletAddress?: string;
Expand All @@ -47,6 +52,11 @@ export interface MasaShape {
};
isIdentityLoading?: boolean;
handlePurchaseIdentity?: () => void;
handlePurchaseIdentityWithSoulname?: (
soulname: string,
registrationPrice: number,
paymentMethod: PaymentMethod
) => Promise<boolean>;
reloadIdentity?: () => void;

// session
Expand Down
14 changes: 14 additions & 0 deletions src/provider/modules/scopes/scopes.ts
@@ -0,0 +1,14 @@
import { SoulNameDetails } from '@masa-finance/masa-sdk';
import { useMemo, useState } from 'react';

export const useScopes = (soulnames: SoulNameDetails[]) => {
const [scope, setScope] = useState<string[]>([]);
const areScopesFullfiled = useMemo(() => {
if (scope?.includes('soulname') && (soulnames?.length ?? 0) === 0)
return false;

return true;
}, [soulnames, scope]);

return { scope, setScope, areScopesFullfiled };
};

0 comments on commit 4d93a78

Please sign in to comment.