projectProvider.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. type Props = RouteComponentProps<RouteParams, {}> & {
  12. organization: Organization;
  13. api: Client;
  14. children?: React.ReactNode;
  15. hasMetricAlerts: boolean;
  16. };
  17. type RouteParams = {
  18. projectId: string;
  19. };
  20. function AlertBuilderProjectProvider(props: Props) {
  21. const {children, params, organization, api, ...other} = props;
  22. const {projectId} = params;
  23. return (
  24. <Projects orgId={organization.slug} allProjects>
  25. {({projects, initiallyLoaded, isIncomplete}) => {
  26. if (!initiallyLoaded) {
  27. return <LoadingIndicator />;
  28. }
  29. const project = (projects as Project[]).find(({slug}) => slug === projectId);
  30. // if loaded, but project fetching states incomplete or project can't be found, project doesn't exist
  31. if (isIncomplete || !project) {
  32. return (
  33. <Alert type="warning">
  34. {t('The project you were looking for was not found.')}
  35. </Alert>
  36. );
  37. }
  38. // fetch members list for mail action fields
  39. fetchOrgMembers(api, organization.slug, [project.id]);
  40. return (
  41. <React.Fragment>
  42. {children && React.isValidElement(children)
  43. ? React.cloneElement(children, {
  44. ...other,
  45. ...children.props,
  46. project,
  47. organization,
  48. })
  49. : children}
  50. </React.Fragment>
  51. );
  52. }}
  53. </Projects>
  54. );
  55. }
  56. export default withApi(AlertBuilderProjectProvider);