1
1
import { useCallback , useEffect , useMemo , useState } from 'react' ;
2
2
import styled from '@emotion/styled' ;
3
3
import * as Sentry from '@sentry/react' ;
4
+ import debounce from 'lodash/debounce' ;
4
5
import omit from 'lodash/omit' ;
5
6
import startCase from 'lodash/startCase' ;
6
7
import { PlatformIcon } from 'platformicons' ;
@@ -18,7 +19,7 @@ import ExternalLink from 'sentry/components/links/externalLink';
18
19
import List from 'sentry/components/list' ;
19
20
import ListItem from 'sentry/components/list/listItem' ;
20
21
import { SupportedLanguages } from 'sentry/components/onboarding/frameworkSuggestionModal' ;
21
- import { useCreateProject } from 'sentry/components/onboarding/useCreateProject ' ;
22
+ import { useCreateProjectAndRules } from 'sentry/components/onboarding/useCreateProjectAndRules ' ;
22
23
import type { Platform } from 'sentry/components/platformPicker' ;
23
24
import PlatformPicker from 'sentry/components/platformPicker' ;
24
25
import TeamSelector from 'sentry/components/teamSelector' ;
@@ -139,7 +140,7 @@ export function CreateProject() {
139
140
const location = useLocation ( ) ;
140
141
const { createNotificationAction, notificationProps} = useCreateNotificationAction ( ) ;
141
142
const canUserCreateProject = useCanCreateProject ( ) ;
142
- const createProject = useCreateProject ( ) ;
143
+ const createProjectAndRules = useCreateProjectAndRules ( ) ;
143
144
const { teams} = useTeams ( ) ;
144
145
const accessTeams = teams . filter ( ( team : Team ) => team . access . includes ( 'team:admin' ) ) ;
145
146
const referrer = decodeScalar ( location . query . referrer ) ;
@@ -149,49 +150,6 @@ export function CreateProject() {
149
150
null
150
151
) ;
151
152
152
- const createRules = useCallback (
153
- async ( {
154
- project,
155
- alertRuleConfig,
156
- } : { project : Project } & Pick < FormData , 'alertRuleConfig' > ) => {
157
- const ruleIds = [ ] ;
158
-
159
- if ( alertRuleConfig ?. shouldCreateCustomRule ) {
160
- const ruleData = await api . requestPromise (
161
- `/projects/${ organization . slug } /${ project . slug } /rules/` ,
162
- {
163
- method : 'POST' ,
164
- data : {
165
- name : project . name ,
166
- conditions : alertRuleConfig ?. conditions ,
167
- actions : alertRuleConfig ?. actions ,
168
- actionMatch : alertRuleConfig ?. actionMatch ,
169
- frequency : alertRuleConfig ?. frequency ,
170
- } ,
171
- }
172
- ) ;
173
-
174
- ruleIds . push ( ruleData . id ) ;
175
- }
176
-
177
- const notificationRule = await createNotificationAction ( {
178
- shouldCreateRule : alertRuleConfig ?. shouldCreateRule ,
179
- name : project . name ,
180
- projectSlug : project . slug ,
181
- conditions : alertRuleConfig ?. conditions ,
182
- actionMatch : alertRuleConfig ?. actionMatch ,
183
- frequency : alertRuleConfig ?. frequency ,
184
- } ) ;
185
-
186
- if ( notificationRule ) {
187
- ruleIds . push ( notificationRule . id ) ;
188
- }
189
-
190
- return ruleIds ;
191
- } ,
192
- [ organization , api , createNotificationAction ]
193
- ) ;
194
-
195
153
const autoFill = useMemo ( ( ) => {
196
154
return referrer === 'getting-started' && projectId === createdProject ?. id ;
197
155
} , [ referrer , projectId , createdProject ?. id ] ) ;
@@ -238,9 +196,6 @@ export function CreateProject() {
238
196
missingValues . isMissingMessagingIntegrationChannel ,
239
197
] . filter ( value => value ) . length ;
240
198
241
- const canSubmitForm =
242
- ! createProject . isPending && canUserCreateProject && formErrorCount === 0 ;
243
-
244
199
const submitTooltipText = getSubmitTooltipText ( {
245
200
...missingValues ,
246
201
formErrorCount,
@@ -287,17 +242,15 @@ export function CreateProject() {
287
242
let projectToRollback : Project | undefined ;
288
243
289
244
try {
290
- const project = await createProject . mutateAsync ( {
291
- name : projectName ,
245
+ const { project, ruleIds } = await createProjectAndRules . mutateAsync ( {
246
+ projectName,
292
247
platform : selectedPlatform ,
293
- default_rules : alertRuleConfig ?. defaultRules ?? true ,
294
- firstTeamSlug : team ,
248
+ team,
249
+ alertRuleConfig,
250
+ createNotificationAction,
295
251
} ) ;
296
-
297
252
projectToRollback = project ;
298
253
299
- const ruleIds = await createRules ( { project, alertRuleConfig} ) ;
300
-
301
254
trackAnalytics ( 'project_creation_page.created' , {
302
255
organization,
303
256
issue_alert : alertRuleConfig ?. defaultRules
@@ -376,11 +329,20 @@ export function CreateProject() {
376
329
}
377
330
}
378
331
} ,
379
- [ createRules , organization , createProject , setCreatedProject , navigate , api ]
332
+ [
333
+ organization ,
334
+ setCreatedProject ,
335
+ navigate ,
336
+ api ,
337
+ createProjectAndRules ,
338
+ createNotificationAction ,
339
+ ]
380
340
) ;
381
341
382
342
const handleProjectCreation = useCallback (
383
343
async ( data : FormData ) => {
344
+ setErrors ( undefined ) ;
345
+
384
346
const selectedPlatform = data . platform ;
385
347
386
348
if ( ! isNotPartialPlatform ( selectedPlatform ) ) {
@@ -431,6 +393,11 @@ export function CreateProject() {
431
393
[ configurePlatform , organization ]
432
394
) ;
433
395
396
+ const debounceHandleProjectCreation = useMemo (
397
+ ( ) => debounce ( handleProjectCreation , 2000 , { leading : true , trailing : false } ) ,
398
+ [ handleProjectCreation ]
399
+ ) ;
400
+
434
401
const handlePlatformChange = useCallback (
435
402
( value : Platform | null ) => {
436
403
if ( ! value ) {
@@ -552,8 +519,9 @@ export function CreateProject() {
552
519
< Button
553
520
data-test-id = "create-project"
554
521
priority = "primary"
555
- disabled = { ! canSubmitForm }
556
- onClick = { ( ) => handleProjectCreation ( formData ) }
522
+ disabled = { ! ( canUserCreateProject && formErrorCount === 0 ) }
523
+ busy = { createProjectAndRules . isPending }
524
+ onClick = { ( ) => debounceHandleProjectCreation ( formData ) }
557
525
>
558
526
{ t ( 'Create Project' ) }
559
527
</ Button >
0 commit comments