Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changing the way state is managed #56

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/sample-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@vechain/hardhat-web3": "^0.1.4",
"@vechain/picasso": "^2.1.1",
"@vechain/react-wallet-kit": "*",
"@vechain/vanilla-wallet-kit": "*",
"@vechain/wallet-connect": "*",
"@vechain/wallet-kit": "*",
"@vechain/web3-providers-connex": "^1.1.2",
Expand Down
Binary file removed apps/sample-react-app/public/images/logo/sync.png
Binary file not shown.
Binary file removed apps/sample-react-app/public/images/logo/sync2.png
Binary file not shown.
Binary file removed apps/sample-react-app/public/images/logo/veWorld.png
Binary file not shown.
Binary file removed apps/sample-react-app/public/images/logo/vechain.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 4 additions & 6 deletions apps/sample-react-app/src/Components/SwitchWalletButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ export const SwitchWalletButton: React.FC<SwitchWalletButtonProps> = ({
}): React.ReactElement => {
const { isOpen, onOpen, onClose } = useDisclosure();

const {
accountState: { address, source },
} = useWallet();
const { account, source } = useWallet();

if (address && source)
if (account && source)
return (
<>
<AccountDetailModal
address={address}
address={account}
isOpen={isOpen}
onClose={onClose}
source={source}
/>
<AddressButton
{...buttonProps}
address={address}
address={account}
onClick={onOpen}
showCopyIcon={false}
/>
Expand Down
17 changes: 6 additions & 11 deletions apps/sample-react-app/src/Components/layout/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type { JSX } from 'react';
import React from 'react';
import { Bars3Icon } from '@heroicons/react/24/solid';
import { useWallet } from '@vechain/react-wallet-kit';
import { VechainLogo } from '../../Logos';
import { AccountDetailBody } from '../AccountDetailBody';
import { SwitchWalletButton } from '../SwitchWalletButton';

Expand Down Expand Up @@ -49,9 +48,7 @@ export const NavBar = (): JSX.Element => {
const DesktopNavBar = (): JSX.Element => {
return (
<>
<Box h="30px">
<VechainLogo />
</Box>
<Box h="30px" />
<NavBarWalletConnect />
</>
);
Expand All @@ -62,9 +59,7 @@ const MobileNavBar = (): JSX.Element => {
return (
<>
<MobileNavBarDrawer isOpen={isOpen} onClose={onClose} />
<Box h="30px">
<VechainLogo />
</Box>
<Box h="30px" />
<IconButton
aria-label="light"
fontSize="20px"
Expand All @@ -84,7 +79,7 @@ const MobileNavBarDrawer = ({
isOpen,
onClose,
}: MobileNavBarDrawerProps): JSX.Element => {
const { accountState, disconnect } = useWallet();
const { account, source, disconnect } = useWallet();

return (
<Drawer isOpen={isOpen} onClose={onClose} placement="right">
Expand All @@ -94,11 +89,11 @@ const MobileNavBarDrawer = ({
<VStack h="full" justifyContent="space-between" w="full">
<VStack alignItems="flex-start" spacing={4} w="full">
<Text>Connected Wallet</Text>
{accountState.address && accountState.source ? (
{account && source ? (
<AccountDetailBody
accountAddress={accountState.address}
accountAddress={account}
disconnectWallet={disconnect}
source={accountState.source}
source={source}
/>
) : (
<SwitchWalletButton />
Expand Down
25 changes: 3 additions & 22 deletions apps/sample-react-app/src/Logos/Logo.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
import type { HTMLChakraProps } from '@chakra-ui/react';
import { Image, useColorModeValue } from '@chakra-ui/react';
import { Image } from '@chakra-ui/react';
import React from 'react';
import { VeWorldLogo as VWLogo } from '@vechain/vanilla-wallet-kit';

type IIMage = HTMLChakraProps<'img'>;

export const VechainLogo: React.FC<IIMage> = ({
...props
}): React.ReactElement => {
const lightModeUrl = `${process.env.PUBLIC_URL}/images/logo/vechain.png`;
const darkModeUrl = `${process.env.PUBLIC_URL}/images/logo/vechain_white.png`;
const logoUrl = useColorModeValue(lightModeUrl, darkModeUrl);
return (
<Image
alt="Vechain logo"
h="full"
objectFit="cover"
src={logoUrl}
{...props}
/>
);
};

export const VeWorldLogo: React.FC<IIMage> = ({ ...props }) => {
const lightModeUrl = `${process.env.PUBLIC_URL}/images/logo/veWorld.png`;
const darkModeUrl = `${process.env.PUBLIC_URL}/images/logo/veworld_black.png`;
const logoUrl = useColorModeValue(lightModeUrl, darkModeUrl);
return (
<Image
alt="VeWorld logo"
h="full"
objectFit="cover"
src={logoUrl}
src={VWLogo}
{...props}
/>
);
Expand Down
8 changes: 3 additions & 5 deletions apps/sample-react-app/src/Screens/components/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useCounter } from '../../Hooks/useCounter';
import { AddressButton } from '../../Components';

export const Counter = (): JSX.Element => {
const { accountState } = useWallet();
const { account } = useWallet();
const { count, increment, status, address, error } = useCounter();
const toast = useToast();

Expand All @@ -40,17 +40,15 @@ export const Counter = (): JSX.Element => {
return (
<Button
disabled={
!accountState.address ||
status === 'pending' ||
status === 'in-wallet'
!account || status === 'pending' || status === 'in-wallet'
}
leftIcon={<Icon as={ArrowUpIcon} />}
onClick={incrementCounter}
>
Increment
</Button>
);
}, [accountState.address, incrementCounter, status]);
}, [account, incrementCounter, status]);

const message = useMemo(() => {
switch (status) {
Expand Down
21 changes: 1 addition & 20 deletions apps/sample-vue-app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,18 @@
<vwk-connect-button-with-modal
mode="DARK"
></vwk-connect-button-with-modal>
<ConnectWalletModal v-show="isModalVisible" @close="closeModal" />
</div>
</ConnexProvider>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { defineComponent } from 'vue';
import ConnexProvider from '@/connex/ConnexProvider.vue';

export default defineComponent({
components: {
ConnexProvider,
},

setup() {
const isModalVisible = ref(false);

const showModal = () => {
isModalVisible.value = true;
};

const closeModal = () => {
isModalVisible.value = false;
};

return {
isModalVisible,
showModal,
closeModal,
};
},
});
</script>

Expand Down
41 changes: 26 additions & 15 deletions apps/sample-vue-app/src/connex/ConnexProvider.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { defineComponent, provide, reactive, readonly, toRefs } from 'vue';
import {
ConnectResponse,
MultiWalletConnex,
WalletSource,
WalletSources,
} from '@vechain/wallet-kit';
import type Connex from '@vechain/connex';
import {
Expand Down Expand Up @@ -46,32 +46,42 @@ const walletConnectOptions: WalletConnectOptions = {
export default defineComponent({
setup() {
const walletState: WalletState = reactive<WalletState>({
wallets: WalletSources,
availableWallets: initWallets(true),
availableWallets: initWallets(!!walletConnectOptions),
source: null,
account: null,
});

const onDisconnected = () => {
walletState.account = null;
walletState.source = null;
};

const connex = new MultiWalletConnex({
nodeUrl: 'https://mainnet.vechain.org/',
onDisconnected,
walletConnectOptions,
});

const onDisconnected = () => {
walletState.source = null;
walletState.account = null;
};

const onSourceChanged = (source: WalletSource | null) => {
walletState.source = source;
};

connex.wallet.onDisconnected(onDisconnected);
connex.wallet.onSourceChanged(onSourceChanged);

configureThorModal(connex);

const updateAccount = (addr: string) => {
const setAccount = (addr: string) => {
walletState.account = addr;
};

const updateSource = (source: WalletSource) => {
const setSource = (source: WalletSource) => {
connex.wallet.setSource(source);
walletState.source = source;
};

const connect = async (): Promise<ConnectResponse> => {
const res = await connex.wallet.connect();
walletState.account = res.account;
return res;
};

const _connex: Connex = {
Expand All @@ -84,11 +94,12 @@ export default defineComponent({
walletState.source = null;
walletState.account = null;
};

const walletActions: WalletActions = {
updateAccount,
updateSource,
setAccount,
setSource,
disconnect,
connect: connex.wallet.connect,
connect,
};

provide(ConnexSymbol, readonly(_connex));
Expand Down
25 changes: 23 additions & 2 deletions apps/sample-vue-app/src/connex/injections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@ import {
} from '@/connex/keys';
import { WalletActions, WalletState } from '@/connex/types';

const NOT_SETUP = new Error('`ConnexProvider` not setup properly');

const defaultState: WalletState = {
account: null,
source: null,
availableWallets: [],
};

const defaultActions: WalletActions = {
setAccount: () => {
throw NOT_SETUP;
},
setSource: () => {
throw NOT_SETUP;
},
connect: () => Promise.reject(NOT_SETUP),
disconnect: () => {
throw NOT_SETUP;
},
};

const injectConnex = (): Connex => inject<Connex>(ConnexSymbol, {} as Connex);

const injectWalletState = (): WalletState =>
inject(WalletStateSymbol, {} as WalletState);
inject(WalletStateSymbol, defaultState);

const injectWalletActions = (): WalletActions =>
inject(WalletActionsSymbol, {} as WalletActions);
inject(WalletActionsSymbol, defaultActions);

export { injectConnex, injectWalletState, injectWalletActions };
8 changes: 3 additions & 5 deletions apps/sample-vue-app/src/connex/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { WalletSource } from '@vechain/wallet-kit';
import { ConnectResponse } from '@vechain/wallet-kit/src/types';
import { ConnectResponse, WalletSource } from '@vechain/wallet-kit';

type WalletState = {
wallets: WalletSource[];
availableWallets: WalletSource[];
source: WalletSource | null;
account: string | null;
};

type WalletActions = {
updateAccount: (account: string) => void;
updateSource: (source: WalletSource) => void;
setAccount: (account: string) => void;
setSource: (source: WalletSource) => void;
connect: () => Promise<ConnectResponse>;
disconnect: () => void;
};
1 change: 1 addition & 0 deletions packages/react-wallet-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@vechain/connex": "2.1.0",
"@vechain/connex-framework": "2.1.0",
"@vechain/picasso": "^2.1.1",
"@vechain/vanilla-wallet-kit": "*",
"@vechain/wallet-connect": "*",
"@vechain/wallet-kit": "*",
"thor-devkit": "^2.0.9"
Expand Down
44 changes: 0 additions & 44 deletions packages/react-wallet-kit/src/AccountReducer.ts

This file was deleted.

Loading