React Context Provider
Create the provider
import { createContext, useContext, useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import {
CONTENT_MODE,
WALLET_KEYS,
} from '@make-software/csprclick-core-types';
import type {
AccountType,
CsprClickInitOptions,
ICSPRClickSDK,
} from '@make-software/csprclick-core-types';
import type { ClickUIOptions } from '@make-software/csprclick-core-types/clickui';
declare global {
interface Window {
clickUIOptions: ClickUIOptions;
clickSDKOptions: CsprClickInitOptions;
csprclick?: ICSPRClickSDK;
}
}
window.clickUIOptions = {
uiContainer: 'csprclick-ui',
rootAppElement: '#root',
show1ClickModal: true,
showTopBar: true,
accountMenuItems: [
'AccountCardMenuItem',
'CopyHashMenuItem',
'BuyCSPRMenuItem',
],
defaultTheme: 'light',
};
window.clickSDKOptions = {
appName: 'CSPR.click React template',
appId: 'csprclick-template',
providers: [
WALLET_KEYS.CASPER_WALLET,
WALLET_KEYS.LEDGER,
WALLET_KEYS.METAMASK_SNAP,
],
contentMode: CONTENT_MODE.IFRAME,
};
type ClickContextState = {
publicKey: string | undefined;
provider: string | undefined;
clickRef: ICSPRClickSDK | undefined;
};
type AccountChangedEvent = {
account?: AccountType;
};
const ClickContext = createContext<ClickContextState | undefined>(undefined);
type ClickProviderProps = {
children: ReactNode;
};
export const ClickProvider = ({ children }: ClickProviderProps) => {
const [connectedAccount, setConnectedAccount] = useState<AccountType | undefined>();
const [clickRef, setClickRef] = useState<ICSPRClickSDK | undefined>();
useEffect(() => {
const checkActiveAccount = async (ref: ICSPRClickSDK) => {
try {
const account = await ref.getActiveAccountAsync({
withBalance: true,
withFiatCurrency: 'USD',
});
setConnectedAccount(account?.public_key ? account : undefined);
} catch (error) {
console.error('Failed to get active account', error);
setConnectedAccount(undefined);
}
};
const handleAccountChanged = (event: AccountChangedEvent) => {
const account = event.account;
setConnectedAccount(account?.public_key ? account : undefined);
};
const handleSdkLoaded = () => {
const ref = window.csprclick;
if (!ref) {
return;
}
setClickRef(ref);
ref.on('csprclick:signed_in', handleAccountChanged);
ref.on('csprclick:switched_account', handleAccountChanged);
ref.on('csprclick:unsolicited_account_change', handleAccountChanged);
ref.on('csprclick:signed_out', () => setConnectedAccount(undefined));
checkActiveAccount(ref);
};
window.addEventListener('csprclick:loaded', handleSdkLoaded);
if (window.csprclick) {
handleSdkLoaded();
}
if (!document.querySelector('script#csprclick-client')) {
const script = document.createElement('script');
script.src = 'https://cdn.cspr.click/ui/v2.1.0/csprclick-client-2.1.0.js';
script.id = 'csprclick-client';
script.async = true;
document.head.appendChild(script);
}
return () => {
window.removeEventListener('csprclick:loaded', handleSdkLoaded);
};
}, []);
return (
<ClickContext.Provider
value={{
publicKey: connectedAccount?.public_key,
provider: connectedAccount?.provider,
clickRef,
}}
>
{children}
</ClickContext.Provider>
);
};
export const useClickRef = (): ClickContextState => {
const context = useContext(ClickContext);
if (!context) {
throw new Error('useClickRef must be used within a ClickProvider');
}
return context;
};Add the UI container
Wrap your application
Use the SDK from a component
What's next
Last updated

