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