organizationContainer.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import styled from '@emotion/styled';
  2. import {Alert} from 'sentry/components/core/alert';
  3. import LoadingError from 'sentry/components/loadingError';
  4. import LoadingTriangle from 'sentry/components/loadingTriangle';
  5. import {ORGANIZATION_FETCH_ERROR_TYPES} from 'sentry/constants';
  6. import {t} from 'sentry/locale';
  7. import OrganizationStore from 'sentry/stores/organizationStore';
  8. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  9. import {space} from 'sentry/styles/space';
  10. interface Props {
  11. children: JSX.Element;
  12. }
  13. /**
  14. * Ensures the current organization is loaded. A loading indicator will be
  15. * rendered while loading the organization.
  16. */
  17. function OrganizationContainer({children}: Props) {
  18. const {loading, error, errorType} = useLegacyStore(OrganizationStore);
  19. if (loading) {
  20. return <LoadingTriangle>{t('Loading data for your organization.')}</LoadingTriangle>;
  21. }
  22. // XXX(epurkhiser): There is a special case scenarion when we're unable to
  23. // load an organization due to access issues. Right now this is VERY SPECIFIC
  24. // to being able to enable 2FA, or a user not being a member of any org.
  25. //
  26. // In this scenario we render the children **explicitly without an
  27. // organization in context**.
  28. //
  29. // TODO(epurkhiser): This scenario desprately should be improved
  30. if (
  31. error &&
  32. errorType &&
  33. [
  34. ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS,
  35. ORGANIZATION_FETCH_ERROR_TYPES.NO_ORGS,
  36. ].includes(errorType)
  37. ) {
  38. return children;
  39. }
  40. if (error) {
  41. const errorBody =
  42. errorType === ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS ? (
  43. <Alert.Container>
  44. <Alert type="error" data-test-id="org-access-error">
  45. {t('You do not have access to this organization.')}
  46. </Alert>
  47. </Alert.Container>
  48. ) : errorType === ORGANIZATION_FETCH_ERROR_TYPES.ORG_NOT_FOUND ? (
  49. <Alert.Container>
  50. <Alert type="error" data-test-id="org-loading-error">
  51. {t('The organization you were looking for was not found.')}
  52. </Alert>
  53. </Alert.Container>
  54. ) : (
  55. <LoadingError />
  56. );
  57. return <ErrorWrapper>{errorBody}</ErrorWrapper>;
  58. }
  59. return children;
  60. }
  61. const ErrorWrapper = styled('div')`
  62. padding: ${space(3)};
  63. `;
  64. export default OrganizationContainer;