subscriptionBox.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import Checkbox from 'sentry/components/checkbox';
  4. import FeatureBadge from 'sentry/components/featureBadge';
  5. import Tooltip from 'sentry/components/tooltip';
  6. import {t} from 'sentry/locale';
  7. import space from 'sentry/styles/space';
  8. import {Organization} from 'sentry/types';
  9. import withOrganization from 'sentry/utils/withOrganization';
  10. import {
  11. DESCRIPTIONS,
  12. EVENT_CHOICES,
  13. PERMISSIONS_MAP,
  14. } from 'sentry/views/settings/organizationDeveloperSettings/constants';
  15. type Resource = typeof EVENT_CHOICES[number];
  16. type DefaultProps = {
  17. webhookDisabled: boolean;
  18. };
  19. type Props = DefaultProps & {
  20. checked: boolean;
  21. disabledFromPermissions: boolean;
  22. isNew: boolean;
  23. onChange: (resource: Resource, checked: boolean) => void;
  24. organization: Organization;
  25. resource: Resource;
  26. };
  27. export class SubscriptionBox extends Component<Props> {
  28. static defaultProps: DefaultProps = {
  29. webhookDisabled: false,
  30. };
  31. onChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
  32. const checked = evt.target.checked;
  33. const {resource} = this.props;
  34. this.props.onChange(resource, checked);
  35. };
  36. render() {
  37. const {resource, organization, webhookDisabled, checked, isNew} = this.props;
  38. const features = new Set(organization.features);
  39. let disabled = this.props.disabledFromPermissions || webhookDisabled;
  40. let message = `Must have at least 'Read' permissions enabled for ${PERMISSIONS_MAP[resource]}`;
  41. if (resource === 'error' && !features.has('integrations-event-hooks')) {
  42. disabled = true;
  43. message =
  44. 'Your organization does not have access to the error subscription resource.';
  45. }
  46. if (webhookDisabled) {
  47. message = 'Cannot enable webhook subscription without specifying a webhook url';
  48. }
  49. return (
  50. <Fragment>
  51. <Tooltip disabled={!disabled} title={message} key={resource}>
  52. <SubscriptionGridItem disabled={disabled}>
  53. <SubscriptionInfo>
  54. <SubscriptionTitle>
  55. {t(`${resource}`)}
  56. {isNew && <FeatureBadge type="new" />}
  57. </SubscriptionTitle>
  58. <SubscriptionDescription>
  59. {t(`${DESCRIPTIONS[resource]}`)}
  60. </SubscriptionDescription>
  61. </SubscriptionInfo>
  62. <Checkbox
  63. key={`${resource}${checked}`}
  64. disabled={disabled}
  65. id={resource}
  66. value={resource}
  67. checked={checked}
  68. onChange={this.onChange}
  69. />
  70. </SubscriptionGridItem>
  71. </Tooltip>
  72. </Fragment>
  73. );
  74. }
  75. }
  76. export default withOrganization(SubscriptionBox);
  77. const SubscriptionGridItem = styled('div')<{disabled: boolean}>`
  78. display: flex;
  79. justify-content: space-between;
  80. background: ${p => p.theme.backgroundSecondary};
  81. opacity: ${p => (p.disabled ? 0.3 : 1)};
  82. border-radius: ${p => p.theme.borderRadius};
  83. margin: ${space(1.5)};
  84. padding: ${space(1.5)};
  85. box-sizing: border-box;
  86. `;
  87. const SubscriptionInfo = styled('div')`
  88. display: flex;
  89. flex-direction: column;
  90. align-self: center;
  91. `;
  92. const SubscriptionDescription = styled('div')`
  93. font-size: ${p => p.theme.fontSizeMedium};
  94. line-height: 1;
  95. color: ${p => p.theme.gray300};
  96. `;
  97. const SubscriptionTitle = styled('div')`
  98. font-size: ${p => p.theme.fontSizeLarge};
  99. line-height: 1;
  100. color: ${p => p.theme.textColor};
  101. white-space: nowrap;
  102. margin-bottom: ${space(0.75)};
  103. `;