Skip to content

Commit

Permalink
fix: strict mode issue (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini authored Oct 21, 2024
1 parent d866b5b commit 7bae639
Showing 1 changed file with 65 additions and 50 deletions.
115 changes: 65 additions & 50 deletions packages/dapp-kit-react/src/DAppKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,24 @@ import React, {
useMemo,
useState,
} from 'react';
import type { WalletSource } from '@vechain/dapp-kit';
import type { DAppKit, WalletSource } from '@vechain/dapp-kit';
import { DAppKitUI } from '@vechain/dapp-kit-ui';
import { subscribeKey } from 'valtio/vanilla/utils';
import type { DAppKitProviderOptions, DAppKitContext } from './types';
import { type Certificate } from '@vechain/sdk-core';
import type { DAppKitProviderOptions, DAppKitContext } from './types';

/**
* Context
*/
const Context = createContext<DAppKitContext | undefined>(undefined);

export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
export const DAppKitDataProvider = ({
children,
nodeUrl,
genesis,
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
connex,
}: {
children: React.ReactNode;
connex: DAppKit;
}): React.ReactElement => {
const connex = useMemo(
() =>
DAppKitUI.configure({
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
[
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificateData,
],
);

const [account, setAccount] = useState<string | null>(
connex.wallet.state.address,
);
Expand Down Expand Up @@ -146,6 +103,64 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
return <Context.Provider value={context}>{children}</Context.Provider>;
};

export const DAppKitProvider = ({
children,
nodeUrl,
genesis,
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}: DAppKitProviderOptions): React.ReactElement | null => {
const [connex, setConnex] = useState<DAppKit | null>(null);
useEffect(() => {
setConnex(
DAppKitUI.configure({
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
);
}, [
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificateData,
]);
if (!connex) {
return null;
}
return (
<DAppKitDataProvider connex={connex}>{children}</DAppKitDataProvider>
);
};

export const useConnex = (): DAppKitContext['connex'] => {
const context = useContext(Context);

Expand Down

0 comments on commit 7bae639

Please sign in to comment.