accountIdentities.tsx 8.0 KB

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