permissionsObserver.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {Component, Fragment} from 'react';
  2. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  3. import {t} from 'sentry/locale';
  4. import {Permissions, Scope, WebhookEvent} from 'sentry/types';
  5. import {toResourcePermissions} from 'sentry/utils/consolidatedScopes';
  6. import PermissionSelection from 'sentry/views/settings/organizationDeveloperSettings/permissionSelection';
  7. import Subscriptions from 'sentry/views/settings/organizationDeveloperSettings/resourceSubscriptions';
  8. type DefaultProps = {
  9. appPublished: boolean;
  10. webhookDisabled: boolean;
  11. };
  12. type Props = DefaultProps & {
  13. events: WebhookEvent[];
  14. scopes: Scope[];
  15. };
  16. type State = {
  17. events: WebhookEvent[];
  18. permissions: Permissions;
  19. };
  20. export default class PermissionsObserver extends Component<Props, State> {
  21. static defaultProps: DefaultProps = {
  22. webhookDisabled: false,
  23. appPublished: false,
  24. };
  25. constructor(props: Props) {
  26. super(props);
  27. this.state = {
  28. permissions: this.scopeListToPermissionState(),
  29. events: this.props.events,
  30. };
  31. }
  32. /**
  33. * Converts the list of raw API scopes passed in to an object that can
  34. * before stored and used via `state`. This object is structured by
  35. * resource and holds "Permission" values. For example:
  36. *
  37. * {
  38. * 'Project': 'read',
  39. * ...,
  40. * }
  41. *
  42. */
  43. scopeListToPermissionState() {
  44. return toResourcePermissions(this.props.scopes);
  45. }
  46. onPermissionChange = (permissions: Permissions) => {
  47. this.setState({permissions});
  48. };
  49. onEventChange = (events: WebhookEvent[]) => {
  50. this.setState({events});
  51. };
  52. render() {
  53. const {permissions, events} = this.state;
  54. return (
  55. <Fragment>
  56. <Panel>
  57. <PanelHeader>{t('Permissions')}</PanelHeader>
  58. <PanelBody>
  59. <PermissionSelection
  60. permissions={permissions}
  61. onChange={this.onPermissionChange}
  62. appPublished={this.props.appPublished}
  63. />
  64. </PanelBody>
  65. </Panel>
  66. <Panel>
  67. <PanelHeader>{t('Webhooks')}</PanelHeader>
  68. <PanelBody>
  69. <Subscriptions
  70. permissions={permissions}
  71. events={events}
  72. onChange={this.onEventChange}
  73. webhookDisabled={this.props.webhookDisabled}
  74. />
  75. </PanelBody>
  76. </Panel>
  77. </Fragment>
  78. );
  79. }
  80. }