authTokenRow.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import Confirm from 'sentry/components/confirm';
  5. import Link from 'sentry/components/links/link';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import Placeholder from 'sentry/components/placeholder';
  8. import TimeSince from 'sentry/components/timeSince';
  9. import {Tooltip} from 'sentry/components/tooltip';
  10. import {IconSubtract} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {Organization, OrgAuthToken, Project} from 'sentry/types';
  14. import getDynamicText from 'sentry/utils/getDynamicText';
  15. import {tokenPreview} from 'sentry/views/settings/organizationAuthTokens';
  16. function LastUsed({
  17. organization,
  18. dateLastUsed,
  19. projectLastUsed,
  20. }: {
  21. organization: Organization;
  22. dateLastUsed?: Date;
  23. projectLastUsed?: Project;
  24. }) {
  25. if (dateLastUsed && projectLastUsed) {
  26. return (
  27. <Fragment>
  28. {tct('[date] in project [project]', {
  29. date: (
  30. <TimeSince
  31. date={getDynamicText({
  32. value: dateLastUsed,
  33. fixed: new Date(1508208080000), // National Pasta Day
  34. })}
  35. />
  36. ),
  37. project: (
  38. <Link to={`/settings/${organization.slug}/${projectLastUsed.slug}/`}>
  39. {projectLastUsed.name}
  40. </Link>
  41. ),
  42. })}
  43. </Fragment>
  44. );
  45. }
  46. if (dateLastUsed) {
  47. return (
  48. <Fragment>
  49. <TimeSince
  50. date={getDynamicText({
  51. value: dateLastUsed,
  52. fixed: new Date(1508208080000), // National Pasta Day
  53. })}
  54. />
  55. </Fragment>
  56. );
  57. }
  58. if (projectLastUsed) {
  59. return (
  60. <Fragment>
  61. {tct('in project [project]', {
  62. project: (
  63. <Link to={`/settings/${organization.slug}/${projectLastUsed.slug}/`}>
  64. {projectLastUsed.name}
  65. </Link>
  66. ),
  67. })}
  68. </Fragment>
  69. );
  70. }
  71. return <NeverUsed>{t('never used')}</NeverUsed>;
  72. }
  73. export function OrganizationAuthTokensAuthTokenRow({
  74. organization,
  75. isRevoking,
  76. token,
  77. revokeToken,
  78. projectLastUsed,
  79. isProjectLoading,
  80. }: {
  81. isRevoking: boolean;
  82. organization: Organization;
  83. token: OrgAuthToken;
  84. isProjectLoading?: boolean;
  85. projectLastUsed?: Project;
  86. revokeToken?: (token: OrgAuthToken) => void;
  87. }) {
  88. return (
  89. <Fragment>
  90. <div>
  91. <Label>
  92. <Link to={`/settings/${organization.slug}/auth-tokens/${token.id}/`}>
  93. {token.name}
  94. </Link>
  95. </Label>
  96. {token.tokenLastCharacters && (
  97. <TokenPreview aria-label={t('Token preview')}>
  98. {tokenPreview(
  99. getDynamicText({
  100. value: token.tokenLastCharacters,
  101. fixed: 'ABCD',
  102. })
  103. )}
  104. </TokenPreview>
  105. )}
  106. </div>
  107. <LastUsedDate>
  108. {isProjectLoading ? (
  109. <Placeholder height="1.25em" />
  110. ) : (
  111. <LastUsed
  112. dateLastUsed={token.dateLastUsed}
  113. projectLastUsed={projectLastUsed}
  114. organization={organization}
  115. />
  116. )}
  117. </LastUsedDate>
  118. <Actions>
  119. <Tooltip
  120. title={t(
  121. 'You must be an organization owner, manager or admin to revoke a token.'
  122. )}
  123. disabled={!!revokeToken}
  124. >
  125. <Confirm
  126. disabled={!revokeToken || isRevoking}
  127. onConfirm={revokeToken ? () => revokeToken(token) : undefined}
  128. message={t(
  129. 'Are you sure you want to revoke this token? The token will not be usable anymore, and this cannot be undone.'
  130. )}
  131. >
  132. <Button
  133. size="sm"
  134. disabled={isRevoking || !revokeToken}
  135. aria-label={t('Revoke %s', token.name)}
  136. icon={
  137. isRevoking ? (
  138. <LoadingIndicator mini />
  139. ) : (
  140. <IconSubtract isCircled size="xs" />
  141. )
  142. }
  143. >
  144. {t('Revoke')}
  145. </Button>
  146. </Confirm>
  147. </Tooltip>
  148. </Actions>
  149. </Fragment>
  150. );
  151. }
  152. const Label = styled('div')``;
  153. const Actions = styled('div')`
  154. display: flex;
  155. justify-content: flex-end;
  156. `;
  157. const LastUsedDate = styled('div')`
  158. display: flex;
  159. align-items: center;
  160. gap: ${space(0.5)};
  161. `;
  162. const NeverUsed = styled('div')`
  163. color: ${p => p.theme.gray300};
  164. `;
  165. const TokenPreview = styled('div')`
  166. color: ${p => p.theme.gray300};
  167. `;