u2fEnrolledDetails.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import Button from 'sentry/components/button';
  4. import Confirm from 'sentry/components/confirm';
  5. import DateTime from 'sentry/components/dateTime';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  7. import Input from 'sentry/components/input';
  8. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  9. import Tooltip from 'sentry/components/tooltip';
  10. import {IconClose, IconDelete} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import ConfirmHeader from 'sentry/views/settings/account/accountSecurity/components/confirmHeader';
  14. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  15. const U2fEnrolledDetails = props => {
  16. const {className, isEnrolled, devices, id, onRemoveU2fDevice, onRenameU2fDevice} =
  17. props;
  18. if (id !== 'u2f' || !isEnrolled) {
  19. return null;
  20. }
  21. const hasDevices = devices?.length;
  22. // Note Tooltip doesn't work because of bootstrap(?) pointer events for disabled buttons
  23. const isLastDevice = hasDevices === 1;
  24. return (
  25. <Panel className={className}>
  26. <PanelHeader>{t('Device name')}</PanelHeader>
  27. <PanelBody>
  28. {!hasDevices && (
  29. <EmptyMessage>{t('You have not added any U2F devices')}</EmptyMessage>
  30. )}
  31. {hasDevices &&
  32. devices?.map((device, i) => (
  33. <Device
  34. key={i}
  35. device={device}
  36. isLastDevice={isLastDevice}
  37. onRenameU2fDevice={onRenameU2fDevice}
  38. onRemoveU2fDevice={onRemoveU2fDevice}
  39. />
  40. ))}
  41. <AddAnotherPanelItem>
  42. <Button type="button" to="/settings/account/security/mfa/u2f/enroll/" size="sm">
  43. {t('Add Another Device')}
  44. </Button>
  45. </AddAnotherPanelItem>
  46. </PanelBody>
  47. </Panel>
  48. );
  49. };
  50. const Device = props => {
  51. const {device, isLastDevice, onRenameU2fDevice, onRemoveU2fDevice} = props;
  52. const [deviceName, setDeviceName] = useState(device.name);
  53. const [isEditing, setEditting] = useState(false);
  54. if (!isEditing) {
  55. return (
  56. <DevicePanelItem key={device.name}>
  57. <DeviceInformation>
  58. <Name>{device.name}</Name>
  59. <FadedDateTime date={device.timestamp} />
  60. </DeviceInformation>
  61. <Actions>
  62. <Button size="sm" onClick={() => setEditting(true)}>
  63. {t('Rename Device')}
  64. </Button>
  65. </Actions>
  66. <Actions>
  67. <Confirm
  68. onConfirm={() => onRemoveU2fDevice(device)}
  69. disabled={isLastDevice}
  70. message={
  71. <Fragment>
  72. <ConfirmHeader>{t('Do you want to remove U2F device?')}</ConfirmHeader>
  73. <TextBlock>
  74. {t('Are you sure you want to remove the U2F device "%s"?', device.name)}
  75. </TextBlock>
  76. </Fragment>
  77. }
  78. >
  79. <Button size="sm" priority="danger">
  80. <Tooltip
  81. disabled={!isLastDevice}
  82. title={t('Can not remove last U2F device')}
  83. >
  84. <IconDelete size="xs" />
  85. </Tooltip>
  86. </Button>
  87. </Confirm>
  88. </Actions>
  89. </DevicePanelItem>
  90. );
  91. }
  92. return (
  93. <DevicePanelItem key={device.name}>
  94. <DeviceInformation>
  95. <DeviceNameInput
  96. type="text"
  97. value={deviceName}
  98. onChange={e => {
  99. setDeviceName(e.target.value);
  100. }}
  101. />
  102. <FadedDateTime date={device.timestamp} />
  103. </DeviceInformation>
  104. <Actions>
  105. <Button
  106. priority="primary"
  107. size="sm"
  108. onClick={() => {
  109. onRenameU2fDevice(device, deviceName);
  110. setEditting(false);
  111. }}
  112. >
  113. Rename Device
  114. </Button>
  115. </Actions>
  116. <Actions>
  117. <Button
  118. size="sm"
  119. title="Cancel rename"
  120. onClick={() => {
  121. setDeviceName(device.name);
  122. setEditting(false);
  123. }}
  124. >
  125. <IconClose size="xs" />
  126. </Button>
  127. </Actions>
  128. </DevicePanelItem>
  129. );
  130. };
  131. const DeviceNameInput = styled(Input)`
  132. width: 50%;
  133. margin-right: ${space(2)};
  134. `;
  135. const DevicePanelItem = styled(PanelItem)`
  136. padding: 0;
  137. `;
  138. const DeviceInformation = styled('div')`
  139. display: flex;
  140. align-items: center;
  141. justify-content: space-between;
  142. flex: 1 1;
  143. height: 72px;
  144. padding: ${space(2)};
  145. padding-right: 0;
  146. `;
  147. const FadedDateTime = styled(DateTime)`
  148. font-size: ${p => p.theme.fontSizeRelativeSmall};
  149. opacity: 0.6;
  150. `;
  151. const Name = styled('div')`
  152. flex: 1;
  153. `;
  154. const Actions = styled('div')`
  155. margin: ${space(2)};
  156. `;
  157. const AddAnotherPanelItem = styled(PanelItem)`
  158. justify-content: flex-end;
  159. padding: ${space(2)};
  160. `;
  161. export default styled(U2fEnrolledDetails)`
  162. margin-top: ${space(4)};
  163. `;