organizationContainer.tsx 2.1 KB

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