import {useCallback, useMemo, useState} from 'react'; import styled from '@emotion/styled'; import {Button} from 'sentry/components/button'; import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton'; import EmptyMessage from 'sentry/components/emptyMessage'; import ContextLine from 'sentry/components/events/interfaces/frame/contextLine'; import DefaultTitle from 'sentry/components/events/interfaces/frame/defaultTitle'; import LoadingError from 'sentry/components/loadingError'; import LoadingIndicator from 'sentry/components/loadingIndicator'; import {IconChevron, IconSearch} from 'sentry/icons'; import {t} from 'sentry/locale'; import {space} from 'sentry/styles/space'; import type {Frame, MRI} from 'sentry/types'; import type {MetricCodeLocationFrame} from 'sentry/utils/metrics/types'; import {useMetricCodeLocations} from 'sentry/utils/metrics/useMetricsCodeLocations'; import type {SelectionRange} from 'sentry/views/ddm/chart/types'; interface CodeLocationsProps extends SelectionRange { mri?: MRI; } export function CodeLocations({mri, ...rangeOpts}: CodeLocationsProps) { const {data, isFetching, isError, refetch} = useMetricCodeLocations(mri, rangeOpts); if (isFetching) { return ; } if (isError) { return ; } if (!mri) { return ( } title={t('Nothing to show!')} description={t('Choose a metric to display data.')} /> ); } if (!Array.isArray(data) || data.length === 0) { return ( } title={t('Nothing to show!')} description={t('No code locations found for this metric.')} /> ); } const codeLocations = data[0].frames ?? []; // We only want to show the first 5 code locations const codeLocationsToShow = codeLocations.slice(0, 5); return ( {codeLocationsToShow.slice(0, 5).map((location, index) => ( ))} ); } type CodeLocationProps = { codeLocation: MetricCodeLocationFrame; isFirst?: boolean; isLast?: boolean; }; function CodeLocation({codeLocation, isFirst, isLast}: CodeLocationProps) { const [showContext, setShowContext] = useState(!!isFirst); const toggleShowContext = useCallback(() => { setShowContext(prevState => !prevState); }, []); const hasContext = !!codeLocation.contextLine; return ( { if (!hasContext) { return; } toggleShowContext(); }} isFirst={isFirst} isLast={isLast} hasContext={hasContext} > {showContext && hasContext && ( )} ); } type ToggleCodeLocationContextButtonProps = { disabled: boolean; handleToggle: () => void; isToggled: boolean; }; function ToggleCodeLocationContextButton({ disabled, isToggled, handleToggle, }: ToggleCodeLocationContextButtonProps) { return ( ); } type CodeLocationContextProps = { codeLocation: MetricCodeLocationFrame; isLast?: boolean; }; function CodeLocationContext({codeLocation, isLast}: CodeLocationContextProps) { const lineNo = codeLocation.lineNo ?? 0; const preContextLines: [number, string][] = useMemo( () => codeLocation.preContext?.map((line, index) => [lineNo - 5 + index, line]) ?? [], [codeLocation.preContext, lineNo] ); const postContextLines: [number, string][] = useMemo( () => codeLocation.postContext?.map((line, index) => [lineNo + index, line]) ?? [], [codeLocation.postContext, lineNo] ); return ( {preContextLines.map(line => ( ))} {postContextLines.map(line => ( ))} ); } const CodeLocationWrapper = styled('div')` display: flex; `; const DefaultLineActionButtons = styled('div')` display: flex; gap: ${space(1)}; `; const CodeLocationsWrapper = styled('div')` & code { font-family: inherit; font-size: inherit; } `; const SourceContextWrapper = styled('div')<{isLast?: boolean}>` word-wrap: break-word; font-family: ${p => p.theme.text.familyMono}; font-size: ${p => p.theme.fontSizeSmall}; /* TODO(ddm): find out how it is done on the issues page */ line-height: 24px; min-height: ${space(3)}; white-space: pre; white-space: pre-wrap; background-color: ${p => p.theme.background}; `; const DefaultLineWrapper = styled('div')<{ hasContext?: boolean; isFirst?: boolean; isLast?: boolean; }>` :hover { cursor: ${p => p.hasContext && 'pointer'}; } flex-grow: 1; border-top-left-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)}; border-top-right-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)}; border-bottom-left-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)}; border-bottom-right-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)}; border: 1px solid ${p => p.theme.border}; border-top: ${p => (p.isFirst ? `1px solid ${p.theme.border}` : 'none')}; background-color: ${p => p.theme.backgroundTertiary}; `; const DefaultLine = styled('div')<{showContext?: boolean}>` display: flex; justify-content: space-between; align-items: center; border-bottom: ${p => (p.showContext ? `1px solid ${p.theme.border}` : 'none')}; `; const DefaultLineTitleWrapper = styled('div')` width: 100%; display: flex; align-items: center; justify-content: space-between; font-size: ${p => p.theme.codeFontSize}; line-height: ${p => p.theme.fontSizeLarge}; font-style: normal; padding: ${space(0.75)} ${space(3)} ${space(0.75)} ${space(1.5)}; word-break: break-all; word-break: break-word; `; const LeftLineTitle = styled('div')` display: flex; flex-wrap: wrap; align-items: center; `; const CenterContent = styled('div')` display: flex; justify-content: center; align-items: center; height: 100%; `;