accountIdentities.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import moment from 'moment-timezone';
  4. import {disconnectIdentity} from 'sentry/actionCreators/account';
  5. import {Alert} from 'sentry/components/alert';
  6. import Tag from 'sentry/components/badge/tag';
  7. import {Button} from 'sentry/components/button';
  8. import Confirm from 'sentry/components/confirm';
  9. import {DateTime} from 'sentry/components/dateTime';
  10. import EmptyMessage from 'sentry/components/emptyMessage';
  11. import LoadingError from 'sentry/components/loadingError';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import Panel from 'sentry/components/panels/panel';
  14. import PanelBody from 'sentry/components/panels/panelBody';
  15. import PanelHeader from 'sentry/components/panels/panelHeader';
  16. import PanelItem from 'sentry/components/panels/panelItem';
  17. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  18. import {t, tct} from 'sentry/locale';
  19. import {space} from 'sentry/styles/space';
  20. import type {UserIdentityConfig} from 'sentry/types/auth';
  21. import {UserIdentityCategory, UserIdentityStatus} from 'sentry/types/auth';
  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: any = [];
  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. isPending,
  122. isError,
  123. refetch,
  124. } = useApiQuery<UserIdentityConfig[]>([IDENTITIES_ENDPOINT], {
  125. staleTime: 0,
  126. });
  127. const appIdentities = useMemo(
  128. () =>
  129. identities
  130. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  131. .filter(identity => identity.category !== UserIdentityCategory.ORG_IDENTITY)
  132. .sort(itemOrder),
  133. [identities]
  134. );
  135. const orgIdentities = useMemo(
  136. () =>
  137. identities
  138. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  139. .filter(identity => identity.category === UserIdentityCategory.ORG_IDENTITY)
  140. .sort(itemOrder),
  141. [identities]
  142. );
  143. const handleDisconnect = useCallback(
  144. (identity: UserIdentityConfig) => {
  145. disconnectIdentity(identity, () => {
  146. setApiQueryData(queryClient, [IDENTITIES_ENDPOINT], oldData => {
  147. if (!Array.isArray(oldData)) {
  148. return oldData;
  149. }
  150. return oldData.filter(i => i.id !== identity.id);
  151. });
  152. });
  153. },
  154. [queryClient]
  155. );
  156. if (isPending) {
  157. return <LoadingIndicator />;
  158. }
  159. if (isError) {
  160. return <LoadingError onRetry={refetch} />;
  161. }
  162. return (
  163. <Fragment>
  164. <SentryDocumentTitle title={t('Identities')} />
  165. <SettingsPageHeader title="Identities" />
  166. <Panel>
  167. <PanelHeader>{t('Application Identities')}</PanelHeader>
  168. <PanelBody>
  169. {!appIdentities.length ? (
  170. <EmptyMessage>
  171. {t(
  172. 'There are no application identities associated with your Sentry account'
  173. )}
  174. </EmptyMessage>
  175. ) : (
  176. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  177. appIdentities.map(identity => (
  178. <IdentityItem
  179. key={identity.id}
  180. identity={identity}
  181. onDisconnect={handleDisconnect}
  182. />
  183. ))
  184. )}
  185. </PanelBody>
  186. </Panel>
  187. <Panel>
  188. <PanelHeader>{t('Organization Identities')}</PanelHeader>
  189. <PanelBody>
  190. {!orgIdentities.length ? (
  191. <EmptyMessage>
  192. {t(
  193. 'There are no organization identities associated with your Sentry account'
  194. )}
  195. </EmptyMessage>
  196. ) : (
  197. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  198. orgIdentities.map(identity => (
  199. <IdentityItem
  200. key={identity.id}
  201. identity={identity}
  202. onDisconnect={handleDisconnect}
  203. />
  204. ))
  205. )}
  206. </PanelBody>
  207. </Panel>
  208. </Fragment>
  209. );
  210. }
  211. const IdentityPanelItem = styled(PanelItem)`
  212. align-items: center;
  213. justify-content: space-between;
  214. `;
  215. const InternalContainer = styled('div')`
  216. display: flex;
  217. flex-direction: row;
  218. justify-content: center;
  219. `;
  220. const IdentityText = styled('div')<{isSingleLine?: boolean}>`
  221. height: 36px;
  222. display: flex;
  223. flex-direction: column;
  224. justify-content: ${p => (p.isSingleLine ? 'center' : 'space-between')};
  225. margin-left: ${space(1.5)};
  226. `;
  227. const IdentityName = styled('div')`
  228. font-weight: ${p => p.theme.fontWeightBold};
  229. `;
  230. const IdentityDateTime = styled(DateTime)`
  231. font-size: ${p => p.theme.fontSizeRelativeSmall};
  232. color: ${p => p.theme.subText};
  233. `;
  234. const TagWrapper = styled('div')`
  235. display: flex;
  236. align-items: center;
  237. justify-content: flex-start;
  238. flex-grow: 1;
  239. margin-right: ${space(1)};
  240. gap: ${space(0.75)};
  241. `;
  242. export default AccountIdentities;