organizationSettingsNavigation.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {Component} from 'react';
  2. import HookStore from 'sentry/stores/hookStore';
  3. import {Organization} from 'sentry/types';
  4. import {HookName, Hooks} from 'sentry/types/hooks';
  5. import withOrganization from 'sentry/utils/withOrganization';
  6. import SettingsNavigation from 'sentry/views/settings/components/settingsNavigation';
  7. import navigationConfiguration from 'sentry/views/settings/organization/navigationConfiguration';
  8. import {NavigationSection} from 'sentry/views/settings/types';
  9. type Props = {
  10. organization: Organization;
  11. };
  12. type State = {
  13. hookConfigs: NavigationSection[];
  14. hooks: React.ReactElement[];
  15. };
  16. class OrganizationSettingsNavigation extends Component<Props, State> {
  17. state: State = this.getHooks();
  18. componentDidMount() {
  19. // eslint-disable-next-line react/no-did-mount-set-state
  20. this.setState(this.getHooks());
  21. }
  22. componentWillUnmount() {
  23. this.unsubscribe();
  24. }
  25. /**
  26. * TODO(epurkhiser): Becase the settings organization navigation hooks
  27. * do not conform to a normal component style hook, and take a single
  28. * parameter 'organization', we cannot use the `Hook` component here,
  29. * and must resort to using listening to the HookStore to retrieve hook data.
  30. *
  31. * We should update the hook interface for the two hooks used here
  32. */
  33. unsubscribe = HookStore.listen(
  34. (hookName: HookName, hooks: Hooks['settings:organization-navigation-config'][]) => {
  35. this.handleHooks(hookName, hooks);
  36. },
  37. undefined
  38. );
  39. getHooks() {
  40. // Allow injection via getsentry et all
  41. const {organization} = this.props as Props;
  42. return {
  43. hookConfigs: HookStore.get('settings:organization-navigation-config').map(cb =>
  44. cb(organization)
  45. ),
  46. hooks: HookStore.get('settings:organization-navigation').map(cb =>
  47. cb(organization)
  48. ),
  49. };
  50. }
  51. handleHooks(name: HookName, hooks: Hooks['settings:organization-navigation-config'][]) {
  52. const org = this.props.organization;
  53. if (name !== 'settings:organization-navigation-config') {
  54. return;
  55. }
  56. this.setState({hookConfigs: hooks.map(cb => cb(org))});
  57. }
  58. render() {
  59. const {hooks, hookConfigs} = this.state as State;
  60. const {organization} = this.props as Props;
  61. const access = new Set(organization.access);
  62. const features = new Set(organization.features);
  63. return (
  64. <SettingsNavigation
  65. navigationObjects={navigationConfiguration}
  66. access={access}
  67. features={features}
  68. organization={organization}
  69. hooks={hooks}
  70. hookConfigs={hookConfigs}
  71. />
  72. );
  73. }
  74. }
  75. export default withOrganization(OrganizationSettingsNavigation);