navigation.tsx 1.5 KB

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