organizationContainer.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, or a user not being a member of any org.
  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 (
  33. error &&
  34. errorType &&
  35. [
  36. ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS,
  37. ORGANIZATION_FETCH_ERROR_TYPES.NO_ORGS,
  38. ].includes(errorType)
  39. ) {
  40. return children;
  41. }
  42. if (error) {
  43. const errorBody =
  44. errorType === ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS ? (
  45. <Alert type="error" data-test-id="org-access-error">
  46. {t('You do not have access to this organization.')}
  47. </Alert>
  48. ) : errorType === ORGANIZATION_FETCH_ERROR_TYPES.ORG_NOT_FOUND ? (
  49. <Alert type="error" data-test-id="org-loading-error">
  50. {t('The organization you were looking for was not found.')}
  51. </Alert>
  52. ) : (
  53. <LoadingError />
  54. );
  55. return <ErrorWrapper>{errorBody}</ErrorWrapper>;
  56. }
  57. return children;
  58. }
  59. const ErrorWrapper = styled('div')`
  60. padding: ${space(3)};
  61. `;
  62. export default OrganizationContainer;