navigation.tsx 1.6 KB

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