resourceSubscriptions.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import FormContext from 'sentry/components/forms/formContext';
  4. import {Permissions, WebhookEvent} from 'sentry/types';
  5. import {
  6. EVENT_CHOICES,
  7. PERMISSIONS_MAP,
  8. } from 'sentry/views/settings/organizationDeveloperSettings/constants';
  9. import SubscriptionBox from 'sentry/views/settings/organizationDeveloperSettings/subscriptionBox';
  10. type Resource = (typeof EVENT_CHOICES)[number];
  11. type DefaultProps = {
  12. webhookDisabled: boolean;
  13. };
  14. type Props = DefaultProps & {
  15. events: WebhookEvent[];
  16. onChange: (events: WebhookEvent[]) => void;
  17. permissions: Permissions;
  18. };
  19. export default class Subscriptions extends Component<Props> {
  20. static defaultProps: DefaultProps = {
  21. webhookDisabled: false,
  22. };
  23. constructor(props: Props, context) {
  24. super(props, context);
  25. this.context.form.setValue('events', this.props.events);
  26. }
  27. componentDidUpdate(prevProps: Props) {
  28. const {webhookDisabled, permissions, events} = this.props;
  29. const permittedEvents = events.filter(
  30. resource => permissions[PERMISSIONS_MAP[resource]] !== 'no-access'
  31. );
  32. // When disabling webhooks unset the events
  33. if (!prevProps.webhookDisabled && webhookDisabled && prevProps.events.length) {
  34. this.save([]);
  35. return;
  36. }
  37. if (JSON.stringify(events) !== JSON.stringify(permittedEvents)) {
  38. this.save(permittedEvents);
  39. }
  40. }
  41. static contextType = FormContext;
  42. onChange = (resource: Resource, checked: boolean) => {
  43. const events = new Set(this.props.events);
  44. checked ? events.add(resource) : events.delete(resource);
  45. this.save(Array.from(events));
  46. };
  47. save = (events: WebhookEvent[]) => {
  48. this.props.onChange(events);
  49. this.context.form.setValue('events', events);
  50. };
  51. render() {
  52. const {permissions, webhookDisabled, events} = this.props;
  53. return (
  54. <SubscriptionGrid>
  55. {EVENT_CHOICES.map(choice => {
  56. const disabledFromPermissions =
  57. permissions[PERMISSIONS_MAP[choice]] === 'no-access';
  58. return (
  59. <Fragment key={choice}>
  60. <SubscriptionBox
  61. key={choice}
  62. disabledFromPermissions={disabledFromPermissions}
  63. webhookDisabled={webhookDisabled}
  64. checked={events.includes(choice) && !disabledFromPermissions}
  65. resource={choice}
  66. onChange={this.onChange}
  67. isNew={choice === 'comment'}
  68. />
  69. </Fragment>
  70. );
  71. })}
  72. </SubscriptionGrid>
  73. );
  74. }
  75. }
  76. const SubscriptionGrid = styled('div')`
  77. display: grid;
  78. grid-template: auto / 1fr 1fr 1fr;
  79. @media (max-width: ${props => props.theme.breakpoints.large}) {
  80. grid-template: 1fr 1fr 1fr / auto;
  81. }
  82. `;