projectProvider.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import * as React from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {fetchOrgMembers} from 'app/actionCreators/members';
  4. import {Client} from 'app/api';
  5. import Alert from 'app/components/alert';
  6. import LoadingIndicator from 'app/components/loadingIndicator';
  7. import {t} from 'app/locale';
  8. import {Organization, Project} from 'app/types';
  9. import Projects from 'app/utils/projects';
  10. import withApi from 'app/utils/withApi';
  11. import ScrollToTop from 'app/views/settings/components/scrollToTop';
  12. type Props = RouteComponentProps<RouteParams, {}> & {
  13. organization: Organization;
  14. api: Client;
  15. children?: React.ReactNode;
  16. hasMetricAlerts: boolean;
  17. };
  18. type RouteParams = {
  19. projectId: string;
  20. };
  21. function AlertBuilderProjectProvider(props: Props) {
  22. const {children, params, organization, api, ...other} = props;
  23. const {projectId} = params;
  24. return (
  25. <Projects orgId={organization.slug} allProjects>
  26. {({projects, initiallyLoaded, isIncomplete}) => {
  27. if (!initiallyLoaded) {
  28. return <LoadingIndicator />;
  29. }
  30. const project = (projects as Project[]).find(({slug}) => slug === projectId);
  31. // if loaded, but project fetching states incomplete or project can't be found, project doesn't exist
  32. if (isIncomplete || !project) {
  33. return (
  34. <Alert type="warning">
  35. {t('The project you were looking for was not found.')}
  36. </Alert>
  37. );
  38. }
  39. // fetch members list for mail action fields
  40. fetchOrgMembers(api, organization.slug, [project.id]);
  41. return (
  42. <ScrollToTop location={props.location} disable={() => false}>
  43. {children && React.isValidElement(children)
  44. ? React.cloneElement(children, {
  45. ...other,
  46. ...children.props,
  47. project,
  48. organization,
  49. })
  50. : children}
  51. </ScrollToTop>
  52. );
  53. }}
  54. </Projects>
  55. );
  56. }
  57. export default withApi(AlertBuilderProjectProvider);