diff --git a/packages/apps/src/Content/index.tsx b/packages/apps/src/Content/index.tsx index 609a5d4e0b..2a0b5ae7ac 100644 --- a/packages/apps/src/Content/index.tsx +++ b/packages/apps/src/Content/index.tsx @@ -100,7 +100,7 @@ export default React.memo(styled(Content)(({ theme }: ThemeProps) => ` padding: 3.5rem 0; } - & main > *:not(header):not(.ui--FilterOverlay) { + & main > *:not(header):not(.hasOwnMaxWidth) { max-width: ${theme.contentMaxWidth}; margin-right: auto; margin-left: auto; diff --git a/packages/page-contracts/src/Banner.tsx b/packages/page-contracts/src/Contracts/Banner.tsx similarity index 97% rename from packages/page-contracts/src/Banner.tsx rename to packages/page-contracts/src/Contracts/Banner.tsx index bb41d2b22f..d12eb03b54 100644 --- a/packages/page-contracts/src/Banner.tsx +++ b/packages/page-contracts/src/Contracts/Banner.tsx @@ -7,7 +7,7 @@ import styled from 'styled-components'; import { Button } from '@polkadot/react-components'; import { useToggle } from '@polkadot/react-hooks'; -import { useTranslation } from './translate'; +import { useTranslation } from '../translate'; interface Props { className?: string; diff --git a/packages/page-contracts/src/Contracts/ContractsTable.tsx b/packages/page-contracts/src/Contracts/ContractsTable.tsx new file mode 100644 index 0000000000..2bfdfe504e --- /dev/null +++ b/packages/page-contracts/src/Contracts/ContractsTable.tsx @@ -0,0 +1,138 @@ +// Copyright 2017-2020 @polkadot/app-staking authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { ContractCallOutcome } from '@polkadot/api-contract/types'; +import type { FullNewBlock } from '@polkadot/api-derive/types'; +import type { ContractLink } from './types'; + +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; + +import { ApiPromise } from '@polkadot/api'; +import { ContractPromise } from '@polkadot/api-contract'; +import { Table } from '@polkadot/react-components'; +import { useApi, useCall } from '@polkadot/react-hooks'; +import { formatNumber } from '@polkadot/util'; + +import { useTranslation } from '../translate'; +import Call from './Call'; +import Contract from './Contract'; +import { getContractForAddress } from './util'; + +export interface Props { + contracts: string[]; + updated: number; +} + +interface Indexes { + contractIndex: number; + messageIndex: number; + onCallResult?: (messageIndex: number, result?: ContractCallOutcome) => void; +} + +function filterContracts (api: ApiPromise, keyringContracts: string[] = []): ContractPromise[] { + return keyringContracts + .map((address) => getContractForAddress(api, address.toString())) + .filter((contract): contract is ContractPromise => !!contract); +} + +function ContractsTable ({ contracts: keyringContracts }: Props): React.ReactElement { + const { t } = useTranslation(); + const { api } = useApi(); + const newBlock = useCall(api.derive.chain.subscribeNewBlocks); + const [{ contractIndex, messageIndex, onCallResult }, setIndexes] = useState({ contractIndex: 0, messageIndex: 0 }); + const [isCallOpen, setIsCallOpen] = useState(false); + const [contractLinks, setContractLinks] = useState>({}); + + const headerRef = useRef<[string?, string?, number?][]>([ + [t('contracts'), 'start'], + [undefined, undefined, 3], + [t('status'), 'start'], + [t('projection'), 'media--1100'], + [] + ]); + + useEffect((): void => { + if (newBlock) { + const exts = newBlock.block.extrinsics + .filter(({ method }) => api.tx.contracts.call.is(method)) + .map(({ args }): ContractLink | null => { + const contractId = keyringContracts.find((a) => args[0].eq(a)); + + if (!contractId) { + return null; + } + + return { + blockHash: newBlock.blockHash.toHex(), + blockNumber: formatNumber(newBlock.blockNumber), + contractId + }; + }) + .filter((value): value is ContractLink => !!value); + + exts.length && setContractLinks((links): Record => { + exts.forEach((value): void => { + links[value.contractId] = [value].concat(links[value.contractId] || []).slice(0, 3); + }); + + return { ...links }; + }); + } + }, [api, keyringContracts, newBlock]); + + const contracts = useMemo( + () => filterContracts(api, keyringContracts), + [api, keyringContracts] + ); + + const _toggleCall = useCallback( + () => setIsCallOpen((isCallOpen) => !isCallOpen), + [] + ); + + const _onCall = useCallback( + (contractIndex: number, messageIndex: number, onCallResult: (messageIndex: number, result?: ContractCallOutcome) => void): void => { + setIndexes({ contractIndex, messageIndex, onCallResult }); + setIsCallOpen(true); + }, + [] + ); + + const _setMessageIndex = useCallback( + (messageIndex: number) => setIndexes((state) => ({ ...state, messageIndex })), + [] + ); + + const contract = contracts[contractIndex] || null; + + return ( + <> + ('No contracts available')} + header={headerRef.current} + > + {contracts.map((contract, index): React.ReactNode => ( + + ))} +
+ {isCallOpen && contract && ( + + )} + + ); +} + +export default React.memo(ContractsTable); diff --git a/packages/page-contracts/src/Deploy.tsx b/packages/page-contracts/src/Contracts/Deploy.tsx similarity index 97% rename from packages/page-contracts/src/Deploy.tsx rename to packages/page-contracts/src/Contracts/Deploy.tsx index 590c87c3c9..a20f87ede3 100644 --- a/packages/page-contracts/src/Deploy.tsx +++ b/packages/page-contracts/src/Contracts/Deploy.tsx @@ -15,12 +15,12 @@ import { keyring } from '@polkadot/ui-keyring'; import { isHex } from '@polkadot/util'; import { randomAsHex } from '@polkadot/util-crypto'; -import { ENDOWMENT } from './constants'; -import { ABI, InputMegaGas, InputName, MessageSignature, Params } from './shared'; -import store from './store'; -import { useTranslation } from './translate'; -import useAbi from './useAbi'; -import useWeight from './useWeight'; +import { ENDOWMENT } from '../constants'; +import { ABI, InputMegaGas, InputName, MessageSignature, Params } from '../shared'; +import store from '../store'; +import { useTranslation } from '../translate'; +import useAbi from '../useAbi'; +import useWeight from '../useWeight'; interface Props { codeHash: string; diff --git a/packages/page-contracts/src/Summary.tsx b/packages/page-contracts/src/Contracts/Summary.tsx similarity index 97% rename from packages/page-contracts/src/Summary.tsx rename to packages/page-contracts/src/Contracts/Summary.tsx index 83126b102d..c2c119153e 100644 --- a/packages/page-contracts/src/Summary.tsx +++ b/packages/page-contracts/src/Contracts/Summary.tsx @@ -8,7 +8,7 @@ import { CardSummary, SummaryBox } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; -import { useTranslation } from './translate'; +import { useTranslation } from '../translate'; interface Props { trigger: number; diff --git a/packages/page-contracts/src/Contracts/index.tsx b/packages/page-contracts/src/Contracts/index.tsx index 4854fb9795..ec55df6772 100644 --- a/packages/page-contracts/src/Contracts/index.tsx +++ b/packages/page-contracts/src/Contracts/index.tsx @@ -1,138 +1,125 @@ -// Copyright 2017-2020 @polkadot/app-staking authors & contributors +// Copyright 2017-2020 @polkadot/app-contracts authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { ContractCallOutcome } from '@polkadot/api-contract/types'; -import type { FullNewBlock } from '@polkadot/api-derive/types'; -import type { ContractLink } from './types'; +import type { AppProps as Props } from '@polkadot/react-components/types'; -import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; +import styled from 'styled-components'; -import { ApiPromise } from '@polkadot/api'; -import { ContractPromise } from '@polkadot/api-contract'; -import { Table } from '@polkadot/react-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; -import { formatNumber } from '@polkadot/util'; +import { Button } from '@polkadot/react-components'; +import { useContracts, useToggle } from '@polkadot/react-hooks'; +import Codes from '../Codes'; +import CodeAdd from '../Codes/Add'; +import CodeUpload from '../Codes/Upload'; +import ContractAdd from '../Contracts/Add'; +import ContractsTable from '../Contracts/ContractsTable'; +import store from '../store'; import { useTranslation } from '../translate'; -import Call from './Call'; -import Contract from './Contract'; -import { getContractForAddress } from './util'; +import Banner from './Banner'; +import Deploy from './Deploy'; +import Summary from './Summary'; -export interface Props { - contracts: string[]; - updated: number; -} - -interface Indexes { - contractIndex: number; - messageIndex: number; - onCallResult?: (messageIndex: number, result?: ContractCallOutcome) => void; -} - -function filterContracts (api: ApiPromise, keyringContracts: string[] = []): ContractPromise[] { - return keyringContracts - .map((address) => getContractForAddress(api, address.toString())) - .filter((contract): contract is ContractPromise => !!contract); -} - -function Contracts ({ contracts: keyringContracts }: Props): React.ReactElement { +function Contracts ({ className = '' }: Props): React.ReactElement { const { t } = useTranslation(); - const { api } = useApi(); - const newBlock = useCall(api.derive.chain.subscribeNewBlocks); - const [{ contractIndex, messageIndex, onCallResult }, setIndexes] = useState({ contractIndex: 0, messageIndex: 0 }); - const [isCallOpen, setIsCallOpen] = useState(false); - const [contractLinks, setContractLinks] = useState>({}); + const { allContracts } = useContracts(); + const [codeHash, setCodeHash] = useState(); + const [constructorIndex, setConstructorIndex] = useState(0); + const [isAddOpen, toggleAdd] = useToggle(); + const [isDeployOpen, toggleDeploy, setIsDeployOpen] = useToggle(); + const [isHashOpen, toggleHash] = useToggle(); + const [isUploadOpen, toggleUpload] = useToggle(); + const [updated, setUpdated] = useState(Date.now()); + const [allCodes, setAllCodes] = useState(store.getAllCode()); - const headerRef = useRef<[string?, string?, number?][]>([ - [t('contracts'), 'start'], - [undefined, undefined, 3], - [t('status'), 'start'], - [t('projection'), 'media--1100'], - [] - ]); + const _onShowDeploy = useCallback( + (codeHash: string, constructorIndex: number): void => { + setCodeHash(codeHash || (allCodes && allCodes[0] ? allCodes[0].json.codeHash : undefined)); + setConstructorIndex(constructorIndex); + toggleDeploy(); + }, + [allCodes, toggleDeploy] + ); - useEffect((): void => { - if (newBlock) { - const exts = newBlock.block.extrinsics - .filter(({ method }) => api.tx.contracts.call.is(method)) - .map(({ args }): ContractLink | null => { - const contractId = keyringContracts.find((a) => args[0].eq(a)); + const _onCloseDeploy = useCallback( + () => setIsDeployOpen(false), + [setIsDeployOpen] + ); - if (!contractId) { - return null; - } + useEffect( + (): void => { + const triggerUpdate = (): void => { + setUpdated(Date.now()); + setAllCodes(store.getAllCode()); + }; - return { - blockHash: newBlock.blockHash.toHex(), - blockNumber: formatNumber(newBlock.blockNumber), - contractId - }; - }) - .filter((value): value is ContractLink => !!value); - - exts.length && setContractLinks((links): Record => { - exts.forEach((value): void => { - links[value.contractId] = [value].concat(links[value.contractId] || []).slice(0, 3); + store.on('new-code', triggerUpdate); + store.on('removed-code', triggerUpdate); + store + .loadAll() + .then(() => setAllCodes(store.getAllCode())) + .catch((): void => { + // noop, handled internally }); - - return { ...links }; - }); - } - }, [api, keyringContracts, newBlock]); - - const contracts = useMemo( - () => filterContracts(api, keyringContracts), - [api, keyringContracts] - ); - - const _toggleCall = useCallback( - () => setIsCallOpen((isCallOpen) => !isCallOpen), - [] - ); - - const _onCall = useCallback( - (contractIndex: number, messageIndex: number, onCallResult: (messageIndex: number, result?: ContractCallOutcome) => void): void => { - setIndexes({ contractIndex, messageIndex, onCallResult }); - setIsCallOpen(true); }, [] ); - const _setMessageIndex = useCallback( - (messageIndex: number) => setIndexes((state) => ({ ...state, messageIndex })), - [] - ); - - const contract = contracts[contractIndex] || null; - return ( - <> - ('No contracts available')} - header={headerRef.current} - > - {contracts.map((contract, index): React.ReactNode => ( - - ))} -
- {isCallOpen && contract && ( - + + +