accountIdentities.tsx 7.9 KB

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