accountClose.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addLoadingMessage} from 'sentry/actionCreators/indicator';
  4. import {ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
  5. import {fetchOrganizations} from 'sentry/actionCreators/organizations';
  6. import {Alert} from 'sentry/components/alert';
  7. import {Button} from 'sentry/components/button';
  8. import HookOrDefault from 'sentry/components/hookOrDefault';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import Panel from 'sentry/components/panels/panel';
  11. import PanelAlert from 'sentry/components/panels/panelAlert';
  12. import PanelBody from 'sentry/components/panels/panelBody';
  13. import PanelHeader from 'sentry/components/panels/panelHeader';
  14. import PanelItem from 'sentry/components/panels/panelItem';
  15. import {t, tct} from 'sentry/locale';
  16. import {Organization, OrganizationSummary} from 'sentry/types';
  17. import useApi from 'sentry/utils/useApi';
  18. import {ConfirmAccountClose} from 'sentry/views/settings/account/confirmAccountClose';
  19. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  20. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  21. const BYE_URL = '/';
  22. const leaveRedirect = () => (window.location.href = BYE_URL);
  23. const Important = styled('div')`
  24. font-weight: bold;
  25. font-size: 1.2em;
  26. `;
  27. function GoodbyeModalContent({Header, Body, Footer}: ModalRenderProps) {
  28. return (
  29. <div>
  30. <Header>{t('Closing Account')}</Header>
  31. <Body>
  32. <TextBlock>
  33. {t('Your account has been deactivated and scheduled for removal.')}
  34. </TextBlock>
  35. <TextBlock>
  36. {t('Thanks for using Sentry! We hope to see you again soon!')}
  37. </TextBlock>
  38. </Body>
  39. <Footer>
  40. <Button href={BYE_URL}>{t('Goodbye')}</Button>
  41. </Footer>
  42. </div>
  43. );
  44. }
  45. type OwnedOrg = {
  46. organization: Organization;
  47. singleOwner: boolean;
  48. };
  49. function AccountClose() {
  50. const api = useApi();
  51. const [organizations, setOrganizations] = useState<OwnedOrg[]>([]);
  52. const [orgsToRemove, setOrgsToRemove] = useState<Set<string>>(new Set());
  53. const [isLoading, setIsLoading] = useState(true);
  54. // Load organizations from all regions.
  55. useEffect(() => {
  56. setIsLoading(true);
  57. fetchOrganizations(api, {owner: 1}).then((response: OwnedOrg[]) => {
  58. const singleOwnerOrgs = response
  59. .filter(item => item.singleOwner)
  60. .map(item => item.organization.slug);
  61. setOrgsToRemove(new Set(singleOwnerOrgs));
  62. setOrganizations(response);
  63. setIsLoading(false);
  64. });
  65. }, [api]);
  66. let leaveRedirectTimeout: number | undefined = undefined;
  67. useEffect(() => {
  68. // setup unmount callback
  69. return () => {
  70. window.clearTimeout(leaveRedirectTimeout);
  71. };
  72. }, [leaveRedirectTimeout]);
  73. const handleChange = (
  74. organization: OrganizationSummary,
  75. isSingle: boolean,
  76. event: React.ChangeEvent<HTMLInputElement>
  77. ) => {
  78. const checked = event.target.checked;
  79. // Can't unselect an org where you are the single owner
  80. if (isSingle) {
  81. return;
  82. }
  83. const slugSet = new Set(orgsToRemove);
  84. if (checked) {
  85. slugSet.add(organization.slug);
  86. } else {
  87. slugSet.delete(organization.slug);
  88. }
  89. setOrgsToRemove(slugSet);
  90. };
  91. const handleRemoveAccount = async () => {
  92. addLoadingMessage('Closing account\u2026');
  93. try {
  94. await api.requestPromise('/users/me/', {
  95. method: 'DELETE',
  96. data: {organizations: Array.from(orgsToRemove)},
  97. });
  98. openModal(GoodbyeModalContent, {
  99. onClose: leaveRedirect,
  100. });
  101. // Redirect after 10 seconds
  102. window.clearTimeout(leaveRedirectTimeout);
  103. leaveRedirectTimeout = window.setTimeout(leaveRedirect, 10000);
  104. } catch {
  105. addErrorMessage('Error closing account');
  106. }
  107. };
  108. const HookedCustomConfirmAccountClose = HookOrDefault({
  109. hookName: 'component:confirm-account-close',
  110. defaultComponent: props => <ConfirmAccountClose {...props} />,
  111. });
  112. if (isLoading) {
  113. return <LoadingIndicator />;
  114. }
  115. return (
  116. <div>
  117. <SettingsPageHeader title={t('Close Account')} />
  118. <TextBlock>
  119. {t('This will permanently remove all associated data for your user')}.
  120. </TextBlock>
  121. <Alert type="error" showIcon>
  122. <Important>
  123. {t('Closing your account is permanent and cannot be undone')}!
  124. </Important>
  125. </Alert>
  126. <Panel>
  127. <PanelHeader>{t('Remove the following organizations')}</PanelHeader>
  128. <PanelBody>
  129. <PanelAlert type="info">
  130. {t(
  131. 'Ownership will remain with other organization owners if an organization is not deleted.'
  132. )}
  133. <br />
  134. {tct(
  135. "Boxes which can't be unchecked mean that you are the only organization owner and the organization [strong:will be deleted].",
  136. {strong: <strong />}
  137. )}
  138. </PanelAlert>
  139. {organizations?.map(({organization, singleOwner}) => (
  140. <PanelItem key={organization.slug}>
  141. <label>
  142. <input
  143. style={{marginRight: 6}}
  144. type="checkbox"
  145. value={organization.slug}
  146. onChange={evt => handleChange(organization, singleOwner, evt)}
  147. name="organizations"
  148. checked={orgsToRemove.has(organization.slug)}
  149. disabled={singleOwner}
  150. />
  151. {organization.slug}
  152. </label>
  153. </PanelItem>
  154. ))}
  155. </PanelBody>
  156. </Panel>
  157. <HookedCustomConfirmAccountClose
  158. handleRemoveAccount={handleRemoveAccount}
  159. organizationSlugs={Array.from(orgsToRemove)}
  160. />
  161. </div>
  162. );
  163. }
  164. export default AccountClose;