addGiftEventsAction.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import {Component} from 'react';
  2. import TextField from 'sentry/components/forms/fields/textField';
  3. import {DataCategory} from 'sentry/types/core';
  4. import type {
  5. AdminConfirmParams,
  6. AdminConfirmRenderProps,
  7. } from 'admin/components/adminConfirmationModal';
  8. import {MAX_ADMIN_CATEGORY_GIFTS} from 'getsentry/constants';
  9. import type {Subscription} from 'getsentry/types';
  10. import {getPlanCategoryName} from 'getsentry/utils/dataCategory';
  11. /** @internal exported for tests only */
  12. export const FREE_EVENTS_KEYS = {
  13. [DataCategory.ERRORS]: 'addFreeErrors',
  14. [DataCategory.TRANSACTIONS]: 'addFreeTransactions',
  15. [DataCategory.REPLAYS]: 'addFreeReplays',
  16. [DataCategory.ATTACHMENTS]: 'addFreeAttachments',
  17. [DataCategory.MONITOR_SEATS]: 'addFreeMonitorSeats',
  18. [DataCategory.UPTIME]: 'addFreeUptime',
  19. [DataCategory.SPANS]: 'addFreeSpans',
  20. [DataCategory.SPANS_INDEXED]: 'addFreeSpansIndexed',
  21. [DataCategory.PROFILE_DURATION]: 'addFreeProfileDuration',
  22. };
  23. /**
  24. * Used so form can show "How many errors in multiples of 1,000s? (50 is 50,000 errors)"
  25. * and calculate total based on the multiplier
  26. */
  27. const DISPLAY_FREE_EVENTS_MULTIPLE = {
  28. [DataCategory.ERRORS]: 1_000,
  29. [DataCategory.TRANSACTIONS]: 1_000,
  30. [DataCategory.REPLAYS]: 1,
  31. [DataCategory.ATTACHMENTS]: 1, // GB
  32. [DataCategory.MONITOR_SEATS]: 1,
  33. [DataCategory.UPTIME]: 1,
  34. [DataCategory.SPANS]: 100_000,
  35. [DataCategory.SPANS_INDEXED]: 100_000,
  36. [DataCategory.PROFILE_DURATION]: 1, // hours
  37. };
  38. type Props = AdminConfirmRenderProps & {
  39. dataCategory: DataCategory;
  40. subscription: Subscription;
  41. };
  42. type State = {
  43. freeEvents?: number;
  44. };
  45. /**
  46. * Rendered as part of a openAdminConfirmModal call
  47. */
  48. class AddGiftEventsAction extends Component<Props, State> {
  49. state: State = {
  50. freeEvents: undefined,
  51. };
  52. componentDidMount() {
  53. this.props.setConfirmCallback(this.handleConfirm);
  54. this.props.disableConfirmButton(true);
  55. }
  56. handleChange = (value: string) => {
  57. const freeEvents = this.coerceValue(value);
  58. this.props.disableConfirmButton(freeEvents === 0);
  59. this.setState({freeEvents});
  60. };
  61. coerceValue(value: string) {
  62. const {dataCategory} = this.props;
  63. const intValue = parseInt(value, 10);
  64. const maxValue =
  65. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  66. MAX_ADMIN_CATEGORY_GIFTS[dataCategory] / DISPLAY_FREE_EVENTS_MULTIPLE[dataCategory];
  67. if (isNaN(intValue) || intValue < 0) {
  68. return undefined;
  69. }
  70. return intValue > maxValue ? maxValue : intValue;
  71. }
  72. handleConfirm = (params: AdminConfirmParams) => {
  73. const {onConfirm, dataCategory} = this.props;
  74. const freeEvents = this.calculatedTotal;
  75. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  76. const freeEventsKey = FREE_EVENTS_KEYS[dataCategory];
  77. onConfirm?.({[freeEventsKey]: freeEvents, ...params});
  78. this.resetValue();
  79. };
  80. resetValue = () => {
  81. this.setState({freeEvents: 0});
  82. };
  83. get calculatedTotal() {
  84. const {dataCategory} = this.props;
  85. const {freeEvents} = this.state;
  86. if (!freeEvents) {
  87. return 0;
  88. }
  89. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  90. return freeEvents * DISPLAY_FREE_EVENTS_MULTIPLE[dataCategory];
  91. }
  92. render() {
  93. const {dataCategory, subscription} = this.props;
  94. const {freeEvents} = this.state;
  95. function getlabel() {
  96. if (dataCategory === DataCategory.ATTACHMENTS) {
  97. return 'How many attachments in GB?';
  98. }
  99. if (dataCategory === DataCategory.PROFILE_DURATION) {
  100. return 'How many profile hours?';
  101. }
  102. const categoryName = getPlanCategoryName({
  103. plan: subscription.planDetails,
  104. category: dataCategory,
  105. capitalize: false,
  106. });
  107. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  108. const multiplier = DISPLAY_FREE_EVENTS_MULTIPLE[dataCategory];
  109. const addToMessage =
  110. multiplier > 1
  111. ? ` in multiples of ${multiplier.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')}s?`
  112. : '?';
  113. return `How many ${categoryName}${addToMessage} (50 is ${(50 * multiplier).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')} ${categoryName})`;
  114. }
  115. const total = this.calculatedTotal.toLocaleString();
  116. function getHelp() {
  117. let postFix = '';
  118. if (dataCategory === DataCategory.PROFILE_DURATION) {
  119. if (total === '1') {
  120. postFix = ' hour';
  121. } else {
  122. postFix = ' hours';
  123. }
  124. }
  125. if (dataCategory === DataCategory.ATTACHMENTS) {
  126. postFix = ' GB';
  127. }
  128. return `Total: ${total}${postFix}`;
  129. }
  130. return (
  131. <TextField
  132. autoFocus
  133. inline={false}
  134. stacked
  135. flexibleControlStateSize
  136. data-test-id={`num-free-${dataCategory}`}
  137. label={getlabel()}
  138. help={getHelp()}
  139. name={dataCategory}
  140. inputMode="numeric"
  141. pattern="[0-9]*"
  142. maxLength={dataCategory === DataCategory.REPLAYS ? 7 : 5}
  143. value={freeEvents && !isNaN(freeEvents) ? freeEvents.toString() : ''}
  144. onChange={this.handleChange}
  145. />
  146. );
  147. }
  148. }
  149. export default AddGiftEventsAction;