organizationSettingsNavigation.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {Component} from 'react';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import HookStore from 'sentry/stores/hookStore';
  4. import type {HookName, Hooks} from 'sentry/types/hooks';
  5. import type {Organization} from 'sentry/types/organization';
  6. import withOrganization from 'sentry/utils/withOrganization';
  7. import SettingsNavigation from 'sentry/views/settings/components/settingsNavigation';
  8. import navigationConfiguration from 'sentry/views/settings/organization/navigationConfiguration';
  9. import type {NavigationSection} from 'sentry/views/settings/types';
  10. type Props = {
  11. organization: Organization;
  12. };
  13. type State = {
  14. hookConfigs: NavigationSection[];
  15. hooks: React.ReactElement[];
  16. };
  17. class OrganizationSettingsNavigation extends Component<Props, State> {
  18. state: State = this.getHooks();
  19. componentDidMount() {
  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. (
  35. hookName: HookName,
  36. hooks: Array<Hooks['settings:organization-navigation-config']>
  37. ) => {
  38. this.handleHooks(hookName, hooks);
  39. },
  40. undefined
  41. );
  42. getHooks() {
  43. // Allow injection via getsentry et all
  44. const {organization} = this.props as Props;
  45. return {
  46. hookConfigs: HookStore.get('settings:organization-navigation-config').map(cb =>
  47. cb(organization)
  48. ),
  49. hooks: HookStore.get('settings:organization-navigation').map(cb =>
  50. cb(organization)
  51. ),
  52. };
  53. }
  54. handleHooks(
  55. name: HookName,
  56. hooks: Array<Hooks['settings:organization-navigation-config']>
  57. ) {
  58. const org = this.props.organization;
  59. if (name !== 'settings:organization-navigation-config') {
  60. return;
  61. }
  62. this.setState({hookConfigs: hooks.map(cb => cb(org))});
  63. }
  64. render() {
  65. const {hooks, hookConfigs} = this.state;
  66. const {organization} = this.props as Props;
  67. const access = new Set(organization.access);
  68. const features = new Set(organization.features);
  69. const isSelfHosted = ConfigStore.get('isSelfHosted');
  70. return (
  71. <SettingsNavigation
  72. navigationObjects={navigationConfiguration}
  73. access={access}
  74. features={features}
  75. organization={organization}
  76. hooks={hooks}
  77. hookConfigs={hookConfigs}
  78. isSelfHosted={isSelfHosted}
  79. />
  80. );
  81. }
  82. }
  83. export default withOrganization(OrganizationSettingsNavigation);