Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions static/app/components/onboarding/frameworkSuggestionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {Radio} from 'sentry/components/core/radio';
import {RadioLineItem} from 'sentry/components/forms/controls/radioGroup';
import List from 'sentry/components/list';
import ListItem from 'sentry/components/list/listItem';
import {useIsCreatingProject} from 'sentry/components/onboarding/useCreateProject';
import {useIsCreatingProjectAndRules} from 'sentry/components/onboarding/useCreateProjectAndRules';
import Panel from 'sentry/components/panels/panel';
import PanelBody from 'sentry/components/panels/panelBody';
import categoryList, {createablePlatforms} from 'sentry/data/platformPickerCategories';
Expand Down Expand Up @@ -133,7 +133,8 @@ export function FrameworkSuggestionModal({
organization,
newOrg,
}: FrameworkSuggestionModalProps) {
const isCreatingProject = useIsCreatingProject();
const isCreatingProjectAndRules = useIsCreatingProjectAndRules();

const [selectedFramework, setSelectedFramework] = useState<
OnboardingSelectedSDK | undefined
>(selectedPlatform);
Expand Down Expand Up @@ -306,7 +307,11 @@ export function FrameworkSuggestionModal({
</StyledPanel>
</Body>
<Footer>
<Button priority="primary" onClick={debounceHandleClick} busy={isCreatingProject}>
<Button
priority="primary"
onClick={debounceHandleClick}
busy={isCreatingProjectAndRules}
>
{t('Configure SDK')}
</Button>
</Footer>
Expand Down
9 changes: 1 addition & 8 deletions static/app/components/onboarding/useCreateProject.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import ProjectsStore from 'sentry/stores/projectsStore';
import type {OnboardingSelectedSDK} from 'sentry/types/onboarding';
import type {Project} from 'sentry/types/project';
import {useIsMutating, useMutation} from 'sentry/utils/queryClient';
import {useMutation} from 'sentry/utils/queryClient';
import type RequestError from 'sentry/utils/requestError/requestError';
import useApi from 'sentry/utils/useApi';
import useOrganization from 'sentry/utils/useOrganization';

const MUTATION_KEY = 'create-project';

interface Variables {
platform: OnboardingSelectedSDK;
default_rules?: boolean;
Expand All @@ -20,7 +18,6 @@ export function useCreateProject() {
const organization = useOrganization();

return useMutation<Project, RequestError, Variables>({
mutationKey: [MUTATION_KEY],
mutationFn: ({firstTeamSlug, name, platform, default_rules}) => {
return api.requestPromise(
firstTeamSlug
Expand All @@ -42,7 +39,3 @@ export function useCreateProject() {
},
});
}

export function useIsCreatingProject() {
return Boolean(useIsMutating({mutationKey: [MUTATION_KEY]}));
}
78 changes: 78 additions & 0 deletions static/app/components/onboarding/useCreateProjectAndRules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {useCreateProject} from 'sentry/components/onboarding/useCreateProject';
import {useCreateProjectRules} from 'sentry/components/onboarding/useCreateProjectRules';
import type {OnboardingSelectedSDK} from 'sentry/types/onboarding';
import type {Project} from 'sentry/types/project';
import {defined} from 'sentry/utils';
import {useIsMutating, useMutation} from 'sentry/utils/queryClient';
import type RequestError from 'sentry/utils/requestError/requestError';
import type {useCreateNotificationAction} from 'sentry/views/projectInstall/issueAlertNotificationOptions';
import type {RequestDataFragment} from 'sentry/views/projectInstall/issueAlertOptions';

const MUTATION_KEY = 'create-project-and-rules';

type Variables = {
alertRuleConfig: Partial<RequestDataFragment>;
createNotificationAction: ReturnType<
typeof useCreateNotificationAction
>['createNotificationAction'];
platform: OnboardingSelectedSDK;
projectName: string;
team?: string;
};

type Response = {
project: Project;
ruleIds: string[];
};

export function useCreateProjectAndRules() {
const createProject = useCreateProject();
const createProjectRules = useCreateProjectRules();

return useMutation<Response, RequestError, Variables>({
mutationKey: [MUTATION_KEY],
mutationFn: async ({
projectName,
platform,
alertRuleConfig,
team,
createNotificationAction,
}) => {
const project = await createProject.mutateAsync({
name: projectName,
platform,
default_rules: alertRuleConfig?.defaultRules ?? true,
firstTeamSlug: team,
});

const customRulePromise = alertRuleConfig?.shouldCreateCustomRule
? createProjectRules.mutateAsync({
projectSlug: project.slug,
name: project.name,
conditions: alertRuleConfig?.conditions,
actions: alertRuleConfig?.actions,
actionMatch: alertRuleConfig?.actionMatch,
frequency: alertRuleConfig?.frequency,
})
: undefined;

const notificationRulePromise = createNotificationAction({
shouldCreateRule: alertRuleConfig?.shouldCreateRule,
name: project.name,
projectSlug: project.slug,
conditions: alertRuleConfig?.conditions,
actionMatch: alertRuleConfig?.actionMatch,
frequency: alertRuleConfig?.frequency,
});

const rules = await Promise.all([customRulePromise, notificationRulePromise]);
const ruleIds = rules.filter(defined).map(rule => rule.id);

return {project, ruleIds};
},
});
}

export function useIsCreatingProjectAndRules() {
return Boolean(useIsMutating({mutationKey: [MUTATION_KEY]}));
}
87 changes: 27 additions & 60 deletions static/app/views/projectInstall/createProject.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {useCallback, useEffect, useMemo, useState} from 'react';
import styled from '@emotion/styled';
import * as Sentry from '@sentry/react';
import debounce from 'lodash/debounce';
import omit from 'lodash/omit';
import startCase from 'lodash/startCase';
import {PlatformIcon} from 'platformicons';
Expand All @@ -18,8 +19,7 @@ import ExternalLink from 'sentry/components/links/externalLink';
import List from 'sentry/components/list';
import ListItem from 'sentry/components/list/listItem';
import {SupportedLanguages} from 'sentry/components/onboarding/frameworkSuggestionModal';
import {useCreateProject} from 'sentry/components/onboarding/useCreateProject';
import {useCreateProjectRules} from 'sentry/components/onboarding/useCreateProjectRules';
import {useCreateProjectAndRules} from 'sentry/components/onboarding/useCreateProjectAndRules';
import type {Platform} from 'sentry/components/platformPicker';
import PlatformPicker from 'sentry/components/platformPicker';
import TeamSelector from 'sentry/components/teamSelector';
Expand Down Expand Up @@ -135,13 +135,11 @@ const keyToErrorText: Record<string, string> = {
export function CreateProject() {
const api = useApi();
const navigate = useNavigate();
const [errors, setErrors] = useState();
const organization = useOrganization();
const location = useLocation();
const {createNotificationAction, notificationProps} = useCreateNotificationAction();
const canUserCreateProject = useCanCreateProject();
const createProject = useCreateProject();
const createProjectRules = useCreateProjectRules();
const createProjectAndRules = useCreateProjectAndRules();
const {teams} = useTeams();
const accessTeams = teams.filter((team: Team) => team.access.includes('team:admin'));
const referrer = decodeScalar(location.query.referrer);
Expand All @@ -151,44 +149,6 @@ export function CreateProject() {
null
);

const createRules = useCallback(
async ({
project,
alertRuleConfig,
}: {project: Project} & Pick<FormData, 'alertRuleConfig'>) => {
const ruleIds = [];

if (alertRuleConfig?.shouldCreateCustomRule) {
const ruleData = await createProjectRules.mutateAsync({
projectSlug: project.slug,
name: project.name,
conditions: alertRuleConfig?.conditions,
actions: alertRuleConfig?.actions,
actionMatch: alertRuleConfig?.actionMatch,
frequency: alertRuleConfig?.frequency,
});

ruleIds.push(ruleData.id);
}

const notificationRule = await createNotificationAction({
shouldCreateRule: alertRuleConfig?.shouldCreateRule,
name: project.name,
projectSlug: project.slug,
conditions: alertRuleConfig?.conditions,
actionMatch: alertRuleConfig?.actionMatch,
frequency: alertRuleConfig?.frequency,
});

if (notificationRule) {
ruleIds.push(notificationRule.id);
}

return ruleIds;
},
[createNotificationAction, createProjectRules]
);

const autoFill = useMemo(() => {
return referrer === 'getting-started' && projectId === createdProject?.id;
}, [referrer, projectId, createdProject?.id]);
Expand Down Expand Up @@ -235,9 +195,6 @@ export function CreateProject() {
missingValues.isMissingMessagingIntegrationChannel,
].filter(value => value).length;

const canSubmitForm =
!createProject.isPending && canUserCreateProject && formErrorCount === 0;

const submitTooltipText = getSubmitTooltipText({
...missingValues,
formErrorCount,
Expand Down Expand Up @@ -284,17 +241,15 @@ export function CreateProject() {
let projectToRollback: Project | undefined;

try {
const project = await createProject.mutateAsync({
name: projectName,
const {project, ruleIds} = await createProjectAndRules.mutateAsync({
projectName,
platform: selectedPlatform,
default_rules: alertRuleConfig?.defaultRules ?? true,
firstTeamSlug: team,
team,
alertRuleConfig,
createNotificationAction,
});

projectToRollback = project;

const ruleIds = await createRules({project, alertRuleConfig});

trackAnalytics('project_creation_page.created', {
organization,
issue_alert: alertRuleConfig?.defaultRules
Expand Down Expand Up @@ -341,7 +296,6 @@ export function CreateProject() {
)
);
} catch (error) {
setErrors(error.responseJSON);
addErrorMessage(t('Failed to create project %s', `${projectName}`));

// Only log this if the error is something other than:
Expand Down Expand Up @@ -373,7 +327,14 @@ export function CreateProject() {
}
}
},
[createRules, organization, createProject, setCreatedProject, navigate, api]
[
organization,
setCreatedProject,
navigate,
api,
createProjectAndRules,
createNotificationAction,
]
);

const handleProjectCreation = useCallback(
Expand Down Expand Up @@ -428,6 +389,11 @@ export function CreateProject() {
[configurePlatform, organization]
);

const debounceHandleProjectCreation = useMemo(
() => debounce(handleProjectCreation, 2000, {leading: true, trailing: false}),
[handleProjectCreation]
);

const handlePlatformChange = useCallback(
(value: Platform | null) => {
if (!value) {
Expand Down Expand Up @@ -549,21 +515,22 @@ export function CreateProject() {
<Button
data-test-id="create-project"
priority="primary"
disabled={!canSubmitForm}
onClick={() => handleProjectCreation(formData)}
disabled={!(canUserCreateProject && formErrorCount === 0)}
busy={createProjectAndRules.isPending}
Comment on lines +518 to +519
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make a distinction between busy and disabled here

onClick={() => debounceHandleProjectCreation(formData)}
>
{t('Create Project')}
</Button>
</Tooltip>
</div>
</FormFieldGroup>
{errors && (
{createProjectAndRules.isError && createProjectAndRules.error.responseJSON && (
<Alert.Container>
<Alert type="error">
{Object.keys(errors).map(key => (
{Object.keys(createProjectAndRules.error.responseJSON).map(key => (
<div key={key}>
<strong>{keyToErrorText[key] ?? startCase(key)}</strong>:{' '}
{(errors as any)[key]}
{(createProjectAndRules.error.responseJSON as any)[key]}
</div>
))}
</Alert>
Expand Down
Loading