codeLocations.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import isArray from 'lodash/isArray';
  4. import {Button} from 'sentry/components/button';
  5. import DefaultTitle from 'sentry/components/events/interfaces/frame/defaultTitle';
  6. import {tn} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {Frame, PageFilters} from 'sentry/types';
  9. import {hasDDMExperimentalFeature} from 'sentry/utils/metrics/features';
  10. import {useMetricsCodeLocations} from 'sentry/utils/metrics/useMetricsCodeLocations';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. export function CodeLocations({
  13. mri,
  14. projects,
  15. }: {
  16. mri: string;
  17. projects?: PageFilters['projects'];
  18. }) {
  19. const {data} = useMetricsCodeLocations(mri, projects);
  20. const [isExpanded, setIsExpanded] = useState(false);
  21. const organization = useOrganization();
  22. if (!hasDDMExperimentalFeature(organization)) {
  23. return null;
  24. }
  25. if (!isArray(data?.codeLocations) || data?.codeLocations.length === 0) {
  26. return null;
  27. }
  28. const codeLocations = data?.codeLocations;
  29. if (!codeLocations) {
  30. return null;
  31. }
  32. const frameToShow = codeLocations[codeLocations.length - 1].frames[0];
  33. const otherFrames = codeLocations[codeLocations.length - 1].frames.slice(1) ?? [];
  34. if (!frameToShow) {
  35. return null;
  36. }
  37. return (
  38. <Wrapper>
  39. <DefaultLine className="title">
  40. <DefaultLineTitleWrapper>
  41. <LeftLineTitle>
  42. <DefaultTitle
  43. frame={frameToShow as Frame}
  44. platform="other"
  45. isHoverPreviewed={false}
  46. />
  47. </LeftLineTitle>
  48. </DefaultLineTitleWrapper>
  49. {otherFrames.length > 0 && (
  50. <ToggleButton size="xs" borderless onClick={() => setIsExpanded(curr => !curr)}>
  51. {isExpanded
  52. ? tn(
  53. 'Hide %s more code location',
  54. 'Hide %s more code locations',
  55. otherFrames.length
  56. )
  57. : tn(
  58. 'Show %s more code location',
  59. 'Show %s more code locations',
  60. otherFrames.length
  61. )}
  62. </ToggleButton>
  63. )}
  64. </DefaultLine>
  65. {isExpanded &&
  66. otherFrames.map(frame => (
  67. <DefaultLine className="title" key={frame.absPath}>
  68. <DefaultLineTitleWrapper>
  69. <LeftLineTitle>
  70. <DefaultTitle
  71. frame={frame as Frame}
  72. platform="other"
  73. isHoverPreviewed={false}
  74. />
  75. </LeftLineTitle>
  76. </DefaultLineTitleWrapper>
  77. </DefaultLine>
  78. ))}
  79. </Wrapper>
  80. );
  81. }
  82. const ToggleButton = styled(Button)`
  83. color: ${p => p.theme.subText};
  84. font-style: italic;
  85. font-weight: normal;
  86. padding: ${space(0.25)} ${space(0.5)};
  87. &:hover {
  88. color: ${p => p.theme.subText};
  89. }
  90. `;
  91. const Wrapper = styled('div')`
  92. margin-top: ${space(1)};
  93. background-color: ${p => p.theme.backgroundTertiary};
  94. padding: ${space(0.25)} ${space(0.5)};
  95. border-radius: ${p => p.theme.borderRadius};
  96. `;
  97. const DefaultLine = styled('div')`
  98. display: flex;
  99. justify-content: space-between;
  100. align-items: center;
  101. `;
  102. const DefaultLineTitleWrapper = styled('div')`
  103. display: flex;
  104. align-items: center;
  105. justify-content: space-between;
  106. font-style: italic;
  107. `;
  108. const LeftLineTitle = styled('div')`
  109. display: flex;
  110. align-items: center;
  111. `;