createProjectsFooter.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  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 transition={testableTransition({duration: 1.8})}>
  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 transition={testableTransition({duration: 1.3})}>
  187. <Button
  188. priority="primary"
  189. onClick={handleProjectCreation}
  190. disabled={!selectedPlatform}
  191. data-test-id="platform-select-next"
  192. >
  193. {t('Configure SDK')}
  194. </Button>
  195. </ButtonWrapper>
  196. </GenericFooter>
  197. );
  198. }
  199. const SelectionWrapper = styled(motion.div)`
  200. display: flex;
  201. flex-direction: column;
  202. justify-content: center;
  203. align-items: center;
  204. @media (max-width: ${p => p.theme.breakpoints.small}) {
  205. display: none;
  206. }
  207. `;
  208. const ButtonWrapper = styled(motion.div)`
  209. display: flex;
  210. height: 100%;
  211. align-items: center;
  212. margin-right: ${space(4)};
  213. margin-left: ${space(4)};
  214. `;
  215. const SelectedPlatformIcon = styled(PlatformIcon)`
  216. margin-right: ${space(1)};
  217. `;
  218. const PlatformsSelected = styled('div')`
  219. margin-top: ${space(1)};
  220. `;
  221. const ClearButton = styled(Button)`
  222. margin-left: ${space(2)};
  223. padding: 0;
  224. `;