index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import {Component} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {
  5. changeProjectSlug,
  6. removeProject,
  7. transferProject,
  8. } from 'sentry/actionCreators/projects';
  9. import {hasEveryAccess} from 'sentry/components/acl/access';
  10. import {Button} from 'sentry/components/button';
  11. import Confirm from 'sentry/components/confirm';
  12. import FieldGroup from 'sentry/components/forms/fieldGroup';
  13. import TextField from 'sentry/components/forms/fields/textField';
  14. import Form, {FormProps} from 'sentry/components/forms/form';
  15. import JsonForm from 'sentry/components/forms/jsonForm';
  16. import {FieldValue} from 'sentry/components/forms/model';
  17. import Hook from 'sentry/components/hook';
  18. import ExternalLink from 'sentry/components/links/externalLink';
  19. import {removePageFiltersStorage} from 'sentry/components/organizations/pageFilters/persistence';
  20. import {Panel, PanelAlert, PanelHeader} from 'sentry/components/panels';
  21. import {fields} from 'sentry/data/forms/projectGeneralSettings';
  22. import {t, tct} from 'sentry/locale';
  23. import ProjectsStore from 'sentry/stores/projectsStore';
  24. import {Organization, Project} from 'sentry/types';
  25. import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
  26. import recreateRoute from 'sentry/utils/recreateRoute';
  27. import routeTitleGen from 'sentry/utils/routeTitle';
  28. import withOrganization from 'sentry/utils/withOrganization';
  29. import AsyncView from 'sentry/views/asyncView';
  30. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  31. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  32. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  33. type Props = AsyncView['props'] &
  34. RouteComponentProps<{projectId: string}, {}> & {
  35. onChangeSlug: (slug: string) => void;
  36. organization: Organization;
  37. };
  38. type State = AsyncView['state'] & {
  39. data: Project;
  40. };
  41. class ProjectGeneralSettings extends AsyncView<Props, State> {
  42. private _form: Record<string, FieldValue> = {};
  43. getTitle() {
  44. const {projectId} = this.props.params;
  45. return routeTitleGen(t('Project Settings'), projectId, false);
  46. }
  47. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  48. const {organization} = this.props;
  49. const {projectId} = this.props.params;
  50. return [['data', `/projects/${organization.slug}/${projectId}/`]];
  51. }
  52. handleTransferFieldChange = (id: string, value: FieldValue) => {
  53. this._form[id] = value;
  54. };
  55. handleRemoveProject = () => {
  56. const {organization} = this.props;
  57. const project = this.state.data;
  58. removePageFiltersStorage(organization.slug);
  59. if (!project) {
  60. return;
  61. }
  62. removeProject({
  63. api: this.api,
  64. orgSlug: organization.slug,
  65. projectSlug: project.slug,
  66. origin: 'settings',
  67. })
  68. .then(
  69. () => {
  70. addSuccessMessage(
  71. tct('[project] was successfully removed', {project: project.slug})
  72. );
  73. },
  74. err => {
  75. addErrorMessage(tct('Error removing [project]', {project: project.slug}));
  76. throw err;
  77. }
  78. )
  79. .then(() => {
  80. // Need to hard reload because lots of components do not listen to Projects Store
  81. window.location.assign('/');
  82. }, handleXhrErrorResponse('Unable to remove project'));
  83. };
  84. handleTransferProject = async () => {
  85. const {organization} = this.props;
  86. const project = this.state.data;
  87. if (!project) {
  88. return;
  89. }
  90. if (typeof this._form.email !== 'string' || this._form.email.length < 1) {
  91. return;
  92. }
  93. try {
  94. await transferProject(this.api, organization.slug, project, this._form.email);
  95. // Need to hard reload because lots of components do not listen to Projects Store
  96. window.location.assign('/');
  97. } catch (err) {
  98. if (err.status >= 500) {
  99. handleXhrErrorResponse('Unable to transfer project')(err);
  100. }
  101. }
  102. };
  103. isProjectAdmin = () =>
  104. hasEveryAccess(['project:admin'], {
  105. organization: this.props.organization,
  106. project: this.state.data,
  107. });
  108. renderRemoveProject() {
  109. const project = this.state.data;
  110. const isProjectAdmin = this.isProjectAdmin();
  111. const {isInternal} = project;
  112. return (
  113. <FieldGroup
  114. label={t('Remove Project')}
  115. help={tct(
  116. 'Remove the [project] project and all related data. [linebreak] Careful, this action cannot be undone.',
  117. {
  118. project: <strong>{project.slug}</strong>,
  119. linebreak: <br />,
  120. }
  121. )}
  122. >
  123. {!isProjectAdmin &&
  124. t('You do not have the required permission to remove this project.')}
  125. {isInternal &&
  126. t(
  127. 'This project cannot be removed. It is used internally by the Sentry server.'
  128. )}
  129. {isProjectAdmin && !isInternal && (
  130. <Confirm
  131. onConfirm={this.handleRemoveProject}
  132. priority="danger"
  133. confirmText={t('Remove Project')}
  134. message={
  135. <div>
  136. <TextBlock>
  137. <strong>
  138. {t('Removing this project is permanent and cannot be undone!')}
  139. </strong>
  140. </TextBlock>
  141. <TextBlock>
  142. {t('This will also remove all associated event data.')}
  143. </TextBlock>
  144. </div>
  145. }
  146. >
  147. <div>
  148. <Button priority="danger">{t('Remove Project')}</Button>
  149. </div>
  150. </Confirm>
  151. )}
  152. </FieldGroup>
  153. );
  154. }
  155. renderTransferProject() {
  156. const project = this.state.data;
  157. const isProjectAdmin = this.isProjectAdmin();
  158. const {isInternal} = project;
  159. return (
  160. <FieldGroup
  161. label={t('Transfer Project')}
  162. help={tct(
  163. 'Transfer the [project] project and all related data. [linebreak] Careful, this action cannot be undone.',
  164. {
  165. project: <strong>{project.slug}</strong>,
  166. linebreak: <br />,
  167. }
  168. )}
  169. >
  170. {!isProjectAdmin &&
  171. t('You do not have the required permission to transfer this project.')}
  172. {isInternal &&
  173. t(
  174. 'This project cannot be transferred. It is used internally by the Sentry server.'
  175. )}
  176. {isProjectAdmin && !isInternal && (
  177. <Confirm
  178. onConfirm={this.handleTransferProject}
  179. priority="danger"
  180. confirmText={t('Transfer project')}
  181. renderMessage={({confirm}) => (
  182. <div>
  183. <TextBlock>
  184. <strong>
  185. {t('Transferring this project is permanent and cannot be undone!')}
  186. </strong>
  187. </TextBlock>
  188. <TextBlock>
  189. {t(
  190. 'Please enter the email of an organization owner to whom you would like to transfer this project.'
  191. )}
  192. </TextBlock>
  193. <Panel>
  194. <Form
  195. hideFooter
  196. onFieldChange={this.handleTransferFieldChange}
  197. onSubmit={(_data, _onSuccess, _onError, e) => {
  198. e.stopPropagation();
  199. confirm();
  200. }}
  201. >
  202. <TextField
  203. name="email"
  204. label={t('Organization Owner')}
  205. placeholder="admin@example.com"
  206. required
  207. help={t(
  208. 'A request will be emailed to this address, asking the organization owner to accept the project transfer.'
  209. )}
  210. />
  211. </Form>
  212. </Panel>
  213. </div>
  214. )}
  215. >
  216. <div>
  217. <Button priority="danger">{t('Transfer Project')}</Button>
  218. </div>
  219. </Confirm>
  220. )}
  221. </FieldGroup>
  222. );
  223. }
  224. renderBody() {
  225. const {organization} = this.props;
  226. const project = this.state.data;
  227. const {projectId} = this.props.params;
  228. const endpoint = `/projects/${organization.slug}/${projectId}/`;
  229. const access = new Set(organization.access.concat(project.access));
  230. const jsonFormProps = {
  231. additionalFieldProps: {
  232. organization,
  233. },
  234. features: new Set(organization.features),
  235. access,
  236. disabled: !hasEveryAccess(['project:write'], {organization, project}),
  237. };
  238. const team = project.teams.length ? project.teams?.[0] : undefined;
  239. /*
  240. HACK: The <Form /> component applies its props to its children meaning the hooked component
  241. would need to conform to the form settings applied in a separate repository. This is
  242. not feasible to maintain and may introduce compatability errors if something changes
  243. in either repository. For that reason, the Form component is split in two, since the
  244. fields do not depend on one another, allowing for the Hook to manage it's own state.
  245. */
  246. const formProps: FormProps = {
  247. saveOnBlur: true,
  248. allowUndo: true,
  249. initialData: {
  250. ...project,
  251. team,
  252. },
  253. apiMethod: 'PUT' as const,
  254. apiEndpoint: endpoint,
  255. onSubmitSuccess: resp => {
  256. this.setState({data: resp});
  257. if (projectId !== resp.slug) {
  258. changeProjectSlug(projectId, resp.slug);
  259. // Container will redirect after stores get updated with new slug
  260. this.props.onChangeSlug(resp.slug);
  261. }
  262. // This will update our project context
  263. ProjectsStore.onUpdateSuccess(resp);
  264. },
  265. };
  266. return (
  267. <div>
  268. <SettingsPageHeader title={t('Project Settings')} />
  269. <PermissionAlert project={project} />
  270. <Form {...formProps}>
  271. <JsonForm
  272. {...jsonFormProps}
  273. title={t('Project Details')}
  274. fields={[fields.name, fields.platform]}
  275. />
  276. <JsonForm
  277. {...jsonFormProps}
  278. title={t('Email')}
  279. fields={[fields.subjectPrefix]}
  280. />
  281. </Form>
  282. <Hook
  283. name="spend-visibility:spike-protection-project-settings"
  284. project={project}
  285. />
  286. <Form {...formProps}>
  287. <JsonForm
  288. {...jsonFormProps}
  289. title={t('Event Settings')}
  290. fields={[fields.resolveAge]}
  291. />
  292. <JsonForm
  293. {...jsonFormProps}
  294. title={t('Client Security')}
  295. fields={[
  296. fields.allowedDomains,
  297. fields.scrapeJavaScript,
  298. fields.securityToken,
  299. fields.securityTokenHeader,
  300. fields.verifySSL,
  301. ]}
  302. renderHeader={() => (
  303. <PanelAlert type="info">
  304. <TextBlock noMargin>
  305. {tct(
  306. 'Configure origin URLs which Sentry should accept events from. This is used for communication with clients like [link].',
  307. {
  308. link: (
  309. <ExternalLink href="https://github.com/getsentry/sentry-javascript">
  310. sentry-javascript
  311. </ExternalLink>
  312. ),
  313. }
  314. )}{' '}
  315. {tct(
  316. 'This will restrict requests based on the [Origin] and [Referer] headers.',
  317. {
  318. Origin: <code>Origin</code>,
  319. Referer: <code>Referer</code>,
  320. }
  321. )}
  322. </TextBlock>
  323. </PanelAlert>
  324. )}
  325. />
  326. </Form>
  327. <Panel>
  328. <PanelHeader>{t('Project Administration')}</PanelHeader>
  329. {this.renderRemoveProject()}
  330. {this.renderTransferProject()}
  331. </Panel>
  332. </div>
  333. );
  334. }
  335. }
  336. type ContainerProps = {
  337. organization: Organization;
  338. } & RouteComponentProps<{projectId: string}, {}>;
  339. class ProjectGeneralSettingsContainer extends Component<ContainerProps> {
  340. componentWillUnmount() {
  341. this.unsubscribe();
  342. }
  343. changedSlug: string | undefined = undefined;
  344. unsubscribe = ProjectsStore.listen(() => this.onProjectsUpdate(), undefined);
  345. onProjectsUpdate() {
  346. if (!this.changedSlug) {
  347. return;
  348. }
  349. const project = ProjectsStore.getBySlug(this.changedSlug);
  350. if (!project) {
  351. return;
  352. }
  353. browserHistory.replace(
  354. recreateRoute('', {
  355. ...this.props,
  356. params: {
  357. ...this.props.params,
  358. projectId: this.changedSlug,
  359. },
  360. })
  361. );
  362. }
  363. render() {
  364. return (
  365. <ProjectGeneralSettings
  366. onChangeSlug={(newSlug: string) => (this.changedSlug = newSlug)}
  367. {...this.props}
  368. />
  369. );
  370. }
  371. }
  372. export default withOrganization(ProjectGeneralSettingsContainer);