index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {useCallback, useEffect, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {PromptData, promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  4. import Feature from 'sentry/components/acl/feature';
  5. import Alert from 'sentry/components/alert';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import {t} from 'sentry/locale';
  8. import {PageContent} from 'sentry/styles/organization';
  9. import {Organization, RequestState} from 'sentry/types';
  10. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  11. import useApi from 'sentry/utils/useApi';
  12. import withOrganization from 'sentry/utils/withOrganization';
  13. import ProfilingOnboarding from './profilingOnboarding';
  14. function renderNoAccess() {
  15. return (
  16. <PageContent>
  17. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  18. </PageContent>
  19. );
  20. }
  21. function shouldShowProfilingOnboarding(state: RequestState<PromptData>): boolean {
  22. if (state.type === 'resolved') {
  23. return typeof state.data?.dismissedTime !== 'number';
  24. }
  25. return false;
  26. }
  27. type Props = {
  28. children: React.ReactChildren;
  29. organization: Organization;
  30. };
  31. function ProfilingContainer({organization, children}: Props) {
  32. const api = useApi();
  33. const [requestState, setRequestState] = useState<RequestState<PromptData>>({
  34. type: 'initial',
  35. });
  36. // Fetch prompt data and see if we need to show the onboarding.
  37. useEffect(() => {
  38. setRequestState({type: 'loading'});
  39. promptsCheck(api, {
  40. organizationId: organization.id,
  41. feature: 'profiling_onboarding',
  42. })
  43. .then(data => {
  44. setRequestState({type: 'resolved', data});
  45. })
  46. .catch(e => {
  47. Sentry.captureException(e);
  48. setRequestState({type: 'errored', error: t('Error: Unable to load prompt data')});
  49. });
  50. }, [api, organization]);
  51. // Eagerly update state and update check
  52. const dismissPrompt = useCallback(
  53. (status: 'done' | 'dismissed') => {
  54. setRequestState({type: 'resolved', data: {dismissedTime: Date.now()}});
  55. trackAdvancedAnalyticsEvent('profiling_views.onboarding_action', {
  56. action: status,
  57. organization,
  58. });
  59. return promptsUpdate(api, {
  60. feature: 'profiling_onboarding',
  61. organizationId: organization.id,
  62. // This will always send dismissed, becuse we dont actually
  63. // care about the snooze mechanism. It would be awkward to suddenly
  64. // creep a full page into view.
  65. status: 'dismissed',
  66. });
  67. },
  68. [organization, api]
  69. );
  70. const handleDone = useCallback(() => {
  71. dismissPrompt('done');
  72. }, [dismissPrompt]);
  73. const handleDismiss = useCallback(() => {
  74. dismissPrompt('dismissed');
  75. }, [dismissPrompt]);
  76. return (
  77. <Feature
  78. hookName="feature-disabled:profiling-page"
  79. features={['profiling']}
  80. organization={organization}
  81. renderDisabled={renderNoAccess}
  82. >
  83. {requestState.type === 'loading' ? (
  84. <LoadingIndicator />
  85. ) : shouldShowProfilingOnboarding(requestState) ? (
  86. <ProfilingOnboarding
  87. organization={organization}
  88. onDismissClick={handleDismiss}
  89. onDoneClick={handleDone}
  90. />
  91. ) : (
  92. children
  93. )}
  94. </Feature>
  95. );
  96. }
  97. export default withOrganization(ProfilingContainer);