productUnavailableCTA.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {useCallback, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button, LinkButton} from 'sentry/components/button';
  4. import {Alert} from 'sentry/components/core/alert';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {Organization} from 'sentry/types/organization';
  8. import useApi from 'sentry/utils/useApi';
  9. import {
  10. sendReplayOnboardRequest,
  11. sendUpgradeRequest,
  12. } from 'getsentry/actionCreators/upsell';
  13. import withSubscription from 'getsentry/components/withSubscription';
  14. import {useAM2UpsellModal} from 'getsentry/hooks/useAM2UpsellModal';
  15. import type {Subscription} from 'getsentry/types';
  16. import {PlanTier} from 'getsentry/types';
  17. import type {ProductUnavailableUpsellAlert} from 'getsentry/utils/trackGetsentryAnalytics';
  18. import trackGetsentryAnalytics from 'getsentry/utils/trackGetsentryAnalytics';
  19. function getUpdatePlanLabel({
  20. hasSessionReplay,
  21. hasPerformanceView,
  22. }: {
  23. hasPerformanceView: boolean;
  24. hasSessionReplay: boolean;
  25. }) {
  26. if (!hasSessionReplay && !hasPerformanceView) {
  27. return t(
  28. 'Update your organization to the latest version of its plan to use Performance and Session Replay.'
  29. );
  30. }
  31. if (!hasSessionReplay) {
  32. return t(
  33. 'Update your organization to the latest version of its plan to use Session Replay.'
  34. );
  35. }
  36. return t(
  37. 'Update your organization to the latest version of its plan to use Performance.'
  38. );
  39. }
  40. function getRequestUpdateLabel({
  41. hasSessionReplay,
  42. hasPerformanceView,
  43. }: {
  44. hasPerformanceView: boolean;
  45. hasSessionReplay: boolean;
  46. }) {
  47. if (!hasSessionReplay && !hasPerformanceView) {
  48. return t(
  49. 'To use Performance and Session Replay, request an owner in your organization to update its plan to the latest version.'
  50. );
  51. }
  52. if (!hasSessionReplay) {
  53. return t(
  54. 'To use Session Replay, request an owner in your organization to update its plan to the latest version.'
  55. );
  56. }
  57. return t(
  58. 'To use Performance, request an owner in your organization to update its plan to the latest version.'
  59. );
  60. }
  61. function RequestUpdateAlert({
  62. children,
  63. organization,
  64. isAncientPlan,
  65. hasPerformanceView,
  66. hasSessionReplay,
  67. }: {
  68. children: React.ReactNode;
  69. hasPerformanceView: boolean;
  70. hasSessionReplay: boolean;
  71. isAncientPlan: boolean;
  72. organization: Organization;
  73. }) {
  74. const api = useApi();
  75. const [loading, setLoading] = useState(false);
  76. const [requestSent, setRequestSent] = useState(false);
  77. const analyticsCommonProps: ProductUnavailableUpsellAlert & {
  78. organization: Organization;
  79. } = useMemo(
  80. () => ({
  81. organization,
  82. has_performance: hasPerformanceView,
  83. has_session_replay: hasSessionReplay,
  84. action: 'request_update',
  85. }),
  86. [organization, hasPerformanceView, hasSessionReplay]
  87. );
  88. useEffect(() => {
  89. trackGetsentryAnalytics(
  90. 'product_unavailable_upsell_alert.viewed',
  91. analyticsCommonProps
  92. );
  93. }, [analyticsCommonProps]);
  94. const handleClick = useCallback(async () => {
  95. setLoading(true);
  96. trackGetsentryAnalytics(
  97. 'product_unavailable_upsell_alert_button.clicked',
  98. analyticsCommonProps
  99. );
  100. if (isAncientPlan) {
  101. await sendUpgradeRequest({
  102. api,
  103. organization,
  104. handleSuccess: () => setRequestSent(true),
  105. });
  106. } else {
  107. await sendReplayOnboardRequest({
  108. api,
  109. orgSlug: organization.slug,
  110. currentPlan: 'am1-non-beta',
  111. onSuccess: () => setRequestSent(true),
  112. });
  113. }
  114. setLoading(false);
  115. }, [api, isAncientPlan, organization, analyticsCommonProps]);
  116. return (
  117. <AlertWithCustomMargin
  118. system
  119. type="info"
  120. trailingItems={
  121. <Button
  122. size="xs"
  123. onClick={handleClick}
  124. busy={loading}
  125. disabled={requestSent}
  126. title={requestSent ? t('Request sent!') : undefined}
  127. >
  128. {t('Request Update')}
  129. </Button>
  130. }
  131. >
  132. {children}
  133. </AlertWithCustomMargin>
  134. );
  135. }
  136. function UpdatePlanAlert({
  137. children,
  138. organization,
  139. subscription,
  140. isAncientPlan,
  141. canSelfServe,
  142. hasPerformanceView,
  143. hasSessionReplay,
  144. }: {
  145. canSelfServe: boolean;
  146. children: React.ReactNode;
  147. hasPerformanceView: boolean;
  148. hasSessionReplay: boolean;
  149. isAncientPlan: boolean;
  150. organization: Organization;
  151. subscription: Subscription;
  152. }) {
  153. const am2UpsellModal = useAM2UpsellModal({
  154. subscription,
  155. surface: 'replay_project_creation',
  156. onComplete: () => {
  157. window.location.reload();
  158. },
  159. });
  160. const analyticsCommonProps: ProductUnavailableUpsellAlert & {
  161. organization: Organization;
  162. } = useMemo(
  163. () => ({
  164. organization,
  165. has_performance: hasPerformanceView,
  166. has_session_replay: hasSessionReplay,
  167. action: isAncientPlan || !canSelfServe ? 'manage_subscription' : 'update_plan',
  168. }),
  169. [organization, hasPerformanceView, hasSessionReplay, isAncientPlan, canSelfServe]
  170. );
  171. useEffect(() => {
  172. trackGetsentryAnalytics(
  173. 'product_unavailable_upsell_alert.viewed',
  174. analyticsCommonProps
  175. );
  176. }, [analyticsCommonProps]);
  177. const handleClick = useCallback(() => {
  178. trackGetsentryAnalytics(
  179. 'product_unavailable_upsell_alert_button.clicked',
  180. analyticsCommonProps
  181. );
  182. if (isAncientPlan || !canSelfServe) {
  183. return;
  184. }
  185. am2UpsellModal.showModal();
  186. }, [analyticsCommonProps, canSelfServe, isAncientPlan, am2UpsellModal]);
  187. return (
  188. <AlertWithCustomMargin
  189. type="info"
  190. system
  191. trailingItems={
  192. isAncientPlan || !canSelfServe ? (
  193. <LinkButton
  194. size="xs"
  195. to={`/settings/${organization.slug}/billing/overview/?referrer=replay_onboard_mmx-cta`}
  196. onClick={handleClick}
  197. >
  198. {t('Manage Subscription')}
  199. </LinkButton>
  200. ) : (
  201. <Button size="xs" onClick={handleClick}>
  202. {t('Update Plan')}
  203. </Button>
  204. )
  205. }
  206. >
  207. {children}
  208. </AlertWithCustomMargin>
  209. );
  210. }
  211. function ProductUnavailableCTAContainer({
  212. organization,
  213. subscription,
  214. }: {
  215. organization: Organization;
  216. subscription: Subscription;
  217. }) {
  218. const hasSessionReplay = organization.features.includes('session-replay');
  219. const hasPerformanceView = organization.features.includes('performance-view');
  220. if (hasSessionReplay && hasPerformanceView) {
  221. return null;
  222. }
  223. // MM1 & MM2 plans have no direct update path into AM2, prices could be wildly different
  224. // Members can email owners requesting a plan upgrade and owners can manage subscription
  225. const isAncientPlan = [PlanTier.MM1, PlanTier.MM2].includes(
  226. subscription.planTier as PlanTier
  227. );
  228. const hasBillingAccess = organization.access?.includes('org:billing');
  229. const canSelfServe = subscription.canSelfServe;
  230. return hasBillingAccess ? (
  231. <UpdatePlanAlert
  232. subscription={subscription}
  233. organization={organization}
  234. isAncientPlan={isAncientPlan}
  235. canSelfServe={canSelfServe}
  236. hasSessionReplay={hasSessionReplay}
  237. hasPerformanceView={hasPerformanceView}
  238. >
  239. {getUpdatePlanLabel({hasSessionReplay, hasPerformanceView})}
  240. </UpdatePlanAlert>
  241. ) : (
  242. <RequestUpdateAlert
  243. hasSessionReplay={hasSessionReplay}
  244. hasPerformanceView={hasPerformanceView}
  245. organization={organization}
  246. isAncientPlan={isAncientPlan}
  247. >
  248. {getRequestUpdateLabel({hasSessionReplay, hasPerformanceView})}
  249. </RequestUpdateAlert>
  250. );
  251. }
  252. export const ProductUnavailableCTA = withSubscription(ProductUnavailableCTAContainer, {
  253. noLoader: true,
  254. });
  255. const AlertWithCustomMargin = styled(Alert)`
  256. margin: -${space(3)} -${space(4)} ${space(2)} -${space(4)};
  257. `;