navigation.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. comingFromProjectId={
  29. Array.isArray(comingFromProjectId) ? '' : comingFromProjectId || ''
  30. }
  31. onFinish={path => {
  32. modalProps.closeModal();
  33. return window.setTimeout(() => router.push(path), 0);
  34. }}
  35. />
  36. ),
  37. {}
  38. );
  39. } else {
  40. if (projectById) {
  41. to = replaceRouterParams(to, {
  42. projectId: projectById.slug,
  43. project: projectById.id,
  44. });
  45. }
  46. router.push(to);
  47. }
  48. }