projectServiceHooks.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. clearIndicators,
  7. } from 'sentry/actionCreators/indicator';
  8. import {Button} from 'sentry/components/button';
  9. import EmptyMessage from 'sentry/components/emptyMessage';
  10. import FieldGroup from 'sentry/components/forms/fieldGroup';
  11. import Link from 'sentry/components/links/link';
  12. import {Panel, PanelAlert, PanelBody, PanelHeader} from 'sentry/components/panels';
  13. import Switch from 'sentry/components/switchButton';
  14. import Truncate from 'sentry/components/truncate';
  15. import {IconAdd} from 'sentry/icons';
  16. import {t} from 'sentry/locale';
  17. import {Organization, ServiceHook} from 'sentry/types';
  18. import withOrganization from 'sentry/utils/withOrganization';
  19. import AsyncView from 'sentry/views/asyncView';
  20. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  21. type RowProps = {
  22. hook: ServiceHook;
  23. onToggleActive: () => void;
  24. orgId: string;
  25. projectId: string;
  26. };
  27. function ServiceHookRow({orgId, projectId, hook, onToggleActive}: RowProps) {
  28. return (
  29. <FieldGroup
  30. label={
  31. <Link
  32. data-test-id="project-service-hook"
  33. to={`/settings/${orgId}/projects/${projectId}/hooks/${hook.id}/`}
  34. >
  35. <Truncate value={hook.url} />
  36. </Link>
  37. }
  38. help={
  39. <small>
  40. {hook.events && hook.events.length !== 0 ? (
  41. hook.events.join(', ')
  42. ) : (
  43. <em>{t('no events configured')}</em>
  44. )}
  45. </small>
  46. }
  47. >
  48. <Switch isActive={hook.status === 'active'} size="lg" toggle={onToggleActive} />
  49. </FieldGroup>
  50. );
  51. }
  52. type Props = RouteComponentProps<{projectId: string}, {}> & {
  53. organization: Organization;
  54. };
  55. type State = {
  56. hookList: null | ServiceHook[];
  57. } & AsyncView['state'];
  58. class ProjectServiceHooks extends AsyncView<Props, State> {
  59. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  60. const {organization, params} = this.props;
  61. const projectId = params.projectId;
  62. return [['hookList', `/projects/${organization.slug}/${projectId}/hooks/`]];
  63. }
  64. onToggleActive = (hook: ServiceHook) => {
  65. const {organization, params} = this.props;
  66. const {hookList} = this.state;
  67. if (!hookList) {
  68. return;
  69. }
  70. addLoadingMessage(t('Saving changes\u2026'));
  71. this.api.request(
  72. `/projects/${organization.slug}/${params.projectId}/hooks/${hook.id}/`,
  73. {
  74. method: 'PUT',
  75. data: {
  76. isActive: hook.status !== 'active',
  77. },
  78. success: data => {
  79. clearIndicators();
  80. this.setState({
  81. hookList: hookList.map(h => {
  82. if (h.id === data.id) {
  83. return {
  84. ...h,
  85. ...data,
  86. };
  87. }
  88. return h;
  89. }),
  90. });
  91. },
  92. error: () => {
  93. addErrorMessage(t('Unable to remove application. Please try again.'));
  94. },
  95. }
  96. );
  97. };
  98. renderEmpty() {
  99. return (
  100. <EmptyMessage>
  101. {t('There are no service hooks associated with this project.')}
  102. </EmptyMessage>
  103. );
  104. }
  105. renderResults() {
  106. const {organization, params} = this.props;
  107. return (
  108. <Fragment>
  109. <PanelHeader key="header">{t('Service Hook')}</PanelHeader>
  110. <PanelBody key="body">
  111. <PanelAlert type="info" showIcon>
  112. {t(
  113. 'Service Hooks are an early adopter preview feature and will change in the future.'
  114. )}
  115. </PanelAlert>
  116. {this.state.hookList?.map(hook => (
  117. <ServiceHookRow
  118. key={hook.id}
  119. orgId={organization.slug}
  120. projectId={params.projectId}
  121. hook={hook}
  122. onToggleActive={this.onToggleActive.bind(this, hook)}
  123. />
  124. ))}
  125. </PanelBody>
  126. </Fragment>
  127. );
  128. }
  129. renderBody() {
  130. const {hookList} = this.state;
  131. const body =
  132. hookList && hookList.length > 0 ? this.renderResults() : this.renderEmpty();
  133. const {organization, params} = this.props;
  134. const access = new Set(organization.access);
  135. return (
  136. <Fragment>
  137. <SettingsPageHeader
  138. title={t('Service Hooks')}
  139. action={
  140. access.has('project:write') ? (
  141. <Button
  142. data-test-id="new-service-hook"
  143. to={`/settings/${organization.slug}/projects/${params.projectId}/hooks/new/`}
  144. size="sm"
  145. priority="primary"
  146. icon={<IconAdd size="xs" isCircled />}
  147. >
  148. {t('Create New Hook')}
  149. </Button>
  150. ) : null
  151. }
  152. />
  153. <Panel>{body}</Panel>
  154. </Fragment>
  155. );
  156. }
  157. }
  158. export default withOrganization(ProjectServiceHooks);