useGroupDetailsRoute.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  2. import type {Organization} from 'sentry/types/organization';
  3. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import {useParams} from 'sentry/utils/useParams';
  6. import useRouter from 'sentry/utils/useRouter';
  7. import {Tab, TabPaths} from 'sentry/views/issueDetails/types';
  8. type RouteProps = RouteComponentProps<{groupId: string; eventId?: string}, {}>;
  9. function getCurrentTab({router}: {router: RouteProps['router']}) {
  10. const currentRoute = router.routes[router.routes.length - 1];
  11. // If we're in the tag details page ("/tags/:tagKey/")
  12. if (router.params.tagKey) {
  13. return Tab.TAGS;
  14. }
  15. return (
  16. Object.values(Tab).find(tab => currentRoute?.path === TabPaths[tab]) ?? Tab.DETAILS
  17. );
  18. }
  19. function getCurrentRouteInfo({
  20. groupId,
  21. eventId,
  22. organization,
  23. router,
  24. }: {
  25. eventId: string | undefined;
  26. groupId: string;
  27. organization: Organization;
  28. router: RouteProps['router'];
  29. }): {
  30. baseUrl: string;
  31. currentTab: Tab;
  32. } {
  33. const currentTab = getCurrentTab({router});
  34. const baseUrl = normalizeUrl(
  35. `/organizations/${organization.slug}/issues/${groupId}/${
  36. router.params.eventId && eventId ? `events/${eventId}/` : ''
  37. }`
  38. );
  39. return {baseUrl, currentTab};
  40. }
  41. export function useGroupDetailsRoute(): {
  42. baseUrl: string;
  43. currentTab: Tab;
  44. } {
  45. const organization = useOrganization();
  46. const params = useParams<{groupId: string; eventId?: string}>();
  47. const router = useRouter();
  48. return getCurrentRouteInfo({
  49. groupId: params.groupId,
  50. eventId: params.eventId,
  51. organization,
  52. router,
  53. });
  54. }