Sidebar.tsx 8.5 KB

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