diff --git a/packages/app-accounts/src/Account.tsx b/packages/app-accounts/src/Account.tsx index 7815437b78bf..6b47be0efdec 100644 --- a/packages/app-accounts/src/Account.tsx +++ b/packages/app-accounts/src/Account.tsx @@ -9,7 +9,7 @@ import React, { useState, useEffect } from 'react'; import { Label } from 'semantic-ui-react'; import styled from 'styled-components'; import { AddressInfo, AddressSmall, Button, ChainLock, Forget, Icon, InputTags, LinkPolkascan, Menu, Popup, Input } from '@polkadot/react-components'; -import { useApi } from '@polkadot/react-hooks'; +import { useApi, useToggle } from '@polkadot/react-hooks'; import keyring from '@polkadot/ui-keyring'; import Backup from './modals/Backup'; @@ -31,16 +31,16 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite const [tags, setTags] = useState([]); const [accName, setAccName] = useState(''); const [genesisHash, setGenesisHash] = useState(null); - const [isBackupOpen, setIsBackupOpen] = useState(false); - const [isEditingName, setIsEditingName] = useState(false); - const [isEditingTags, setIsEditingTags] = useState(false); - const [{ isDevelopment, isEditable, isExternal }, setFlags] = useState({ isDevelopment: false, isEditable: false, isExternal: false }); - const [isDeriveOpen, setIsDeriveOpen] = useState(false); - const [isForgetOpen, setIsForgetOpen] = useState(false); - const [isPasswordOpen, setIsPasswordOpen] = useState(false); - const [isSettingPopupOpen, setIsSettingPopupOpen] = useState(false); - const [isTransferOpen, setIsTransferOpen] = useState(false); const [isVisible, setIsVisible] = useState(true); + const [{ isDevelopment, isEditable, isExternal }, setFlags] = useState({ isDevelopment: false, isEditable: false, isExternal: false }); + const [isBackupOpen, toggleBackup] = useToggle(); + const [isEditingName, toggleEditName] = useToggle(); + const [isEditingTags, toggleEditTags] = useToggle(); + const [isDeriveOpen, toggleDerive] = useToggle(); + const [isForgetOpen, toggleForget] = useToggle(); + const [isPasswordOpen, togglePassword] = useToggle(); + const [isSettingsOpen, toggleSettings] = useToggle(); + const [isTransferOpen, toggleTransfer] = useToggle(); const _setTags = (tags: string[]): void => setTags(tags.sort()); @@ -73,16 +73,8 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite return null; } - const _toggleEditName = (): void => setIsEditingName(!isEditingName); - const _toggleEditTags = (): void => setIsEditingTags(!isEditingTags); - const _toggleBackup = (): void => setIsBackupOpen(!isBackupOpen); - const _toggleDerive = (): void => setIsDeriveOpen(!isDeriveOpen); - const _toggleForget = (): void => setIsForgetOpen(!isForgetOpen); - const _togglePass = (): void => setIsPasswordOpen(!isPasswordOpen); - const _toggleSettingPopup = (): void => setIsSettingPopupOpen(!isSettingPopupOpen); - const _toggleTransfer = (): void => setIsTransferOpen(!isTransferOpen); const _saveName = (): void => { - _toggleEditName(); + toggleEditName(); const meta = { name: accName, whenEdited: Date.now() }; @@ -97,7 +89,7 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite } }; const _saveTags = (): void => { - _toggleEditTags(); + toggleEditTags(); const meta = { tags, whenEdited: Date.now() }; @@ -165,7 +157,7 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite ) : undefined } - onClickName={_toggleEditName} + onClickName={toggleEditName} toggle={isEditingName} value={address} /> @@ -173,14 +165,14 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite )} {isDeriveOpen && ( )} {isForgetOpen && ( @@ -188,20 +180,20 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite address={address} onForget={_onForget} key='modal-forget-account' - onClose={_toggleForget} + onClose={toggleForget} /> )} {isPasswordOpen && ( )} {isTransferOpen && ( )} @@ -221,7 +213,7 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite /> ) : ( -
+
{tags.length ? tags.map((tag): React.ReactNode => ( @@ -252,19 +244,19 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite icon='paper plane' isPrimary label={t('send')} - onClick={_toggleTransfer} + onClick={toggleTransfer} size='small' tooltip={t('Send funds from this account')} /> } @@ -272,11 +264,11 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite {t('Derive account from source')} @@ -285,19 +277,19 @@ function Account ({ address, allowTags, className, isFavorite, t, toggleFavorite {t('Create a backup file for this account')} {t("Change this account's password")} {t('Forget this account')} diff --git a/packages/app-accounts/src/Overview.tsx b/packages/app-accounts/src/Overview.tsx index 99859420fa7c..cdf7053ff6f8 100644 --- a/packages/app-accounts/src/Overview.tsx +++ b/packages/app-accounts/src/Overview.tsx @@ -9,7 +9,7 @@ import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import keyring from '@polkadot/ui-keyring'; import { getLedger, isLedger } from '@polkadot/react-api'; -import { useAccounts, useFavorites } from '@polkadot/react-hooks'; +import { useAccounts, useFavorites, useToggle } from '@polkadot/react-hooks'; import { Button, InputTags, Table } from '@polkadot/react-components'; import CreateModal from './modals/Create'; @@ -41,9 +41,9 @@ async function queryLedger (): Promise { function Overview ({ className, onStatusChange, t }: Props): React.ReactElement { const { allAccounts, hasAccounts } = useAccounts(); - const [isCreateOpen, setIsCreateOpen] = useState(false); - const [isImportOpen, setIsImportOpen] = useState(false); - const [isQrOpen, setIsQrOpen] = useState(false); + const [isCreateOpen, toggleCreate] = useToggle(); + const [isImportOpen, toggleImport] = useToggle(); + const [isQrOpen, toggleQr] = useToggle(); const [favorites, toggleFavorite] = useFavorites(STORE_FAVS); const [sortedAccounts, setSortedAccounts] = useState([]); const [tags, setTags] = useState([]); @@ -62,28 +62,24 @@ function Overview ({ className, onStatusChange, t }: Props): React.ReactElement< ); }, [allAccounts, favorites]); - const _toggleCreate = (): void => setIsCreateOpen(!isCreateOpen); - const _toggleImport = (): void => setIsImportOpen(!isImportOpen); - const _toggleQr = (): void => setIsQrOpen(!isQrOpen); - return (
{isCreateOpen && ( )} {isImportOpen && ( )} {isQrOpen && ( )} @@ -92,21 +88,21 @@ function Overview ({ className, onStatusChange, t }: Props): React.ReactElement< icon='add' isPrimary label={t('Add account')} - onClick={_toggleCreate} + onClick={toggleCreate} />
{isUnlockVisible && ( diff --git a/packages/app-treasury/src/Overview/Propose.tsx b/packages/app-treasury/src/Overview/Propose.tsx index 86295d7a20aa..aa8b5d08bd8e 100644 --- a/packages/app-treasury/src/Overview/Propose.tsx +++ b/packages/app-treasury/src/Overview/Propose.tsx @@ -7,7 +7,7 @@ import { I18nProps as Props } from '@polkadot/react-components/types'; import BN from 'bn.js'; import React, { useState } from 'react'; import { Button, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components'; -import { useAccounts } from '@polkadot/react-hooks'; +import { useAccounts, useToggle } from '@polkadot/react-hooks'; import translate from '../translate'; @@ -15,15 +15,13 @@ function Propose ({ className, t }: Props): React.ReactElement | null { const { hasAccounts } = useAccounts(); const [accountId, setAccountId] = useState(null); const [beneficiary, setBeneficiary] = useState(null); - const [isProposeOpen, setIsProposeOpen] = useState(false); const [value, setValue] = useState(); + const [isProposeOpen, togglePropose] = useToggle(); if (!hasAccounts) { return null; } - const _togglePropose = (): void => setIsProposeOpen(!isProposeOpen); - const hasValue = value?.gtn(0); return ( @@ -64,7 +62,7 @@ function Propose ({ className, t }: Props): React.ReactElement | null { icon='cancel' isNegative label={t('Cancel')} - onClick={_togglePropose} + onClick={togglePropose} /> | null { isDisabled={!accountId || !hasValue} isPrimary label={t('Submit proposal')} - onClick={_togglePropose} + onClick={togglePropose} params={[value, beneficiary]} tx='treasury.proposeSpend' /> @@ -85,7 +83,7 @@ function Propose ({ className, t }: Props): React.ReactElement | null { icon='check' isPrimary label={t('Submit proposal')} - onClick={_togglePropose} + onClick={togglePropose} /> ); diff --git a/packages/app-treasury/src/Overview/Voting.tsx b/packages/app-treasury/src/Overview/Voting.tsx index fe662ec5b28b..2792d7890b0e 100644 --- a/packages/app-treasury/src/Overview/Voting.tsx +++ b/packages/app-treasury/src/Overview/Voting.tsx @@ -8,7 +8,7 @@ import { I18nProps } from '@polkadot/react-components/types'; import React, { useEffect, useState } from 'react'; import { Button, Dropdown, Input, Modal, VoteAccount, VoteActions, VoteToggle } from '@polkadot/react-components'; -import { useAccounts } from '@polkadot/react-hooks'; +import { useAccounts, useToggle } from '@polkadot/react-hooks'; import { isBoolean } from '@polkadot/util'; import translate from '../translate'; @@ -28,8 +28,8 @@ function Voting ({ proposals, t }: Props): React.ReactElement | null { const [councilOptId, setCouncilOptId] = useState(0); const [accountId, setAccountId] = useState(null); const [{ councilId, councilHash }, setCouncilInfo] = useState<{ councilId: ProposalIndex | null; councilHash: Hash | null }>({ councilId: null, councilHash: null }); - const [isVotingOpen, setIsVotingOpen] = useState(false); const [voteValue, setVoteValue] = useState(true); + const [isVotingOpen, toggleVoting] = useToggle(); useEffect((): void => { const available = proposals @@ -47,7 +47,6 @@ function Voting ({ proposals, t }: Props): React.ReactElement | null { return null; } - const _toggleVoting = (): void => setIsVotingOpen(!isVotingOpen); const _onChangeVote = (vote?: boolean): void => setVoteValue(isBoolean(vote) ? vote : true); const _onChangeProposal = (optionId: number): void => { const councilProp = proposals.find(({ votes }): boolean => !!(votes?.index.eq(optionId))); @@ -91,7 +90,7 @@ function Voting ({ proposals, t }: Props): React.ReactElement | null { @@ -101,7 +100,7 @@ function Voting ({ proposals, t }: Props): React.ReactElement | null { icon='check' isPrimary label={t('Vote')} - onClick={_toggleVoting} + onClick={toggleVoting} /> ); diff --git a/packages/react-components/src/HelpOverlay.tsx b/packages/react-components/src/HelpOverlay.tsx index 52195f5c8c13..1a87843e28f9 100644 --- a/packages/react-components/src/HelpOverlay.tsx +++ b/packages/react-components/src/HelpOverlay.tsx @@ -4,9 +4,10 @@ import { BareProps } from './types'; -import React, { useState } from 'react'; +import React from 'react'; import ReactMd from 'react-markdown'; import styled from 'styled-components'; +import { useToggle } from '@polkadot/react-hooks'; import Icon from './Icon'; @@ -15,23 +16,21 @@ interface Props extends BareProps { } function HelpOverlay ({ className, md }: Props): React.ReactElement { - const [isVisible, setIsVisible] = useState(false); - - const _toggleVisible = (): void => setIsVisible(!isVisible); + const [isVisible, toggleVisible] = useToggle(); return (
| null { const history = useHistory(); - const [isCollapsed, setIsCollapsed] = useState(true); + const [isCollapsed, toggleCollapsed] = useToggle(); if (!children) { return null; @@ -32,7 +33,6 @@ function Inset ({ children, className, header, href, isCollapsible, isError, isS const _onClick = (): void => { href && history.push(href); }; - const _toggleCollapsed = (): void => setIsCollapsed(!isCollapsed); return (

{header}

{ - const [isTooltipOpen, setIsTooltipOpen] = useState(false); - - const _toggleTooltip = (): void => setIsTooltipOpen(!isTooltipOpen); + const [isTooltipOpen, toggleTooltip] = useToggle(); return (
{isTooltipOpen && ( { - const [isVisible, setIsVisible] = useState(false); - - const _toggleVisible = (): void => setIsVisible(!isVisible); + const [isVisible, toggleVisible] = useToggle(); return ( {children} diff --git a/packages/react-params/src/Param/Bytes.tsx b/packages/react-params/src/Param/Bytes.tsx index 181440066117..c786ec44b623 100644 --- a/packages/react-params/src/Param/Bytes.tsx +++ b/packages/react-params/src/Param/Bytes.tsx @@ -4,17 +4,17 @@ import { Props } from '../types'; -import React, { useState } from 'react'; +import React from 'react'; import { Compact } from '@polkadot/types'; import { Button } from '@polkadot/react-components'; +import { useToggle } from '@polkadot/react-hooks'; import BaseBytes from './BaseBytes'; import File from './File'; export default function Bytes ({ className, defaultValue, isDisabled, isError, label, name, onChange, onEnter, onEscape, style, type, withLabel }: Props): React.ReactElement { - const [isFileDrop, setIsFileDrop] = useState(false); + const [isFileDrop, toggleFile] = useToggle(); - const _toggleFile = (): void => setIsFileDrop(true); const _onChangeFile = (value: Uint8Array): void => { onChange && onChange({ isValid: value.length !== 0, @@ -54,7 +54,7 @@ export default function Bytes ({ className, defaultValue, isDisabled, isError, l {!isDisabled && (