organizationSettingsNavigation.tsx 2.8 KB

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