From 67b69dc9760cdb2e5d66688e43c81d73527ebf17 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 23 Apr 2020 10:49:34 +0200 Subject: [PATCH] Split Sidebar flags (& use component) (#2609) * Split Sidebar flags (& use component) * Fix Account flags expansion --- .../page-accounts/src/Accounts/Account.tsx | 2 +- packages/page-accounts/src/Sidebar/Flags.tsx | 72 ++++++++++ .../page-accounts/src/Sidebar/Sidebar.tsx | 133 +++++------------- packages/react-components/src/Tag.tsx | 14 +- packages/react-hooks/src/types.ts | 3 +- packages/react-hooks/src/useAccountInfo.ts | 4 +- 6 files changed, 121 insertions(+), 107 deletions(-) create mode 100644 packages/page-accounts/src/Sidebar/Flags.tsx diff --git a/packages/page-accounts/src/Accounts/Account.tsx b/packages/page-accounts/src/Accounts/Account.tsx index 8ca53abc3f..d201cec18e 100644 --- a/packages/page-accounts/src/Accounts/Account.tsx +++ b/packages/page-accounts/src/Accounts/Account.tsx @@ -51,7 +51,7 @@ function Account ({ account: { address, meta }, className, filter, isFavorite, s const recoveryInfo = useCall(api.api.query.recovery?.recoverable, [address], { transform: (opt: Option) => opt.unwrapOr(null) }); - const { genesisHash, isDevelopment, isEditable, isExternal, name: accName, onSaveGenesisHash, tags } = useAccountInfo(address); + const { flags: { isDevelopment, isEditable, isExternal }, genesisHash, name: accName, onSaveGenesisHash, tags } = useAccountInfo(address); const [isVisible, setIsVisible] = useState(true); const [isBackupOpen, toggleBackup] = useToggle(); const [isDeriveOpen, toggleDerive] = useToggle(); diff --git a/packages/page-accounts/src/Sidebar/Flags.tsx b/packages/page-accounts/src/Sidebar/Flags.tsx new file mode 100644 index 0000000000..1f7d2e4d69 --- /dev/null +++ b/packages/page-accounts/src/Sidebar/Flags.tsx @@ -0,0 +1,72 @@ +// Copyright 2017-2020 @polkadot/app-accounts authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { AddressFlags } from '@polkadot/react-hooks/types'; + +import React from 'react'; +import { Tag } from '@polkadot/react-components'; + +import { useTranslation } from '../translate'; + +interface Props { + flags: AddressFlags; +} + +function Flags ({ flags: { isCouncil, isDevelopment, isExternal, isSociety, isSudo, isTechCommittee } }: Props): React.ReactElement | null { + const { t } = useTranslation(); + const hasFlags = isDevelopment || isExternal || isSociety || isCouncil || isTechCommittee || isSudo; + + if (!hasFlags) { + return null; + } + + return ( +
+ {isExternal && ( + + )} + {isDevelopment && ( + + )} + {isCouncil && ( + + )} + {isSociety && ( + + )} + {isTechCommittee && ( + + )} + {isSudo && ( + + )} +
+ ); +} + +export default React.memo(Flags); diff --git a/packages/page-accounts/src/Sidebar/Sidebar.tsx b/packages/page-accounts/src/Sidebar/Sidebar.tsx index f8af3393f8..690fc3564e 100644 --- a/packages/page-accounts/src/Sidebar/Sidebar.tsx +++ b/packages/page-accounts/src/Sidebar/Sidebar.tsx @@ -6,15 +6,15 @@ import { BareProps } from '@polkadot/react-components/types'; import { AccountId, Address } from '@polkadot/types/interfaces'; import React, { useCallback } from 'react'; -import { Label } from 'semantic-ui-react'; import styled from 'styled-components'; import { useAccountInfo, useApi, useRegistrars, useToggle } from '@polkadot/react-hooks'; import { classes } from '@polkadot/react-components/util'; import { colorLink } from '@polkadot/react-components/styles/theme'; -import { AccountNameJudgement, AccountName, AddressMini, AvatarItem, Button, Icon, IconLink, IdentityIcon, Input, InputTags, LinkExternal, Transfer } from '@polkadot/react-components'; +import { AccountNameJudgement, AccountName, AddressMini, AvatarItem, Button, Icon, IconLink, IdentityIcon, Input, InputTags, LinkExternal, Tag, Transfer } from '@polkadot/react-components'; import { isHex } from '@polkadot/util'; import { useTranslation } from '../translate'; +import Flags from './Flags'; interface Props extends BareProps { address: AccountId | Address | string | Uint8Array; @@ -26,7 +26,7 @@ function Sidebar ({ address, className, onClose, onUpdateName, style }: Props): const { t } = useTranslation(); const { api } = useApi(); const { isRegistrar, registrars } = useRegistrars(); - const { identity, isCouncil, isDevelopment, isEditable, isEditingName, isEditingTags, isExternal, isInContacts, isOwned, isSociety, isSudo, isTechCommittee, name, onForgetAddress, onSaveName, onSaveTags, setName, setTags, tags, toggleIsEditingName, toggleIsEditingTags } = useAccountInfo(address); + const { flags, identity, isEditingName, isEditingTags, name, onForgetAddress, onSaveName, onSaveTags, setName, setTags, tags, toggleIsEditingName, toggleIsEditingTags } = useAccountInfo(address); const [isHoveringButton, toggleIsHoveringButton] = useToggle(); const [isTransferOpen, toggleIsTransferOpen] = useToggle(); const [isJudgementOpen, toggleIsJudgementOpen] = useToggle(); @@ -48,7 +48,6 @@ function Sidebar ({ address, className, onClose, onUpdateName, style }: Props): ); const useIdentity = !!api.query.identity?.identityOf; - const hasFlags = isDevelopment || isExternal || isSociety || isCouncil || isTechCommittee || isSudo; return ( <> @@ -72,7 +71,7 @@ function Sidebar ({ address, className, onClose, onUpdateName, style }: Props): {address.toString()} ) - : isEditable + : flags.isEditable ? (name.toUpperCase() || t('')) : undefined } value={address} withSidebar={false} > - {(!isEditingName && isEditable) && ( + {(!isEditingName && flags.isEditable) && ( {tags.length ? tags.map((tag): React.ReactNode => ( - + /> )) : } ) } - {(!isEditingTags && (isInContacts || isOwned)) && ( + {(!isEditingTags && (flags.isInContacts || flags.isOwned)) && ( )} - {hasFlags && ( -
- {isExternal && ( - - )} - {isDevelopment && ( - - )} - {isCouncil && ( - - )} - {isSociety && ( - - )} - {isTechCommittee && ( - - )} - {isSudo && ( - - )} -
- )} +
@@ -259,10 +200,10 @@ function Sidebar ({ address, className, onClose, onUpdateName, style }: Props):
- {' '} +   {t('identity')}
- + />
{ +function Tag ({ className, color, hover, isTag = true, label, size = 'small' }: Props): React.ReactElement { const [trigger] = useState(`tag-hover-${Date.now()}-${tagId++}`); + const tooltipProps = hover + ? { 'data-for': trigger, 'data-tip': true } + : {}; return (