createProjectsFooter.tsx 7.5 KB

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