Skip to content

Commit

Permalink
accounts context (#83)
Browse files Browse the repository at this point in the history
* accounts context

* fixes
  • Loading branch information
TopETH authored Apr 24, 2024
1 parent 71d8b6d commit d2468fa
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 158 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
"@mui/lab": "5.0.0-alpha.134",
"@mui/material": "^5.15.14",
"@mui/x-date-pickers": "^6.19.5",
"@polkadot/api": "^10.12.6",
"@polkadot/api-contract": "^10.13.1",
"@polkadot/extension-inject": "^0.46.4",
"@polkadot/types": "^10.12.6",
"@polkadot/util": "^12.3.1",
"@scio-labs/use-inkathon": "^0.0.1-alpha.44",
"@polkadot/api": "latest",
"@polkadot/api-contract": "latest",
"@polkadot/extension-dapp": "latest",
"@polkadot/extension-inject": "latest",
"@polkadot/types": "latest",
"@polkadot/ui-keyring": "latest",
"@polkadot/util": "latest",
"@types/humanize-duration": "^3.27.3",
"clsx": "^1.1.1",
"coretime-utils": "^0.2.8",
Expand Down
32 changes: 16 additions & 16 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ import {
ListItemButton,
useTheme,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import React, { useState } from 'react';

import { KeyringState, useAccounts } from '@/contexts/account';

import styles from './index.module.scss';
import { ActionButton } from '../Elements';
import { WalletModal } from '../Modals/WalletConnect';
import { ProgressButton } from '../Elements';

export const Header = () => {
const theme = useTheme();
const {
state: { accounts, activeAccount, status },
setActiveAccount,
disconnectWallet,
connectWallet,
} = useAccounts();

const { activeAccount, disconnect, accounts, setActiveAccount } =
useInkathon();
const [accountsOpen, openAccounts] = useState(false);
const [walletModalOpen, openWalletModal] = useState(false);

const onDisconnect = () => {
openAccounts(false);
disconnect && disconnect();
disconnectWallet();
};

return (
Expand All @@ -49,7 +52,7 @@ export const Header = () => {
borderRadius: 4,
}}
>
{activeAccount.name}
{`${activeAccount.meta.name}(${activeAccount.meta.source})`}
<ExpandMore />
</ListItemButton>
)}
Expand All @@ -70,15 +73,15 @@ export const Header = () => {
<ListItemButton
key={index}
onClick={() => {
setActiveAccount && setActiveAccount(account);
setActiveAccount(account);
openAccounts(false);
}}
sx={{
borderRadius: '0.5rem',
background: theme.palette.grey['100'],
}}
>
{account.name}
{`${account.meta.name}(${account.meta.source})`}
</ListItemButton>
)
)}
Expand All @@ -96,17 +99,14 @@ export const Header = () => {
</Collapse>
</List>
) : (
<ActionButton
onClick={() => openWalletModal(true)}
<ProgressButton
onClick={() => connectWallet()}
label='Connect Wallet'
loading={status === KeyringState.LOADING}
/>
)}
</Box>
</Box>
<WalletModal
open={walletModalOpen}
onClose={() => openWalletModal(false)}
/>
</>
);
};
8 changes: 5 additions & 3 deletions src/components/Modals/Interlace/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
Typography,
useTheme,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { CoreMask } from 'coretime-utils';
import { useEffect, useState } from 'react';

import { RegionCard } from '@/components/Elements';

