codeLocations.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 {hasDdmAlertsSupport} 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 (!hasDdmAlertsSupport(organization)) {
  23. return null;
  24. }
  25. if (!isArray(data?.codeLocations) || data?.codeLocations.length === 0) {
  26. return null;
  27. }
  28. const frameToShow = data?.codeLocations[0].frames[0];
  29. const otherFrames = data?.codeLocations[0].frames.slice(1) ?? [];
  30. if (!frameToShow) {
  31. return null;
  32. }
  33. return (
  34. <Wrapper>
  35. <DefaultLine className="title">
  36. <DefaultLineTitleWrapper>
  37. <LeftLineTitle>
  38. <DefaultTitle
  39. frame={frameToShow as Frame}
  40. platform="other"
  41. isHoverPreviewed={false}
  42. />
  43. </LeftLineTitle>
  44. </DefaultLineTitleWrapper>
  45. {otherFrames.length > 0 && (
  46. <ToggleButton size="xs" borderless onClick={() => setIsExpanded(curr => !curr)}>
  47. {isExpanded
  48. ? tn(
  49. 'Hide %s more code location',
  50. 'Hide %s more code locations',
  51. otherFrames.length
  52. )
  53. : tn(
  54. 'Show %s more code location',
  55. 'Show %s more code locations',
  56. otherFrames.length
  57. )}
  58. </ToggleButton>
  59. )}
  60. </DefaultLine>
  61. {isExpanded &&
  62. otherFrames.map(frame => (
  63. <DefaultLine className="title" key={frame.absPath}>
  64. <DefaultLineTitleWrapper>
  65. <LeftLineTitle>
  66. <DefaultTitle
  67. frame={frame as Frame}
  68. platform="other"
  69. isHoverPreviewed={false}
  70. />
  71. </LeftLineTitle>
  72. </DefaultLineTitleWrapper>
  73. </DefaultLine>
  74. ))}
  75. </Wrapper>
  76. );
  77. }
  78. const ToggleButton = styled(Button)`
  79. color: ${p => p.theme.subText};
  80. font-style: italic;
  81. font-weight: normal;
  82. padding: ${space(0.25)} ${space(0.5)};
  83. &:hover {
  84. color: ${p => p.theme.subText};
  85. }
  86. `;
  87. const Wrapper = styled('div')`
  88. margin-top: ${space(1)};
  89. background-color: ${p => p.theme.backgroundTertiary};
  90. padding: ${space(0.25)} ${space(0.5)};
  91. border-radius: ${p => p.theme.borderRadius};
  92. `;
  93. const DefaultLine = styled('div')`
  94. display: flex;
  95. justify-content: space-between;
  96. align-items: center;
  97. `;
  98. const DefaultLineTitleWrapper = styled('div')`
  99. display: flex;
  100. align-items: center;
  101. justify-content: space-between;
  102. font-style: italic;
  103. `;
  104. const LeftLineTitle = styled('div')`
  105. display: flex;
  106. align-items: center;
  107. `;