accountIdentities.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 Tag from 'sentry/components/badge/tag';
  6. import {Button} from 'sentry/components/button';
  7. import Confirm from 'sentry/components/confirm';
  8. import {Alert} from 'sentry/components/core/alert';
  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.Container>
  80. <Alert type="error" showIcon>
  81. {tct('Disconnect Your [provider] Identity?', {
  82. provider: identity.provider.name,
  83. })}
  84. </Alert>
  85. </Alert.Container>
  86. <TextBlock>
  87. {identity.isLogin
  88. ? t(
  89. 'After disconnecting, you will need to use a password or another identity to sign in.'
  90. )
  91. : t("This action can't be undone.")}
  92. </TextBlock>
  93. </Fragment>
  94. }
  95. >
  96. <Button size="sm">{t('Disconnect')}</Button>
  97. </Confirm>
  98. ) : (
  99. <Button
  100. size="sm"
  101. disabled
  102. title={
  103. identity.status === UserIdentityStatus.NEEDED_FOR_GLOBAL_AUTH
  104. ? t(
  105. 'You need this identity to sign into your account. If you want to disconnect it, set a password first.'
  106. )
  107. : identity.status === UserIdentityStatus.NEEDED_FOR_ORG_AUTH
  108. ? t('You need this identity to access your organization.')
  109. : null
  110. }
  111. >
  112. {t('Disconnect')}
  113. </Button>
  114. )}
  115. </InternalContainer>
  116. </IdentityPanelItem>
  117. );
  118. }
  119. function AccountIdentities() {
  120. const queryClient = useQueryClient();
  121. const {
  122. data: identities = EMPTY_ARRAY,
  123. isPending,
  124. isError,
  125. refetch,
  126. } = useApiQuery<UserIdentityConfig[]>([IDENTITIES_ENDPOINT], {
  127. staleTime: 0,
  128. });
  129. const appIdentities = useMemo(
  130. () =>
  131. identities
  132. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  133. .filter(identity => identity.category !== UserIdentityCategory.ORG_IDENTITY)
  134. .sort(itemOrder),
  135. [identities]
  136. );
  137. const orgIdentities = useMemo(
  138. () =>
  139. identities
  140. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  141. .filter(identity => identity.category === UserIdentityCategory.ORG_IDENTITY)
  142. .sort(itemOrder),
  143. [identities]
  144. );
  145. const handleDisconnect = useCallback(
  146. (identity: UserIdentityConfig) => {
  147. disconnectIdentity(identity, () => {
  148. setApiQueryData(queryClient, [IDENTITIES_ENDPOINT], oldData => {
  149. if (!Array.isArray(oldData)) {
  150. return oldData;
  151. }
  152. return oldData.filter(i => i.id !== identity.id);
  153. });
  154. });
  155. },
  156. [queryClient]
  157. );
  158. if (isPending) {
  159. return <LoadingIndicator />;
  160. }
  161. if (isError) {
  162. return <LoadingError onRetry={refetch} />;
  163. }
  164. return (
  165. <Fragment>
  166. <SentryDocumentTitle title={t('Identities')} />
  167. <SettingsPageHeader title="Identities" />
  168. <Panel>
  169. <PanelHeader>{t('Application Identities')}</PanelHeader>
  170. <PanelBody>
  171. {!appIdentities.length ? (
  172. <EmptyMessage>
  173. {t(
  174. 'There are no application identities associated with your Sentry account'
  175. )}
  176. </EmptyMessage>
  177. ) : (
  178. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  179. appIdentities.map(identity => (
  180. <IdentityItem
  181. key={identity.id}
  182. identity={identity}
  183. onDisconnect={handleDisconnect}
  184. />
  185. ))
  186. )}
  187. </PanelBody>
  188. </Panel>
  189. <Panel>
  190. <PanelHeader>{t('Organization Identities')}</PanelHeader>
  191. <PanelBody>
  192. {!orgIdentities.length ? (
  193. <EmptyMessage>
  194. {t(
  195. 'There are no organization identities associated with your Sentry account'
  196. )}
  197. </EmptyMessage>
  198. ) : (
  199. // @ts-expect-error TS(7006): Parameter 'identity' implicitly has an 'any' type.
  200. orgIdentities.map(identity => (
  201. <IdentityItem
  202. key={identity.id}
  203. identity={identity}
  204. onDisconnect={handleDisconnect}
  205. />
  206. ))
  207. )}
  208. </PanelBody>
  209. </Panel>
  210. </Fragment>
  211. );
  212. }
  213. const IdentityPanelItem = styled(PanelItem)`
  214. align-items: center;
  215. justify-content: space-between;
  216. `;
  217. const InternalContainer = styled('div')`
  218. display: flex;
  219. flex-direction: row;
  220. justify-content: center;
  221. `;
  222. const IdentityText = styled('div')<{isSingleLine?: boolean}>`
  223. height: 36px;
  224. display: flex;
  225. flex-direction: column;
  226. justify-content: ${p => (p.isSingleLine ? 'center' : 'space-between')};
  227. margin-left: ${space(1.5)};
  228. `;
  229. const IdentityName = styled('div')`
  230. font-weight: ${p => p.theme.fontWeightBold};
  231. `;
  232. const IdentityDateTime = styled(DateTime)`
  233. font-size: ${p => p.theme.fontSizeRelativeSmall};
  234. color: ${p => p.theme.subText};
  235. `;
  236. const TagWrapper = styled('div')`
  237. display: flex;
  238. align-items: center;
  239. justify-content: flex-start;
  240. flex-grow: 1;
  241. margin-right: ${space(1)};
  242. gap: ${space(0.75)};
  243. `;
  244. export default AccountIdentities;