root.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import {useEffect} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {DEFAULT_APP_ROUTE} from 'sentry/constants';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  6. import replaceRouterParams from 'sentry/utils/replaceRouterParams';
  7. /**
  8. * This view is used when a user lands on the route `/` which historically
  9. * is a server-rendered route which redirects the user to their last selected organization
  10. *
  11. * However, this does not work when in the experimental SPA mode (e.g. developing against a remote API,
  12. * or a deploy preview), so we must replicate the functionality and redirect
  13. * the user to the proper organization.
  14. *
  15. * TODO: There might be an edge case where user does not have `lastOrganization` set,
  16. * in which case we should load their list of organizations and make a decision
  17. */
  18. function AppRoot() {
  19. const {lastOrganization} = useLegacyStore(ConfigStore);
  20. useEffect(() => {
  21. if (!lastOrganization) {
  22. return;
  23. }
  24. const orgSlug = lastOrganization;
  25. const url = replaceRouterParams(DEFAULT_APP_ROUTE, {orgSlug});
  26. browserHistory.replace(url);
  27. }, [lastOrganization]);
  28. return null;
  29. }
  30. export default AppRoot;