project.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import Alert from 'sentry/components/alert';
  4. import ApiForm from 'sentry/components/forms/apiForm';
  5. import HiddenField from 'sentry/components/forms/fields/hiddenField';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import NarrowLayout from 'sentry/components/narrowLayout';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {t} from 'sentry/locale';
  10. import {useApiQuery} from 'sentry/utils/queryClient';
  11. import {decodeScalar} from 'sentry/utils/queryString';
  12. import {useParams} from 'sentry/utils/useParams';
  13. type RouteParams = {
  14. id: string;
  15. orgId: string;
  16. };
  17. type Props = RouteComponentProps<RouteParams, {}>;
  18. function UnsubscribeProject({location}: Props) {
  19. const signature = decodeScalar(location.query._);
  20. const params = useParams();
  21. return (
  22. <SentryDocumentTitle title={t('Unsubscribe')}>
  23. <NarrowLayout>
  24. <h3>{t('Unsubscribe')}</h3>
  25. <UnsubscribeBody
  26. signature={signature}
  27. orgSlug={params.orgId}
  28. issueId={params.id}
  29. />
  30. </NarrowLayout>
  31. </SentryDocumentTitle>
  32. );
  33. }
  34. interface UnsubscribeResponse {
  35. displayName: string;
  36. slug: string;
  37. type: string;
  38. viewUrl: string;
  39. }
  40. type BodyProps = {
  41. issueId: string;
  42. orgSlug: string;
  43. signature?: string;
  44. };
  45. function UnsubscribeBody({orgSlug, issueId, signature}: BodyProps) {
  46. const endpoint = `/organizations/${orgSlug}/unsubscribe/project/${issueId}/`;
  47. const {isLoading, isError, data} = useApiQuery<UnsubscribeResponse>(
  48. [endpoint, {query: {_: signature}}],
  49. {staleTime: 0}
  50. );
  51. if (isLoading) {
  52. return <LoadingIndicator />;
  53. }
  54. if (isError) {
  55. return (
  56. <Alert type="error">
  57. {t('There was an error loading unsubscribe data. Your link may have expired.')}
  58. </Alert>
  59. );
  60. }
  61. return (
  62. <Fragment>
  63. <p>
  64. <strong>{t('Account')}</strong>: {data.displayName}
  65. </p>
  66. <p>
  67. {t(
  68. 'You are about to unsubscribe from project notifications for the following project:'
  69. )}
  70. </p>
  71. <p>
  72. <strong>
  73. {orgSlug} / {data.slug}
  74. </strong>
  75. </p>
  76. <p>{t('You can subscribe to it again by going to your account settings.')}</p>
  77. <ApiForm
  78. apiEndpoint={`${endpoint}?_=${signature}`}
  79. apiMethod="POST"
  80. submitLabel={t('Unsubscribe')}
  81. cancelLabel={t('Cancel')}
  82. onCancel={() => {
  83. // Use window.location as we're going to an HTML view
  84. window.location.assign('/auth/login/');
  85. }}
  86. onSubmitSuccess={() => {
  87. // Use window.location as we're going to an HTML view
  88. window.location.assign('/auth/login/');
  89. }}
  90. initialData={{cancel: 1}}
  91. >
  92. <HiddenField name="cancel" />
  93. </ApiForm>
  94. </Fragment>
  95. );
  96. }
  97. export default UnsubscribeProject;