Sidebar.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2017-2020 @polkadot/app-accounts authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import React, { useCallback } from 'react';
  5. import styled from 'styled-components';
  6. import { useAccountInfo, useToggle } from '@polkadot/react-hooks';
  7. import { colorLink } from '@polkadot/react-components/styles/theme';
  8. import { AccountName, Button, Icon, IdentityIcon, Input, LinkExternal, Tags } from '@polkadot/react-components';
  9. import Transfer from '../Accounts/modals/Transfer';
  10. import { useTranslation } from '../translate';
  11. import Balances from './Balances';
  12. import Flags from './Flags';
  13. import Identity from './Identity';
  14. import Multisig from './Multisig';
  15. interface Props {
  16. address: string;
  17. className?: string;
  18. onClose: () => void;
  19. onUpdateName: () => void;
  20. }
  21. function Sidebar ({ address, className = '', onClose, onUpdateName }: Props): React.ReactElement<Props> {
  22. const { t } = useTranslation();
  23. const { accountIndex, flags, identity, isEditingName, isEditingTags, meta, name, onForgetAddress, onSaveName, onSaveTags, setName, setTags, tags, toggleIsEditingName, toggleIsEditingTags } = useAccountInfo(address);
  24. const [isHoveringButton, toggleIsHoveringButton] = useToggle();
  25. const [isTransferOpen, toggleIsTransferOpen] = useToggle();
  26. const _onForgetAddress = useCallback(
  27. (): void => {
  28. onForgetAddress();
  29. onUpdateName && onUpdateName();
  30. },
  31. [onForgetAddress, onUpdateName]
  32. );
  33. const _onUpdateName = useCallback(
  34. (): void => {
  35. onSaveName();
  36. onUpdateName && onUpdateName();
  37. },
  38. [onSaveName, onUpdateName]
  39. );
  40. return (
  41. <div className={className}>
  42. <Button
  43. className='ui--AddressMenu-close'
  44. icon='times'
  45. isBasic
  46. isCircular
  47. onClick={onClose}
  48. />
  49. <div className='ui--AddressMenu-header'>
  50. <IdentityIcon
  51. size={80}
  52. value={address}
  53. />
  54. <div className='ui--AddressMenu-addr'>
  55. {address}
  56. </div>
  57. {accountIndex && (
  58. <div className='ui--AddressMenu-addr'>
  59. {accountIndex}
  60. </div>
  61. )}
  62. <AccountName
  63. onClick={(flags.isEditable && !isEditingName) ? toggleIsEditingName : undefined}
  64. override={
  65. isEditingName
  66. ? (
  67. <Input
  68. autoFocus
  69. className='name--input'
  70. defaultValue={name}
  71. onBlur={(flags.isInContacts || flags.isOwned) ? _onUpdateName : undefined}
  72. onChange={setName}
  73. withLabel={false}
  74. />
  75. )
  76. : flags.isEditable
  77. ? (name.toUpperCase() || t<string>('<unknown>'))
  78. : undefined
  79. }
  80. value={address}
  81. withSidebar={false}
  82. >
  83. {(!isEditingName && flags.isEditable) && (
  84. <Icon
  85. className='inline-icon'
  86. icon='edit'
  87. />
  88. )}
  89. </AccountName>
  90. <div className='ui--AddressMenu-tags'>
  91. <Tags
  92. isEditable
  93. isEditing={isEditingTags}
  94. onChange={setTags}
  95. onSave={onSaveTags}
  96. onToggleIsEditing={toggleIsEditingTags}
  97. size='tiny'
  98. value={tags}
  99. />
  100. </div>
  101. <Flags flags={flags} />
  102. <div className='ui-AddressMenu--button'>
  103. <Button.Group>
  104. <Button
  105. icon='paper-plane'
  106. label={t<string>('Deposit')}
  107. onClick={toggleIsTransferOpen}
  108. />
  109. {flags.isOwned && (
  110. <Button
  111. icon='check'
  112. isBasic
  113. label={t<string>('Owned')}
  114. onMouseEnter={toggleIsHoveringButton}
  115. onMouseLeave={toggleIsHoveringButton}
  116. />
  117. )}
  118. {!flags.isOwned && !flags.isInContacts && (
  119. <Button
  120. icon='plus'
  121. isPositive
  122. label={t<string>('Save')}
  123. onClick={_onUpdateName}
  124. onMouseEnter={toggleIsHoveringButton}
  125. onMouseLeave={toggleIsHoveringButton}
  126. />
  127. )}
  128. {!flags.isOwned && flags.isInContacts && (
  129. <Button
  130. className='ui--AddressMenu-button'
  131. icon='ban'
  132. isNegative={isHoveringButton}
  133. isPositive={!isHoveringButton}
  134. label={t<string>('Remove')}
  135. onClick={_onForgetAddress}
  136. onMouseEnter={toggleIsHoveringButton}
  137. onMouseLeave={toggleIsHoveringButton}
  138. />
  139. )}
  140. </Button.Group>
  141. {isTransferOpen && (
  142. <Transfer
  143. key='modal-transfer'
  144. onClose={toggleIsTransferOpen}
  145. recipientId={address}
  146. />
  147. )}
  148. </div>
  149. </div>
  150. <Balances address={address} />
  151. <Identity
  152. address={address}
  153. identity={identity}
  154. />
  155. <Multisig
  156. isMultisig={flags.isMultisig}
  157. meta={meta}
  158. />
  159. <section>
  160. <LinkExternal
  161. data={address}
  162. type='address'
  163. />
  164. </section>
  165. </div>
  166. );
  167. }
  168. export default React.memo(styled(Sidebar)`
  169. bottom: 0;
  170. position: fixed;
  171. top: 0;
  172. right: 0;
  173. bottom: 0;
  174. max-width: 24rem;
  175. background: #f5f4f3;
  176. padding: 1rem;
  177. box-shadow: -6px 0px 20px 0px rgba(0,0,0,0.2);
  178. z-index: 999;
  179. input {
  180. width: auto !important;
  181. }
  182. .ui--AddressMenu-close {
  183. position: absolute;
  184. right: 0.5rem;
  185. top: 0.5rem;
  186. }
  187. .ui--AddressMenu-header {
  188. align-items: center;
  189. background: white;
  190. border-bottom: 1px solid #e6e6e6;
  191. display: flex;
  192. flex-direction: column;
  193. justify-content: center;
  194. margin: -1rem -1rem 1rem -1rem;
  195. padding: 1rem;
  196. .ui--Button {
  197. transition: 0.5s all;
  198. }
  199. }
  200. .ui--AddressMenu-addr {
  201. font-family: monospace;
  202. margin: 0.5rem 0;
  203. overflow: hidden;
  204. text-align: center;
  205. text-overflow: ellipsis;
  206. width: 100%;
  207. }
  208. .ui--AddressMenu-addr+.ui--AddressMenu-addr {
  209. margin-top: -0.25rem;
  210. }
  211. section {
  212. &:not(:last-child) {
  213. margin-bottom: 1.4rem;
  214. }
  215. .ui--AddressMenu-sectionHeader {
  216. display: inline-flex;
  217. color: #aaa;
  218. margin-bottom: 0.4rem;
  219. width: 100%;
  220. & > :first-child {
  221. flex: 1;
  222. }
  223. }
  224. }
  225. .ui--AddressMenu-identity {
  226. .ui--AddressMenu-identityTable {
  227. font-size: 0.93rem;
  228. margin-top: 0.3rem;
  229. .tr {
  230. display: inline-flex;
  231. align-items: center;
  232. width: 100%;
  233. .th {
  234. font-weight: bold;
  235. text-align: right;
  236. flex-basis: 20%;
  237. }
  238. .td {
  239. flex: 1;
  240. overflow: hidden;
  241. padding-left: 0.6rem;
  242. text-overflow: ellipsis;
  243. }
  244. }
  245. }
  246. .parent {
  247. padding: 0 !important;
  248. }
  249. }
  250. .ui--AddressMenu-tags,
  251. .ui--AddressMenu-flags {
  252. margin-bottom: 0.75rem;
  253. }
  254. .ui--AddressMenu-flags {
  255. display: flex;
  256. flex-wrap: wrap;
  257. justify-content: flex-end;
  258. > * {
  259. margin-bottom: 0.4rem;
  260. &:not(:first-child) {
  261. margin-left: 1rem;
  262. margin-right: 0;
  263. }
  264. }
  265. }
  266. .ui--AddressMenu-identityIcon {
  267. background: ${colorLink}66;
  268. }
  269. .ui--AddressMenu-actions {
  270. ul {
  271. list-style-type: none;
  272. margin-block-start: 0;
  273. margin-block-end: 0;
  274. padding-inline-start: 1rem;
  275. li {
  276. margin: 0.2rem 0;
  277. }
  278. }
  279. }
  280. .tags--toggle {
  281. display: inline-block;
  282. }
  283. .inline-icon {
  284. cursor: pointer;
  285. margin: 0 0 0 0.5rem;
  286. color: ${colorLink};
  287. }
  288. .name--input {
  289. .ui.input {
  290. margin: 0 !important;
  291. > input {
  292. padding: 0 !important;
  293. background: rgba(230, 230, 230, 0.8) !important;
  294. border: 0 !important;
  295. border-radius: 0 !important;
  296. box-shadow: 0 3px 3px rgba(0,0,0,.2);
  297. }
  298. }
  299. }
  300. `);