import { useAccounts } from '@/contexts/account';
import { useCoretimeApi } from '@/contexts/apis';
import { useRegions } from '@/contexts/regions';
import { useToast } from '@/contexts/toast';
Expand All @@ -36,7 +36,9 @@ export const InterlaceModal = ({
regionMetadata,
}: InterlaceModalProps) => {
const theme = useTheme();
const { activeAccount, activeSigner } = useInkathon();
const {
state: { activeAccount, activeSigner },
} = useAccounts();

const {
state: { api },
Expand Down Expand Up @@ -94,7 +96,7 @@ export const InterlaceModal = ({
};

useEffect(() => {
setPosition(oneStart);
open && setPosition(oneStart);
}, [open, oneStart]);

return (
Expand Down
6 changes: 4 additions & 2 deletions src/components/Modals/Partition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
Typography,
useTheme,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { useEffect, useState } from 'react';

import { RegionCard } from '@/components/Elements';

import { useAccounts } from '@/contexts/account';
import { useCoretimeApi } from '@/contexts/apis';
import { useCommon } from '@/contexts/common';
import { useRegions } from '@/contexts/regions';
Expand Down Expand Up @@ -59,7 +59,9 @@ export const PartitionModal = ({
},
];

const { activeSigner, activeAccount } = useInkathon();
const {
state: { activeSigner, activeAccount },
} = useAccounts();

const theme = useTheme();
const {
Expand Down
11 changes: 6 additions & 5 deletions src/components/Modals/Purchase/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
DialogContent,
Stack,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { useState } from 'react';

import { ListingCard } from '@/components/Elements/ListingCard';

import { useAccounts } from '@/contexts/account';
import { useToast } from '@/contexts/toast';
import { Listing } from '@/models';

Expand All @@ -25,22 +25,23 @@ export const PurchaseModal = ({
onClose,
listing,
}: PurchaseModalProps) => {
const { activeAccount, api } = useInkathon();
const {
state: { activeAccount },
} = useAccounts();

const { toastError, toastSuccess } = useToast();

const [working, setWorking] = useState(false);

const purchaseRegion = async () => {
if (!api || !activeAccount) {
if (!activeAccount) {
return;
}

try {
setWorking(true);

// TODO

// TODO:
toastSuccess(`Successfully purchased region from sale.`);
onClose();
setWorking(false);
Expand Down
12 changes: 7 additions & 5 deletions src/components/Modals/Sell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
Stack,
Typography,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { Region } from 'coretime-utils';
import { useEffect, useState } from 'react';

import { AmountInput, RegionCard } from '@/components/Elements';
import { RecipientSelector } from '@/components/Elements/Selectors/RecipientSelector';

import { useAccounts } from '@/contexts/account';
import { useCoretimeApi } from '@/contexts/apis';
import { useRegions } from '@/contexts/regions';
import { useToast } from '@/contexts/toast';
Expand All @@ -31,7 +31,9 @@ export const SellModal = ({
onClose,
regionMetadata,
}: SellModalProps) => {
const { activeAccount, api } = useInkathon();
const {
state: { activeAccount },
} = useAccounts();
const {
state: { symbol },
} = useCoretimeApi();
Expand All @@ -55,14 +57,14 @@ export const SellModal = ({
};

const approveXcRegion = async (_region: Region) => {
if (!api || !activeAccount) {
if (!activeAccount) {
return;
}

try {
setWorking(true);

// TODO
// TODO:

toastSuccess(`Successfully approved region to the market.`);
setWorking(false);
Expand All @@ -77,7 +79,7 @@ export const SellModal = ({
};

const listRegion = async (_region: Region) => {
if (!api || !activeAccount) {
if (!activeAccount) {
return;
}

Expand Down
6 changes: 4 additions & 2 deletions src/components/Modals/TaskAssign/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
TextField,
Typography,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { useEffect, useState } from 'react';

import { RegionCard } from '@/components/Elements';

import { useAccounts } from '@/contexts/account';
import { useCoretimeApi, useRelayApi } from '@/contexts/apis';
import { useRegions } from '@/contexts/regions';
import { useTasks } from '@/contexts/tasks';
Expand All @@ -34,7 +34,9 @@ export const TaskAssignModal = ({
onClose,
regionMetadata,
}: TaskAssignModalProps) => {
const { activeAccount, activeSigner } = useInkathon();
const {
state: { activeAccount, activeSigner },
} = useAccounts();

const { paraIds } = useRelayApi();
const {
Expand Down
6 changes: 4 additions & 2 deletions src/components/Modals/Transfer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
TextField,
Typography,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { Region } from 'coretime-utils';
import { useEffect, useState } from 'react';

import { transferRegionOnCoretimeChain } from '@/utils/native/transfer';

import { RegionCard } from '@/components/Elements';

import { useAccounts } from '@/contexts/account';
import { useCoretimeApi } from '@/contexts/apis';
import { useRegions } from '@/contexts/regions';
import { useToast } from '@/contexts/toast';
Expand All @@ -33,7 +33,9 @@ export const TransferModal = ({
onClose,
regionMetadata,
}: TransferModalProps) => {
const { activeAccount, activeSigner } = useInkathon();
const {
state: { activeAccount, activeSigner },
} = useAccounts();

const { fetchRegions } = useRegions();
const { toastError, toastInfo, toastSuccess } = useToast();
Expand Down
8 changes: 5 additions & 3 deletions src/components/Modals/Unlist/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
DialogContent,
Stack,
} from '@mui/material';
import { useInkathon } from '@scio-labs/use-inkathon';
import { Region } from 'coretime-utils';
import { useState } from 'react';

import { RegionCard } from '@/components/Elements';

import { useAccounts } from '@/contexts/account';
import { useMarket } from '@/contexts/market';
import { useRegions } from '@/contexts/regions';
import { useToast } from '@/contexts/toast';
Expand All @@ -28,7 +28,9 @@ export const UnlistModal = ({
onClose,
regionMetadata,
}: UnlistModalProps) => {
const { activeAccount, api } = useInkathon();
const {
state: { activeAccount },
} = useAccounts();

const { fetchRegions } = useRegions();
const { fetchMarket } = useMarket();
Expand All @@ -37,7 +39,7 @@ export const UnlistModal = ({
const [working, setWorking] = useState(false);

const unlistRegion = async (_region: Region) => {
if (!api || !activeAccount) {
if (!activeAccount) {
return;
}

Expand Down
3 changes: 0 additions & 3 deletions src/components/Modals/WalletConnect/index.module.scss

This file was deleted.

Loading

0 comments on commit d2468fa

Please sign in to comment.