createProject.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. import {useCallback, useContext, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import omit from 'lodash/omit';
  5. import startCase from 'lodash/startCase';
  6. import {PlatformIcon} from 'platformicons';
  7. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  8. import {openModal} from 'sentry/actionCreators/modal';
  9. import Access from 'sentry/components/acl/access';
  10. import {Alert} from 'sentry/components/alert';
  11. import {Button} from 'sentry/components/button';
  12. import Input from 'sentry/components/input';
  13. import * as Layout from 'sentry/components/layouts/thirds';
  14. import ExternalLink from 'sentry/components/links/externalLink';
  15. import List from 'sentry/components/list';
  16. import ListItem from 'sentry/components/list/listItem';
  17. import {SupportedLanguages} from 'sentry/components/onboarding/frameworkSuggestionModal';
  18. import type {Platform} from 'sentry/components/platformPicker';
  19. import PlatformPicker from 'sentry/components/platformPicker';
  20. import {canCreateProject} from 'sentry/components/projects/canCreateProject';
  21. import TeamSelector from 'sentry/components/teamSelector';
  22. import {Tooltip} from 'sentry/components/tooltip';
  23. import {t, tct} from 'sentry/locale';
  24. import ProjectsStore from 'sentry/stores/projectsStore';
  25. import {space} from 'sentry/styles/space';
  26. import type {OnboardingSelectedSDK} from 'sentry/types/onboarding';
  27. import type {Team} from 'sentry/types/organization';
  28. import {trackAnalytics} from 'sentry/utils/analytics';
  29. import {browserHistory} from 'sentry/utils/browserHistory';
  30. import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
  31. import slugify from 'sentry/utils/slugify';
  32. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  33. import useApi from 'sentry/utils/useApi';
  34. import {useLocation} from 'sentry/utils/useLocation';
  35. import useOrganization from 'sentry/utils/useOrganization';
  36. import {useTeams} from 'sentry/utils/useTeams';
  37. import {
  38. MultipleCheckboxOptions,
  39. useCreateNotificationAction,
  40. } from 'sentry/views/projectInstall/issueAlertNotificationOptions';
  41. import IssueAlertOptions, {
  42. MetricValues,
  43. RuleAction,
  44. } from 'sentry/views/projectInstall/issueAlertOptions';
  45. import {GettingStartedWithProjectContext} from 'sentry/views/projects/gettingStartedWithProjectContext';
  46. export type IssueAlertFragment = Parameters<
  47. React.ComponentProps<typeof IssueAlertOptions>['onChange']
  48. >[0];
  49. function CreateProject() {
  50. const api = useApi();
  51. const organization = useOrganization();
  52. const location = useLocation();
  53. const gettingStartedWithProjectContext = useContext(GettingStartedWithProjectContext);
  54. const {teams} = useTeams();
  55. const autoFill =
  56. location.query.referrer === 'getting-started' &&
  57. location.query.project === gettingStartedWithProjectContext.project?.id;
  58. const accessTeams = teams.filter((team: Team) => team.access.includes('team:admin'));
  59. useRouteAnalyticsEventNames(
  60. 'project_creation_page.viewed',
  61. 'Project Create: Creation page viewed'
  62. );
  63. const [projectName, setProjectName] = useState(
  64. autoFill ? gettingStartedWithProjectContext.project?.name : ''
  65. );
  66. const [platform, setPlatform] = useState<OnboardingSelectedSDK | undefined>(
  67. autoFill ? gettingStartedWithProjectContext.project?.platform : undefined
  68. );
  69. const [team, setTeam] = useState(
  70. autoFill
  71. ? gettingStartedWithProjectContext.project?.teamSlug ?? accessTeams?.[0]?.slug
  72. : accessTeams?.[0]?.slug
  73. );
  74. const [errors, setErrors] = useState(false);
  75. const [inFlight, setInFlight] = useState(false);
  76. const [alertRuleConfig, setAlertRuleConfig] = useState<IssueAlertFragment | undefined>(
  77. undefined
  78. );
  79. const {createNotificationAction, notificationProps} = useCreateNotificationAction();
  80. const frameworkSelectionEnabled = !!organization?.features.includes(
  81. 'onboarding-sdk-selection'
  82. );
  83. const createProject = useCallback(
  84. async (selectedFramework?: OnboardingSelectedSDK) => {
  85. const {slug} = organization;
  86. const {
  87. shouldCreateRule,
  88. shouldCreateCustomRule,
  89. name,
  90. conditions,
  91. actions,
  92. actionMatch,
  93. frequency,
  94. defaultRules,
  95. } = alertRuleConfig || {};
  96. const selectedPlatform = selectedFramework ?? platform;
  97. if (!selectedPlatform) {
  98. addErrorMessage(t('Please select a platform in Step 1'));
  99. return;
  100. }
  101. setInFlight(true);
  102. try {
  103. const url = team
  104. ? `/teams/${slug}/${team}/projects/`
  105. : `/organizations/${slug}/experimental/projects/`;
  106. const projectData = await api.requestPromise(url, {
  107. method: 'POST',
  108. data: {
  109. name: projectName,
  110. platform: selectedPlatform.key,
  111. default_rules: defaultRules ?? true,
  112. },
  113. });
  114. const ruleIds: string[] = [];
  115. if (shouldCreateCustomRule) {
  116. const ruleData = await api.requestPromise(
  117. `/projects/${organization.slug}/${projectData.slug}/rules/`,
  118. {
  119. method: 'POST',
  120. data: {
  121. name,
  122. conditions,
  123. actions,
  124. actionMatch,
  125. frequency,
  126. },
  127. }
  128. );
  129. ruleIds.push(ruleData.id);
  130. }
  131. const ruleData = await createNotificationAction({
  132. shouldCreateRule,
  133. name,
  134. projectSlug: projectData.slug,
  135. conditions,
  136. actionMatch,
  137. frequency,
  138. });
  139. if (ruleData) {
  140. ruleIds.push(ruleData.id);
  141. }
  142. trackAnalytics('project_creation_page.created', {
  143. organization,
  144. issue_alert: defaultRules
  145. ? 'Default'
  146. : shouldCreateCustomRule
  147. ? 'Custom'
  148. : 'No Rule',
  149. project_id: projectData.id,
  150. platform: selectedPlatform.key,
  151. rule_ids: ruleIds,
  152. });
  153. ProjectsStore.onCreateSuccess(projectData, organization.slug);
  154. if (team) {
  155. addSuccessMessage(
  156. tct('Created project [project]', {
  157. project: `${projectData.slug}`,
  158. })
  159. );
  160. } else {
  161. addSuccessMessage(
  162. tct('Created [project] under new team [team]', {
  163. project: `${projectData.slug}`,
  164. team: `#${projectData.team_slug}`,
  165. })
  166. );
  167. }
  168. browserHistory.push(
  169. normalizeUrl(
  170. `/organizations/${organization.slug}/projects/${projectData.slug}/getting-started/`
  171. )
  172. );
  173. } catch (err) {
  174. setInFlight(false);
  175. setErrors(err.responseJSON);
  176. addErrorMessage(
  177. tct('Failed to create project [project]', {
  178. project: `${projectName}`,
  179. })
  180. );
  181. // Only log this if the error is something other than:
  182. // * The user not having access to create a project, or,
  183. // * A project with that slug already exists
  184. if (err.status !== 403 && err.status !== 409) {
  185. Sentry.withScope(scope => {
  186. scope.setExtra('err', err);
  187. Sentry.captureMessage('Project creation failed');
  188. });
  189. }
  190. }
  191. },
  192. [
  193. api,
  194. alertRuleConfig,
  195. organization,
  196. platform,
  197. projectName,
  198. team,
  199. createNotificationAction,
  200. ]
  201. );
  202. const handleProjectCreation = useCallback(async () => {
  203. const selectedPlatform = platform;
  204. if (!selectedPlatform) {
  205. addErrorMessage(t('Please select a platform in Step 1'));
  206. return;
  207. }
  208. if (
  209. selectedPlatform.type !== 'language' ||
  210. !Object.values(SupportedLanguages).includes(
  211. selectedPlatform.language as SupportedLanguages
  212. )
  213. ) {
  214. createProject();
  215. return;
  216. }
  217. const {FrameworkSuggestionModal, modalCss} = await import(
  218. 'sentry/components/onboarding/frameworkSuggestionModal'
  219. );
  220. openModal(
  221. deps => (
  222. <FrameworkSuggestionModal
  223. {...deps}
  224. organization={organization}
  225. selectedPlatform={selectedPlatform}
  226. onConfigure={selectedFramework => {
  227. createProject(selectedFramework);
  228. }}
  229. onSkip={createProject}
  230. />
  231. ),
  232. {
  233. modalCss,
  234. onClose: () => {
  235. trackAnalytics('project_creation.select_framework_modal_close_button_clicked', {
  236. platform: selectedPlatform.key,
  237. organization,
  238. });
  239. },
  240. }
  241. );
  242. }, [platform, createProject, organization]);
  243. function handlePlatformChange(selectedPlatform: Platform | null) {
  244. if (!selectedPlatform?.id) {
  245. setPlatform(undefined);
  246. setProjectName('');
  247. return;
  248. }
  249. const userModifiedName = !!projectName && projectName !== platform?.key;
  250. const newName = userModifiedName ? projectName : selectedPlatform.id;
  251. setPlatform({
  252. ...omit(selectedPlatform, 'id'),
  253. key: selectedPlatform.id,
  254. });
  255. setProjectName(newName);
  256. }
  257. const {shouldCreateRule, shouldCreateCustomRule, conditions} = alertRuleConfig || {};
  258. const canUserCreateProject = canCreateProject(organization);
  259. const canCreateTeam = organization.access.includes('project:admin');
  260. const isOrgMemberWithNoAccess = accessTeams.length === 0 && !canCreateTeam;
  261. const isMissingTeam = !isOrgMemberWithNoAccess && !team;
  262. const isMissingProjectName = projectName === '';
  263. const isMissingAlertThreshold =
  264. shouldCreateCustomRule && !conditions?.every?.(condition => condition.value);
  265. const isMissingMessagingIntegrationChannel =
  266. organization.features.includes('messaging-integration-onboarding-project-creation') &&
  267. shouldCreateRule &&
  268. notificationProps.actions?.some(
  269. action => action === MultipleCheckboxOptions.INTEGRATION
  270. ) &&
  271. !notificationProps.channel;
  272. const formErrorCount = [
  273. isMissingTeam,
  274. isMissingProjectName,
  275. isMissingAlertThreshold,
  276. isMissingMessagingIntegrationChannel,
  277. ].filter(value => value).length;
  278. const canSubmitForm = !inFlight && canUserCreateProject && formErrorCount === 0;
  279. let submitTooltipText: string = t('Please select a team');
  280. if (formErrorCount > 1) {
  281. submitTooltipText = t('Please fill out all the required fields');
  282. } else if (isMissingProjectName) {
  283. submitTooltipText = t('Please provide a project name');
  284. } else if (isMissingAlertThreshold) {
  285. submitTooltipText = t('Please provide an alert threshold');
  286. } else if (isMissingMessagingIntegrationChannel) {
  287. submitTooltipText = t(
  288. 'Please provide an integration channel for alert notifications'
  289. );
  290. }
  291. const keyToErrorText = {
  292. actions: t('Notify via integration'),
  293. conditions: t('Alert conditions'),
  294. name: t('Alert name'),
  295. detail: t('Project details'),
  296. };
  297. const alertFrequencyDefaultValues = useMemo(() => {
  298. if (!autoFill) {
  299. return {};
  300. }
  301. const alertRules = gettingStartedWithProjectContext.project?.alertRules;
  302. if (alertRules?.length === 0) {
  303. return {
  304. alertSetting: String(RuleAction.CREATE_ALERT_LATER),
  305. };
  306. }
  307. if (
  308. alertRules?.[0].conditions?.[0].id?.endsWith('EventFrequencyCondition') ||
  309. alertRules?.[0].conditions?.[0].id?.endsWith('EventUniqueUserFrequencyCondition')
  310. ) {
  311. return {
  312. alertSetting: String(RuleAction.CUSTOMIZED_ALERTS),
  313. interval: String(alertRules?.[0].conditions?.[0].interval),
  314. threshold: String(alertRules?.[0].conditions?.[0].value),
  315. metric: alertRules?.[0].conditions?.[0].id?.endsWith('EventFrequencyCondition')
  316. ? MetricValues.ERRORS
  317. : MetricValues.USERS,
  318. };
  319. }
  320. return {
  321. alertSetting: String(RuleAction.DEFAULT_ALERT),
  322. };
  323. }, [autoFill, gettingStartedWithProjectContext.project?.alertRules]);
  324. return (
  325. <Access access={canUserCreateProject ? ['project:read'] : ['project:admin']}>
  326. <div data-test-id="onboarding-info">
  327. <List symbol="colored-numeric">
  328. <Layout.Title withMargins>{t('Create a new project in 3 steps')}</Layout.Title>
  329. <HelpText>
  330. {tct(
  331. 'Set up a separate project for each part of your application (for example, your API server and frontend client), to quickly pinpoint which part of your application errors are coming from. [link: Read the docs].',
  332. {
  333. link: (
  334. <ExternalLink href="https://docs.sentry.io/product/sentry-basics/integrate-frontend/create-new-project/" />
  335. ),
  336. }
  337. )}
  338. </HelpText>
  339. <StyledListItem>{t('Choose your platform')}</StyledListItem>
  340. <PlatformPicker
  341. platform={platform?.key}
  342. defaultCategory={platform?.category}
  343. setPlatform={handlePlatformChange}
  344. organization={organization}
  345. showOther
  346. noAutoFilter
  347. />
  348. <StyledListItem>{t('Set your alert frequency')}</StyledListItem>
  349. <IssueAlertOptions
  350. {...alertFrequencyDefaultValues}
  351. platformLanguage={platform?.language as SupportedLanguages}
  352. onChange={updatedData => setAlertRuleConfig(updatedData)}
  353. notificationProps={notificationProps}
  354. />
  355. <StyledListItem>{t('Name your project and assign it a team')}</StyledListItem>
  356. <CreateProjectForm
  357. onSubmit={(event: React.FormEvent<HTMLFormElement>) => {
  358. // Prevent the page from reloading
  359. event.preventDefault();
  360. frameworkSelectionEnabled ? handleProjectCreation() : createProject();
  361. }}
  362. >
  363. <div>
  364. <FormLabel>{t('Project name')}</FormLabel>
  365. <ProjectNameInputWrap>
  366. <StyledPlatformIcon platform={platform?.key ?? 'other'} size={20} />
  367. <ProjectNameInput
  368. type="text"
  369. name="name"
  370. placeholder={t('project-name')}
  371. autoComplete="off"
  372. value={projectName}
  373. onChange={e => setProjectName(slugify(e.target.value))}
  374. />
  375. </ProjectNameInputWrap>
  376. </div>
  377. {!isOrgMemberWithNoAccess && (
  378. <div>
  379. <FormLabel>{t('Team')}</FormLabel>
  380. <TeamSelectInput>
  381. <TeamSelector
  382. allowCreate
  383. name="select-team"
  384. aria-label={t('Select a Team')}
  385. menuPlacement="auto"
  386. clearable={false}
  387. value={team}
  388. placeholder={t('Select a Team')}
  389. onChange={choice => setTeam(choice.value)}
  390. teamFilter={(tm: Team) => tm.access.includes('team:admin')}
  391. />
  392. </TeamSelectInput>
  393. </div>
  394. )}
  395. <div>
  396. <Tooltip title={submitTooltipText} disabled={formErrorCount === 0}>
  397. <Button
  398. type="submit"
  399. data-test-id="create-project"
  400. priority="primary"
  401. disabled={!canSubmitForm}
  402. >
  403. {t('Create Project')}
  404. </Button>
  405. </Tooltip>
  406. </div>
  407. </CreateProjectForm>
  408. {errors && (
  409. <Alert type="error">
  410. {Object.keys(errors).map(key => (
  411. <div key={key}>
  412. <strong>{keyToErrorText[key] ?? startCase(key)}</strong>: {errors[key]}
  413. </div>
  414. ))}
  415. </Alert>
  416. )}
  417. </List>
  418. </div>
  419. </Access>
  420. );
  421. }
  422. export {CreateProject};
  423. const StyledListItem = styled(ListItem)`
  424. margin: ${space(2)} 0 ${space(1)} 0;
  425. font-size: ${p => p.theme.fontSizeExtraLarge};
  426. `;
  427. const CreateProjectForm = styled('form')`
  428. display: grid;
  429. grid-template-columns: 300px minmax(250px, max-content) max-content;
  430. gap: ${space(2)};
  431. align-items: end;
  432. padding: ${space(3)} 0;
  433. background: ${p => p.theme.background};
  434. `;
  435. const FormLabel = styled('div')`
  436. font-size: ${p => p.theme.fontSizeExtraLarge};
  437. margin-bottom: ${space(1)};
  438. `;
  439. const ProjectNameInputWrap = styled('div')`
  440. position: relative;
  441. `;
  442. const ProjectNameInput = styled(Input)`
  443. padding-left: calc(${p => p.theme.formPadding.md.paddingLeft}px * 1.5 + 20px);
  444. `;
  445. const StyledPlatformIcon = styled(PlatformIcon)`
  446. position: absolute;
  447. top: 50%;
  448. left: ${p => p.theme.formPadding.md.paddingLeft}px;
  449. transform: translateY(-50%);
  450. `;
  451. const TeamSelectInput = styled('div')`
  452. display: grid;
  453. gap: ${space(1)};
  454. grid-template-columns: 1fr min-content;
  455. align-items: center;
  456. `;
  457. const HelpText = styled('p')`
  458. color: ${p => p.theme.subText};
  459. max-width: 760px;
  460. `;