withPlugins.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import * as React from 'react';
  2. import {fetchPlugins} from 'sentry/actionCreators/plugins';
  3. import PluginsStore from 'sentry/stores/pluginsStore';
  4. import {Organization, Plugin, Project} from 'sentry/types';
  5. import {defined} from 'sentry/utils';
  6. import getDisplayName from 'sentry/utils/getDisplayName';
  7. import withOrganization from 'sentry/utils/withOrganization';
  8. import withProject from 'sentry/utils/withProject';
  9. type WithPluginProps = {
  10. organization: Organization;
  11. project?: Project;
  12. };
  13. type State = {
  14. loading: boolean;
  15. plugins: Plugin[];
  16. };
  17. /**
  18. * Higher order component that fetches list of plugins and
  19. * passes PluginsStore to component as `plugins`
  20. */
  21. function withPlugins<P extends WithPluginProps>(
  22. WrappedComponent: React.ComponentType<P>
  23. ) {
  24. class WithPlugins extends React.Component<
  25. Omit<P, keyof 'plugins'> & WithPluginProps,
  26. State
  27. > {
  28. static displayName = `withPlugins(${getDisplayName(WrappedComponent)})`;
  29. state = {plugins: [], loading: true};
  30. componentDidMount() {
  31. this.fetchPlugins();
  32. }
  33. componentDidUpdate(prevProps, _prevState, prevContext) {
  34. const {organization, project} = this.props;
  35. // Only fetch plugins when a org slug or project slug has changed
  36. const prevOrg = prevProps.organization || prevContext?.organization;
  37. const prevProject = prevProps.project || prevContext?.project;
  38. // If previous org/project is undefined then it means:
  39. // the HoC has mounted, `fetchPlugins` has been called (via cDM), and
  40. // store was updated. We don't need to fetchPlugins again (or it will cause an infinite loop)
  41. //
  42. // This is for the unusual case where component is mounted and receives a new org/project prop
  43. // e.g. when switching projects via breadcrumbs in settings.
  44. if (!defined(prevProject) || !defined(prevOrg)) {
  45. return;
  46. }
  47. const isOrgSame = prevOrg.slug === organization.slug;
  48. const isProjectSame = prevProject.slug === project?.slug;
  49. // Don't do anything if org and project are the same
  50. if (isOrgSame && isProjectSame) {
  51. return;
  52. }
  53. this.fetchPlugins();
  54. }
  55. componentWillUnmount() {
  56. this.unsubscribe();
  57. }
  58. unsubscribe = PluginsStore.listen(({plugins, loading}: State) => {
  59. // State is destructured as store updates contain additional keys
  60. // that are not exposed by this HoC
  61. this.setState({plugins, loading});
  62. }, undefined);
  63. fetchPlugins() {
  64. const {organization, project} = this.props;
  65. if (!project || !organization) {
  66. return;
  67. }
  68. fetchPlugins({projectId: project.slug, orgId: organization.slug});
  69. }
  70. render() {
  71. return (
  72. <WrappedComponent {...(this.props as P & WithPluginProps)} plugins={this.state} />
  73. );
  74. }
  75. }
  76. return withOrganization(withProject(WithPlugins));
  77. }
  78. export default withPlugins;