usePageTabs.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {useCallback} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {Location} from 'history';
  4. import {Organization, Project} from 'sentry/types';
  5. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  6. import {decodeScalar} from 'sentry/utils/queryString';
  7. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  8. import {getSelectedProjectPlatforms} from '../../utils';
  9. import {anomaliesRouteWithQuery} from '../transactionAnomalies/utils';
  10. import {eventsRouteWithQuery} from '../transactionEvents/utils';
  11. import {profilesRouteWithQuery} from '../transactionProfiles/utils';
  12. import {replaysRouteWithQuery} from '../transactionReplays/utils';
  13. import {spansRouteWithQuery} from '../transactionSpans/utils';
  14. import {tagsRouteWithQuery} from '../transactionTags/utils';
  15. import {vitalsRouteWithQuery} from '../transactionVitals/utils';
  16. import {transactionSummaryRouteWithQuery} from '../utils';
  17. import Tab from './tabs';
  18. type TabEvents =
  19. | 'performance_views.vitals.vitals_tab_clicked'
  20. | 'performance_views.tags.tags_tab_clicked'
  21. | 'performance_views.events.events_tab_clicked'
  22. | 'performance_views.spans.spans_tab_clicked'
  23. | 'performance_views.anomalies.anomalies_tab_clicked';
  24. const TAB_ANALYTICS: Partial<Record<Tab, TabEvents>> = {
  25. [Tab.WebVitals]: 'performance_views.vitals.vitals_tab_clicked',
  26. [Tab.Tags]: 'performance_views.tags.tags_tab_clicked',
  27. [Tab.Events]: 'performance_views.events.events_tab_clicked',
  28. [Tab.Spans]: 'performance_views.spans.spans_tab_clicked',
  29. [Tab.Anomalies]: 'performance_views.anomalies.anomalies_tab_clicked',
  30. };
  31. type Opts = {
  32. location: Location;
  33. organization: Organization;
  34. projectId: undefined | string;
  35. projects: Project[];
  36. tab: Tab;
  37. transactionName: undefined | string;
  38. };
  39. function usePageTabs({
  40. location,
  41. organization,
  42. projectId,
  43. projects,
  44. tab,
  45. transactionName,
  46. }: Opts) {
  47. const getNewRoute = useCallback(
  48. (newTab: Tab) => {
  49. if (!transactionName) {
  50. return {};
  51. }
  52. const routeQuery = {
  53. orgSlug: organization.slug,
  54. transaction: transactionName,
  55. projectID: projectId,
  56. query: location.query,
  57. };
  58. switch (newTab) {
  59. case Tab.Tags:
  60. return tagsRouteWithQuery(routeQuery);
  61. case Tab.Events:
  62. return eventsRouteWithQuery(routeQuery);
  63. case Tab.Spans:
  64. return spansRouteWithQuery(routeQuery);
  65. case Tab.Anomalies:
  66. return anomaliesRouteWithQuery(routeQuery);
  67. case Tab.Replays:
  68. return replaysRouteWithQuery(routeQuery);
  69. case Tab.Profiling: {
  70. return profilesRouteWithQuery(routeQuery);
  71. }
  72. case Tab.WebVitals:
  73. return vitalsRouteWithQuery({
  74. orgSlug: organization.slug,
  75. transaction: transactionName,
  76. projectID: decodeScalar(location.query.project),
  77. query: location.query,
  78. });
  79. case Tab.TransactionSummary:
  80. default:
  81. return transactionSummaryRouteWithQuery(routeQuery);
  82. }
  83. },
  84. [location.query, organization.slug, projectId, transactionName]
  85. );
  86. const onTabChange = useCallback(
  87. (newTab: Tab) => {
  88. // Prevent infinite rerenders
  89. if (newTab === tab) {
  90. return;
  91. }
  92. const analyticsKey = TAB_ANALYTICS[newTab];
  93. if (analyticsKey) {
  94. trackAdvancedAnalyticsEvent(analyticsKey, {
  95. organization,
  96. project_platforms: getSelectedProjectPlatforms(location, projects),
  97. });
  98. }
  99. browserHistory.push(normalizeUrl(getNewRoute(newTab)));
  100. },
  101. [getNewRoute, tab, organization, location, projects]
  102. );
  103. return {
  104. onTabChange,
  105. };
  106. }
  107. export default usePageTabs;