SetKey.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2017-2020 @polkadot/app-js 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, { useEffect, useState } from 'react';
  5. import { AddressMini, Icon, InputAddress, Labelled, TxButton } from '@polkadot/react-components';
  6. import styled from 'styled-components';
  7. import { useTranslation } from './translate';
  8. interface Props {
  9. allAccounts: string[];
  10. className?: string;
  11. isMine?: boolean;
  12. sudoKey?: string;
  13. }
  14. function SetKey ({ allAccounts, className = '', isMine, sudoKey }: Props): React.ReactElement<Props> {
  15. const { t } = useTranslation();
  16. const [selected, setSelected] = useState<string | null>(null);
  17. useEffect((): void => {
  18. sudoKey && !selected && setSelected(sudoKey);
  19. }, [selected, sudoKey]);
  20. const willLose = isMine &&
  21. !!selected &&
  22. selected !== sudoKey &&
  23. allAccounts.some((s): boolean => s === selected);
  24. return (
  25. <section>
  26. <section className={`${className} ui--row`}>
  27. {isMine
  28. ? (
  29. <>
  30. <InputAddress
  31. className='sudoInputAddress'
  32. isInput={true}
  33. label={t<string>('sudo key')}
  34. onChange={setSelected}
  35. type='all'
  36. value={selected}
  37. />
  38. <TxButton
  39. accountId={sudoKey}
  40. icon='sign-in-alt'
  41. isDisabled={!isMine || sudoKey === selected}
  42. label={t<string>('Reassign')}
  43. params={[selected]}
  44. tx='sudo.setKey'
  45. />
  46. </>
  47. )
  48. : (
  49. <Labelled
  50. className='ui--Dropdown sudoLabelled'
  51. label={t<string>('sudo key')}
  52. withLabel
  53. >
  54. <AddressMini value={sudoKey} />
  55. </Labelled>
  56. )
  57. }
  58. </section>
  59. {willLose && (
  60. <article className='warning padded'>
  61. <div>
  62. <Icon icon='exclamation-triangle' />
  63. {t<string>('You will no longer have sudo access')}
  64. </div>
  65. </article>
  66. )}
  67. </section>
  68. );
  69. }
  70. export default React.memo(styled(SetKey)`
  71. align-items: flex-end;
  72. justify-content: center;
  73. .summary {
  74. text-align: center;
  75. }
  76. .sudoInputAddress {
  77. margin: -0.25rem 0.5rem -0.25rem 0;
  78. }
  79. .sudoLabelled {
  80. align-items: center;
  81. }
  82. `);