codeLocations.tsx 3.1 KB

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