onDemandBudgets.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import Panel from 'sentry/components/panels/panel';
  6. import PanelFooter from 'sentry/components/panels/panelFooter';
  7. import {t, tct} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {OnDemandBudgets} from 'getsentry/types';
  10. import {OnDemandBudgetMode} from 'getsentry/types';
  11. import {isDeveloperPlan} from 'getsentry/utils/billing';
  12. import StepHeader from 'getsentry/views/amCheckout/steps/stepHeader';
  13. import type {StepProps} from 'getsentry/views/amCheckout/types';
  14. import {getReservedPriceCents} from 'getsentry/views/amCheckout/utils';
  15. import OnDemandBudgetEdit from 'getsentry/views/onDemandBudgets/onDemandBudgetEdit';
  16. import {
  17. convertOnDemandBudget,
  18. getTotalBudget,
  19. parseOnDemandBudgetsFromSubscription,
  20. } from 'getsentry/views/onDemandBudgets/utils';
  21. type Props = StepProps;
  22. type State = {
  23. // Once the on-demand budget is updated, we no longer suggest a new default on-demand value,
  24. // regardless of whether there are further changes in the reserved value.
  25. // This is because if the user has seen and updated or clicked "Continue",
  26. // that is considered the final on-demand value unless the user updates the value themselves.
  27. isUpdated: boolean;
  28. };
  29. class OnDemandBudgetsStep extends Component<Props> {
  30. constructor(props: Props) {
  31. super(props);
  32. this.state = {isUpdated: false};
  33. }
  34. state: State;
  35. get title() {
  36. return t('On-Demand Budgets');
  37. }
  38. setBudgetMode = (nextMode: OnDemandBudgetMode) => {
  39. const {formData, subscription, onUpdate} = this.props;
  40. const currentOnDemandBudget = parseOnDemandBudgetsFromSubscription(subscription);
  41. const onDemandBudget = formData.onDemandBudget!;
  42. if (nextMode === onDemandBudget.budgetMode) {
  43. return;
  44. }
  45. if (nextMode === OnDemandBudgetMode.SHARED) {
  46. const nextOnDemandBudget = convertOnDemandBudget(currentOnDemandBudget, nextMode);
  47. onUpdate({
  48. onDemandBudget: nextOnDemandBudget,
  49. onDemandMaxSpend: getTotalBudget(nextOnDemandBudget),
  50. });
  51. return;
  52. }
  53. if (nextMode === OnDemandBudgetMode.PER_CATEGORY) {
  54. const nextOnDemandBudget = convertOnDemandBudget(currentOnDemandBudget, nextMode);
  55. onUpdate({
  56. onDemandBudget: nextOnDemandBudget,
  57. onDemandMaxSpend: getTotalBudget(nextOnDemandBudget),
  58. });
  59. return;
  60. }
  61. };
  62. renderBody = () => {
  63. const {subscription, activePlan, formData, onUpdate, organization} = this.props;
  64. let currentOnDemandBudget: OnDemandBudgets;
  65. let formOnDemandBudget: OnDemandBudgets;
  66. if (isDeveloperPlan(subscription.planDetails) && !this.state.isUpdated) {
  67. currentOnDemandBudget = {
  68. budgetMode: OnDemandBudgetMode.SHARED,
  69. sharedMaxBudget:
  70. getReservedPriceCents({plan: activePlan, reserved: formData.reserved}) * 3,
  71. };
  72. formOnDemandBudget = currentOnDemandBudget;
  73. this.setState({isUpdated: true});
  74. onUpdate({
  75. onDemandBudget: formOnDemandBudget,
  76. onDemandMaxSpend: formOnDemandBudget.sharedMaxBudget,
  77. });
  78. } else {
  79. currentOnDemandBudget = parseOnDemandBudgetsFromSubscription(subscription);
  80. formOnDemandBudget = formData.onDemandBudget!;
  81. }
  82. const onDemandEnabled = getTotalBudget(currentOnDemandBudget) > 0;
  83. const onDemandSupported = activePlan.allowOnDemand && subscription.supportsOnDemand;
  84. return (
  85. <OnDemandBudgetEdit
  86. onDemandEnabled={onDemandEnabled}
  87. onDemandSupported={onDemandSupported}
  88. currentBudgetMode={currentOnDemandBudget.budgetMode}
  89. onDemandBudget={formOnDemandBudget}
  90. setBudgetMode={this.setBudgetMode}
  91. setOnDemandBudget={(onDemandBudget: OnDemandBudgets) => {
  92. this.setState({isUpdated: true});
  93. onUpdate({
  94. onDemandBudget,
  95. onDemandMaxSpend: getTotalBudget(onDemandBudget),
  96. });
  97. }}
  98. activePlan={activePlan}
  99. organization={organization}
  100. subscription={subscription}
  101. />
  102. );
  103. };
  104. renderFooter = () => {
  105. const {stepNumber, onCompleteStep} = this.props;
  106. return (
  107. <StepFooter data-test-id={this.title}>
  108. <div>
  109. {tct('Need more info? [link:See on-demand pricing chart]', {
  110. link: (
  111. <ExternalLink href="https://docs.sentry.io/pricing/legacy-pricing/#per-category-pricing" />
  112. ),
  113. })}
  114. </div>
  115. <Button priority="primary" onClick={() => onCompleteStep(stepNumber)}>
  116. {t('Continue')}
  117. </Button>
  118. </StepFooter>
  119. );
  120. };
  121. render() {
  122. const {isActive, stepNumber, isCompleted, onEdit} = this.props;
  123. return (
  124. <Panel>
  125. <StepHeader
  126. canSkip
  127. title={this.title}
  128. isActive={isActive}
  129. stepNumber={stepNumber}
  130. isCompleted={isCompleted}
  131. onEdit={onEdit}
  132. />
  133. {isActive && this.renderBody()}
  134. {isActive && this.renderFooter()}
  135. </Panel>
  136. );
  137. }
  138. }
  139. const StepFooter = styled(PanelFooter)`
  140. padding: ${space(2)};
  141. display: grid;
  142. grid-template-columns: auto max-content;
  143. gap: ${space(1)};
  144. align-items: center;
  145. `;
  146. export default OnDemandBudgetsStep;