navigation.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {InjectedRouter} from 'react-router';
  2. import {Location} from 'history';
  3. import {openModal} from 'sentry/actionCreators/modal';
  4. import ContextPickerModal from 'sentry/components/contextPickerModal';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import replaceRouterParams from 'sentry/utils/replaceRouterParams';
  7. // TODO(ts): figure out better typing for react-router here
  8. export function navigateTo(
  9. to: string,
  10. router: InjectedRouter & {location?: Location},
  11. configUrl?: string
  12. ) {
  13. // Check for placeholder params
  14. const needOrg = to.includes(':orgId');
  15. const needProject = to.includes(':projectId') || to.includes(':project');
  16. const comingFromProjectId = router?.location?.query?.project;
  17. const needProjectId = !comingFromProjectId || Array.isArray(comingFromProjectId);
  18. const projectById = ProjectsStore.getById(comingFromProjectId);
  19. if (needOrg || (needProject && (needProjectId || !projectById)) || configUrl) {
  20. openModal(
  21. modalProps => (
  22. <ContextPickerModal
  23. {...modalProps}
  24. nextPath={to}
  25. needOrg={needOrg}
  26. needProject={needProject}
  27. configUrl={configUrl}
  28. onFinish={path => {
  29. modalProps.closeModal();
  30. return window.setTimeout(() => router.push(path), 0);
  31. }}
  32. />
  33. ),
  34. {}
  35. );
  36. } else {
  37. if (projectById) {
  38. to = replaceRouterParams(to, {
  39. projectId: projectById.slug,
  40. project: projectById.id,
  41. });
  42. }
  43. router.push(to);
  44. }
  45. }