createProjectsFooter.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {Fragment, useCallback, useContext} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {motion} from 'framer-motion';
  5. import {PlatformIcon} from 'platformicons';
  6. import {
  7. addErrorMessage,
  8. addLoadingMessage,
  9. clearIndicators,
  10. } from 'sentry/actionCreators/indicator';
  11. import {openModal} from 'sentry/actionCreators/modal';
  12. import {createProject} from 'sentry/actionCreators/projects';
  13. import {Button} from 'sentry/components/button';
  14. import {SupportedLanguages} from 'sentry/components/onboarding/frameworkSuggestionModal';
  15. import {OnboardingContext} from 'sentry/components/onboarding/onboardingContext';
  16. import {t} from 'sentry/locale';
  17. import ProjectsStore from 'sentry/stores/projectsStore';
  18. import {space} from 'sentry/styles/space';
  19. import type {OnboardingSelectedSDK, Organization, Project} from 'sentry/types';
  20. import {OnboardingProjectStatus} from 'sentry/types';
  21. import {trackAnalytics} from 'sentry/utils/analytics';
  22. import testableTransition from 'sentry/utils/testableTransition';
  23. import useApi from 'sentry/utils/useApi';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import {useTeams} from 'sentry/utils/useTeams';
  26. import GenericFooter from './genericFooter';
  27. type Props = {
  28. clearPlatform: () => void;
  29. genSkipOnboardingLink: () => React.ReactNode;
  30. onComplete: (selectedPlatform: OnboardingSelectedSDK) => void;
  31. organization: Organization;
  32. selectedPlatform?: OnboardingSelectedSDK;
  33. };
  34. export function CreateProjectsFooter({
  35. organization,
  36. selectedPlatform,
  37. onComplete,
  38. genSkipOnboardingLink,
  39. clearPlatform,
  40. }: Props) {
  41. const frameworkSelectionEnabled = !!organization?.features.includes(
  42. 'onboarding-sdk-selection'
  43. );
  44. const api = useApi();
  45. const {teams} = useTeams();
  46. const onboardingContext = useContext(OnboardingContext);
  47. const {projects} = useProjects();
  48. const createPlatformProject = useCallback(
  49. async (selectedFramework?: OnboardingSelectedSDK) => {
  50. if (!selectedPlatform) {
  51. return;
  52. }
  53. let createProjectForPlatform: OnboardingSelectedSDK | undefined = undefined;
  54. if (selectedFramework) {
  55. createProjectForPlatform = projects.find(p => p.slug === selectedFramework.key)
  56. ? undefined
  57. : selectedFramework;
  58. } else {
  59. createProjectForPlatform = projects.find(
  60. p => p.slug === onboardingContext.data.selectedSDK?.key
  61. )
  62. ? undefined
  63. : onboardingContext.data.selectedSDK;
  64. }
  65. if (!createProjectForPlatform) {
  66. const platform = selectedFramework ? selectedFramework : selectedPlatform;
  67. trackAnalytics('growth.onboarding_set_up_your_project', {
  68. platform: selectedPlatform.key,
  69. organization,
  70. });
  71. onComplete(platform);
  72. return;
  73. }
  74. try {
  75. addLoadingMessage(t('Loading SDK configuration\u2026'));
  76. const response = (await createProject({
  77. api,
  78. orgSlug: organization.slug,
  79. team: teams[0].slug,
  80. platform: createProjectForPlatform.key,
  81. name: createProjectForPlatform.key,
  82. options: {
  83. defaultRules: true,
  84. },
  85. })) as Project;
  86. ProjectsStore.onCreateSuccess(response, organization.slug);
  87. // Measure to filter out projects that might have been created during the onboarding and not deleted from the session due to an error
  88. // Note: in the onboarding flow the projects are created based on the platform slug
  89. const newProjects = Object.keys(onboardingContext.data.projects).reduce(
  90. (acc, key) => {
  91. if (onboardingContext.data.projects[key].slug !== response.slug) {
  92. acc[key] = onboardingContext.data.projects[key];
  93. }
  94. return acc;
  95. },
  96. {}
  97. );
  98. onboardingContext.setData({
  99. selectedSDK: createProjectForPlatform,
  100. projects: {
  101. ...newProjects,
  102. [response.id]: {
  103. slug: response.slug,
  104. status: OnboardingProjectStatus.WAITING,
  105. },
  106. },
  107. });
  108. trackAnalytics('growth.onboarding_set_up_your_project', {
  109. platform: selectedPlatform.key,
  110. organization,
  111. });
  112. clearIndicators();
  113. setTimeout(() => onComplete(createProjectForPlatform!));
  114. } catch (err) {
  115. addErrorMessage(t('Failed to load SDK configuration'));
  116. Sentry.captureException(err);
  117. }
  118. },
  119. [onboardingContext, selectedPlatform, api, organization, teams, projects, onComplete]
  120. );
  121. const handleProjectCreation = useCallback(async () => {
  122. if (!selectedPlatform) {
  123. return;
  124. }
  125. if (
  126. selectedPlatform.type !== 'language' ||
  127. !Object.values(SupportedLanguages).includes(
  128. selectedPlatform.language as SupportedLanguages
  129. )
  130. ) {
  131. createPlatformProject();
  132. return;
  133. }
  134. const {FrameworkSuggestionModal, modalCss} = await import(
  135. 'sentry/components/onboarding/frameworkSuggestionModal'
  136. );
  137. openModal(
  138. deps => (
  139. <FrameworkSuggestionModal
  140. {...deps}
  141. organization={organization}
  142. selectedPlatform={selectedPlatform}
  143. onConfigure={selectedFramework => {
  144. onboardingContext.setData({
  145. ...onboardingContext.data,
  146. selectedSDK: selectedFramework,
  147. });
  148. createPlatformProject(selectedFramework);
  149. }}
  150. onSkip={createPlatformProject}
  151. newOrg
  152. />
  153. ),
  154. {
  155. modalCss,
  156. onClose: () => {
  157. trackAnalytics('onboarding.select_framework_modal_close_button_clicked', {
  158. platform: selectedPlatform.key,
  159. organization,
  160. });
  161. },
  162. }
  163. );
  164. }, [selectedPlatform, createPlatformProject, onboardingContext, organization]);
  165. return (
  166. <GenericFooter>
  167. {genSkipOnboardingLink()}
  168. <SelectionWrapper>
  169. {selectedPlatform ? (
  170. <Fragment>
  171. <div>
  172. <SelectedPlatformIcon
  173. platform={selectedPlatform.key ?? 'other'}
  174. size={23}
  175. />
  176. </div>
  177. <PlatformsSelected>
  178. {t('platform selected')}
  179. <ClearButton priority="link" onClick={clearPlatform} size="zero">
  180. {t('Clear')}
  181. </ClearButton>
  182. </PlatformsSelected>
  183. </Fragment>
  184. ) : null}
  185. </SelectionWrapper>
  186. <ButtonWrapper>
  187. <Button
  188. priority="primary"
  189. onClick={() =>
  190. frameworkSelectionEnabled ? handleProjectCreation() : createPlatformProject()
  191. }
  192. disabled={!selectedPlatform}
  193. data-test-id="platform-select-next"
  194. >
  195. {t('Configure SDK')}
  196. </Button>
  197. </ButtonWrapper>
  198. </GenericFooter>
  199. );
  200. }
  201. const SelectionWrapper = styled(motion.div)`
  202. display: flex;
  203. flex-direction: column;
  204. justify-content: center;
  205. align-items: center;
  206. @media (max-width: ${p => p.theme.breakpoints.small}) {
  207. display: none;
  208. }
  209. `;
  210. SelectionWrapper.defaultProps = {
  211. transition: testableTransition({
  212. duration: 1.8,
  213. }),
  214. };
  215. const ButtonWrapper = styled(motion.div)`
  216. display: flex;
  217. height: 100%;
  218. align-items: center;
  219. margin-right: ${space(4)};
  220. margin-left: ${space(4)};
  221. `;
  222. ButtonWrapper.defaultProps = {
  223. transition: testableTransition({
  224. duration: 1.3,
  225. }),
  226. };
  227. const SelectedPlatformIcon = styled(PlatformIcon)`
  228. margin-right: ${space(1)};
  229. `;
  230. const PlatformsSelected = styled('div')`
  231. margin-top: ${space(1)};
  232. `;
  233. const ClearButton = styled(Button)`
  234. margin-left: ${space(2)};
  235. padding: 0;
  236. `;