accountIdentities.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import moment from 'moment';
  5. import {disconnectIdentity} from 'sentry/actionCreators/account';
  6. import Alert from 'sentry/components/alert';
  7. import Button from 'sentry/components/button';
  8. import Confirm from 'sentry/components/confirm';
  9. import DateTime from 'sentry/components/dateTime';
  10. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  11. import Tag from 'sentry/components/tag';
  12. import {t, tct} from 'sentry/locale';
  13. import space from 'sentry/styles/space';
  14. import {UserIdentityCategory, UserIdentityConfig, UserIdentityStatus} from 'sentry/types';
  15. import AsyncView from 'sentry/views/asyncView';
  16. import EmptyMessage from 'sentry/views/settings/components/emptyMessage';
  17. import IdentityIcon from 'sentry/views/settings/components/identityIcon';
  18. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  19. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  20. const ENDPOINT = '/users/me/user-identities/';
  21. type Props = RouteComponentProps<{}, {}>;
  22. type State = {
  23. identities: UserIdentityConfig[] | null;
  24. } & AsyncView['state'];
  25. class AccountIdentities extends AsyncView<Props, State> {
  26. getDefaultState() {
  27. return {
  28. ...super.getDefaultState(),
  29. identities: [],
  30. };
  31. }
  32. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  33. return [['identities', ENDPOINT]];
  34. }
  35. getTitle() {
  36. return t('Identities');
  37. }
  38. renderItem = (identity: UserIdentityConfig) => {
  39. return (
  40. <IdentityPanelItem key={`${identity.category}:${identity.id}`}>
  41. <InternalContainer>
  42. <IdentityIcon providerId={identity.provider.key} />
  43. <IdentityText isSingleLine={!identity.dateAdded}>
  44. <IdentityName>{identity.provider.name}</IdentityName>
  45. {identity.dateAdded && <IdentityDateTime date={moment(identity.dateAdded)} />}
  46. </IdentityText>
  47. </InternalContainer>
  48. <InternalContainer>
  49. <TagWrapper>
  50. {identity.category === UserIdentityCategory.SOCIAL_IDENTITY && (
  51. <Tag type="default">{t('Legacy')}</Tag>
  52. )}
  53. {identity.category !== UserIdentityCategory.ORG_IDENTITY && (
  54. <Tag type="default">
  55. {identity.isLogin ? t('Sign In') : t('Integration')}
  56. </Tag>
  57. )}
  58. {identity.organization && (
  59. <Tag type="highlight">{identity.organization.slug}</Tag>
  60. )}
  61. </TagWrapper>
  62. {this.renderButton(identity)}
  63. </InternalContainer>
  64. </IdentityPanelItem>
  65. );
  66. };
  67. renderButton(identity: UserIdentityConfig) {
  68. return identity.status === UserIdentityStatus.CAN_DISCONNECT ? (
  69. <Confirm
  70. onConfirm={() => this.handleDisconnect(identity)}
  71. priority="danger"
  72. confirmText={t('Disconnect')}
  73. message={
  74. <Fragment>
  75. <Alert type="error" showIcon>
  76. {tct('Disconnect Your [provider] Identity?', {
  77. provider: identity.provider.name,
  78. })}
  79. </Alert>
  80. <TextBlock>
  81. {identity.isLogin
  82. ? t(
  83. 'After disconnecting, you will need to use a password or another identity to sign in.'
  84. )
  85. : t("This action can't be undone.")}
  86. </TextBlock>
  87. </Fragment>
  88. }
  89. >
  90. <Button size="sm">{t('Disconnect')}</Button>
  91. </Confirm>
  92. ) : (
  93. <Button
  94. size="sm"
  95. disabled
  96. title={
  97. identity.status === UserIdentityStatus.NEEDED_FOR_GLOBAL_AUTH
  98. ? t(
  99. 'You need this identity to sign into your account. If you want to disconnect it, set a password first.'
  100. )
  101. : identity.status === UserIdentityStatus.NEEDED_FOR_ORG_AUTH
  102. ? t('You need this identity to access your organization.')
  103. : null
  104. }
  105. >
  106. {t('Disconnect')}
  107. </Button>
  108. );
  109. }
  110. handleDisconnect = (identity: UserIdentityConfig) => {
  111. disconnectIdentity(identity, () => this.reloadData());
  112. };
  113. itemOrder = (a: UserIdentityConfig, b: UserIdentityConfig) => {
  114. function categoryRank(c: UserIdentityConfig) {
  115. return [
  116. UserIdentityCategory.GLOBAL_IDENTITY,
  117. UserIdentityCategory.SOCIAL_IDENTITY,
  118. UserIdentityCategory.ORG_IDENTITY,
  119. ].indexOf(c.category);
  120. }
  121. if (a.provider.name !== b.provider.name) {
  122. return a.provider.name < b.provider.name ? -1 : 1;
  123. }
  124. if (a.category !== b.category) {
  125. return categoryRank(a) - categoryRank(b);
  126. }
  127. if ((a.organization?.name ?? '') !== (b.organization?.name ?? '')) {
  128. return (a.organization?.name ?? '') < (b.organization?.name ?? '') ? -1 : 1;
  129. }
  130. return 0;
  131. };
  132. renderBody() {
  133. const appIdentities = this.state.identities
  134. ?.filter(identity => identity.category !== UserIdentityCategory.ORG_IDENTITY)
  135. .sort(this.itemOrder);
  136. const orgIdentities = this.state.identities
  137. ?.filter(identity => identity.category === UserIdentityCategory.ORG_IDENTITY)
  138. .sort(this.itemOrder);
  139. return (
  140. <Fragment>
  141. <SettingsPageHeader title="Identities" />
  142. <Panel>
  143. <PanelHeader>{t('Application Identities')}</PanelHeader>
  144. <PanelBody>
  145. {!appIdentities?.length ? (
  146. <EmptyMessage>
  147. {t(
  148. 'There are no application identities associated with your Sentry account'
  149. )}
  150. </EmptyMessage>
  151. ) : (
  152. appIdentities.map(this.renderItem)
  153. )}
  154. </PanelBody>
  155. </Panel>
  156. <Panel>
  157. <PanelHeader>{t('Organization Identities')}</PanelHeader>
  158. <PanelBody>
  159. {!orgIdentities?.length ? (
  160. <EmptyMessage>
  161. {t(
  162. 'There are no organization identities associated with your Sentry account'
  163. )}
  164. </EmptyMessage>
  165. ) : (
  166. orgIdentities.map(this.renderItem)
  167. )}
  168. </PanelBody>
  169. </Panel>
  170. </Fragment>
  171. );
  172. }
  173. }
  174. const IdentityPanelItem = styled(PanelItem)`
  175. align-items: center;
  176. justify-content: space-between;
  177. `;
  178. const InternalContainer = styled('div')`
  179. display: flex;
  180. flex-direction: row;
  181. justify-content: center;
  182. `;
  183. const IdentityText = styled('div')<{isSingleLine?: boolean}>`
  184. height: 36px;
  185. display: flex;
  186. flex-direction: column;
  187. justify-content: ${p => (p.isSingleLine ? 'center' : 'space-between')};
  188. margin-left: ${space(1.5)};
  189. `;
  190. const IdentityName = styled('div')`
  191. font-weight: bold;
  192. `;
  193. const IdentityDateTime = styled(DateTime)`
  194. font-size: ${p => p.theme.fontSizeRelativeSmall};
  195. color: ${p => p.theme.subText};
  196. `;
  197. const TagWrapper = styled('div')`
  198. display: flex;
  199. align-items: center;
  200. justify-content: flex-start;
  201. flex-grow: 1;
  202. margin-right: ${space(1)};
  203. `;
  204. export default AccountIdentities;