Skip to content

Commit

Permalink
feat: hook, context, and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardojrmcom committed Sep 9, 2022
1 parent 8de472c commit 878436e
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, {
useEffect,
useState,
useMemo,
useContext,
createContext,
ReactNode,
} from 'react';
import { PhantasmaLink } from './phantasmaLink';

export interface PhantasmaLinkContextType {
dapp?: PhantasmaLink;
dappName: string;
isLoggedIn: boolean;
isLoggedInSet: React.Dispatch<React.SetStateAction<boolean>>;
}

const init: PhantasmaLinkContextType = {
dapp: undefined,
dappName: 'My-Dapp',
isLoggedIn: false,
isLoggedInSet: () => undefined,
};

const PhantasmaLinkContext = createContext<PhantasmaLinkContextType>(init);

export type PhantasmaWalletType = 'poltergeist' | 'ecto';

export interface PhantasmaLinkProviderProps {
children?: ReactNode;
dappName?: string;
}

export const PhantasmaLinkProvider = ({
dappName = 'My-Dapp',
children,
}: PhantasmaLinkProviderProps) => {
const [dapp, dappSet] = useState<PhantasmaLinkContextType['dapp']>(init.dapp);
const [isLoggedIn, isLoggedInSet] = useState<
PhantasmaLinkContextType['isLoggedIn']
>(init.isLoggedIn);

useEffect(() => {
const dappInit = new PhantasmaLink(dappName);
dappSet(dappInit);
}, [dappSet, dappName]);

const ctx = useMemo(
() => ({ dapp, dappName, isLoggedIn, isLoggedInSet }),
[dapp, dappName, isLoggedIn, isLoggedInSet],
);

return (
<PhantasmaLinkContext.Provider value={ctx}>
{children}
</PhantasmaLinkContext.Provider>
);
};

export const usePhantasmaLink = () => useContext(PhantasmaLinkContext);

0 comments on commit 878436e

Please sign in to comment.