diff --git a/packages/page-assets/src/Overview/Create/Create.tsx b/packages/page-assets/src/Overview/Create/Create.tsx index b83bc23ec0..2553e94f5a 100644 --- a/packages/page-assets/src/Overview/Create/Create.tsx +++ b/packages/page-assets/src/Overview/Create/Create.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2021 @polkadot/app-assets authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type BN from 'bn.js'; import type { AssetId } from '@polkadot/types/interfaces'; import type { InfoState, TeamState } from './types'; @@ -17,13 +18,14 @@ interface Props { assetIds: AssetId[]; className?: string; onClose: () => void; + openId: BN; } const BATCH_OPTIONS = { isBatchAll: true }; -function Create ({ assetIds, className, onClose }: Props): React.ReactElement { +function Create ({ assetIds, className, onClose, openId }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [step, nextStep, prevStep] = useStepper(); @@ -69,6 +71,7 @@ function Create ({ assetIds, className, onClose }: Props): React.ReactElement )} {step === 2 && asset && ( diff --git a/packages/page-assets/src/Overview/Create/Info.tsx b/packages/page-assets/src/Overview/Create/Info.tsx index 3a44e3a2cd..02cdcb24b0 100644 --- a/packages/page-assets/src/Overview/Create/Info.tsx +++ b/packages/page-assets/src/Overview/Create/Info.tsx @@ -9,7 +9,7 @@ import React, { useEffect, useMemo, useState } from 'react'; import { Input, InputAddress, InputBalance, InputNumber, Modal } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; -import { BN_ONE, BN_ZERO } from '@polkadot/util'; +import { BN_ZERO } from '@polkadot/util'; import { useTranslation } from '../../translate'; @@ -18,19 +18,14 @@ interface Props { className?: string; defaultValue: InfoState | null; onChange: (info: InfoState | null) => void; + openId: BN; } -function findAvailbleId (assetIds: AssetId[]): BN { - return assetIds.length - ? assetIds[assetIds.length - 1].add(BN_ONE) - : BN_ONE; -} - -function Info ({ assetIds, className = '', defaultValue, onChange }: Props): React.ReactElement { +function Info ({ assetIds, className = '', defaultValue, onChange, openId }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [initial] = useState(() => defaultValue); - const [initialId] = useState(() => findAvailbleId(assetIds)); + const [initialId] = useState(() => openId); const [accountId, setAccountId] = useState(null); const [assetId, setAssetId] = useState(null); const [assetDecimals, setAssetDecimals] = useState(null); diff --git a/packages/page-assets/src/Overview/Create/index.tsx b/packages/page-assets/src/Overview/Create/index.tsx index abe3a06247..c6fa7029af 100644 --- a/packages/page-assets/src/Overview/Create/index.tsx +++ b/packages/page-assets/src/Overview/Create/index.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2021 @polkadot/app-assets authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type BN from 'bn.js'; import type { AssetId } from '@polkadot/types/interfaces'; import React from 'react'; @@ -14,9 +15,10 @@ import Create from './Create'; interface Props { assetIds?: AssetId[]; className?: string; + openId: BN; } -function CreateButton ({ assetIds, className }: Props): React.ReactElement { +function CreateButton ({ assetIds, className, openId }: Props): React.ReactElement { const { t } = useTranslation(); const { hasAccounts } = useAccounts(); const [isOpen, toggleOpen] = useToggle(); @@ -34,6 +36,7 @@ function CreateButton ({ assetIds, className }: Props): React.ReactElement )} diff --git a/packages/page-assets/src/Overview/index.tsx b/packages/page-assets/src/Overview/index.tsx index 1f69ef02cf..9d31cc5b1e 100644 --- a/packages/page-assets/src/Overview/index.tsx +++ b/packages/page-assets/src/Overview/index.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2021 @polkadot/app-assets authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type BN from 'bn.js'; import type { AssetId } from '@polkadot/types/interfaces'; import type { AssetInfo } from '../types'; @@ -16,14 +17,18 @@ interface Props { className?: string; ids?: AssetId[]; infos?: AssetInfo[]; + openId: BN; } -function Overview ({ className, ids, infos }: Props): React.ReactElement { +function Overview ({ className, ids, infos, openId }: Props): React.ReactElement { return (
- +
diff --git a/packages/page-assets/src/index.tsx b/packages/page-assets/src/index.tsx index eba5085a6d..a9c98e5e3c 100644 --- a/packages/page-assets/src/index.tsx +++ b/packages/page-assets/src/index.tsx @@ -1,11 +1,15 @@ // Copyright 2017-2021 @polkadot/app-assets authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type BN from 'bn.js'; +import type { AssetId } from '@polkadot/types/interfaces'; + import React, { useMemo, useRef } from 'react'; import { Route, Switch } from 'react-router'; import { Tabs } from '@polkadot/react-components'; import { useAccounts } from '@polkadot/react-hooks'; +import { BN_ONE } from '@polkadot/util'; import Balances from './Balances'; import Overview from './Overview'; @@ -18,7 +22,23 @@ interface Props { className?: string; } -function GiltApp ({ basePath, className }: Props): React.ReactElement { +function findOpenId (ids?: AssetId[]): BN { + if (!ids || !ids.length) { + return BN_ONE; + } + + const lastTaken = ids.find((id, index) => + index === 0 + ? !id.eq(BN_ONE) + : !id.sub(BN_ONE).eq(ids[index - 1]) + ); + + return lastTaken + ? lastTaken.sub(BN_ONE) + : ids[ids.length - 1].add(BN_ONE); +} + +function AssetApp ({ basePath, className }: Props): React.ReactElement { const { t } = useTranslation(); const { hasAccounts } = useAccounts(); const ids = useAssetIds(); @@ -41,6 +61,11 @@ function GiltApp ({ basePath, className }: Props): React.ReactElement { [hasAccounts, infos] ); + const openId = useMemo( + () => findOpenId(ids), + [ids] + ); + return (
{ @@ -63,4 +89,4 @@ function GiltApp ({ basePath, className }: Props): React.ReactElement { ); } -export default React.memo(GiltApp); +export default React.memo(AssetApp);