createProjectsFooter.tsx 7.6 KB

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