projectServiceHooks.tsx 5.0 KB

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