123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- 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/metrics/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 <LoadingIndicator />;
- }
- if (isError) {
- return <LoadingError onRetry={refetch} />;
- }
- if (!mri) {
- return (
- <CenterContent>
- <EmptyMessage
- style={{margin: 'auto'}}
- icon={<IconSearch size="xxl" />}
- title={t('Nothing to show!')}
- description={t('Choose a metric to display data.')}
- />
- </CenterContent>
- );
- }
- if (!Array.isArray(data) || data.length === 0) {
- return (
- <CenterContent>
- <EmptyMessage
- style={{margin: 'auto'}}
- icon={<IconSearch size="xxl" />}
- title={t('Nothing to show!')}
- description={t('No code locations found for this metric.')}
- />
- </CenterContent>
- );
- }
- const codeLocations = data[0].frames ?? [];
- // We only want to show the first 5 code locations
- const codeLocationsToShow = codeLocations.slice(0, 5);
- return (
- <CodeLocationsWrapper>
- {codeLocationsToShow.slice(0, 5).map((location, index) => (
- <CodeLocation
- key={`location-${index}`}
- codeLocation={location}
- isFirst={index === 0}
- isLast={index === codeLocationsToShow.length - 1}
- />
- ))}
- </CodeLocationsWrapper>
- );
- }
- 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 (
- <CodeLocationWrapper>
- <DefaultLineWrapper
- onClick={() => {
- if (!hasContext) {
- return;
- }
- toggleShowContext();
- }}
- isFirst={isFirst}
- isLast={isLast}
- hasContext={hasContext}
- >
- <DefaultLine className="title" showContext={showContext}>
- <DefaultLineTitleWrapper>
- <LeftLineTitle>
- <DefaultTitle
- frame={codeLocation as Frame}
- isHoverPreviewed={false}
- platform="other"
- />
- </LeftLineTitle>
- <DefaultLineActionButtons>
- <CopyToClipboardButton
- text={`${codeLocation.filename}:${codeLocation.lineNo}`}
- size="zero"
- iconSize="xs"
- borderless
- onClick={e => {
- e.stopPropagation();
- }}
- />
- <ToggleCodeLocationContextButton
- disabled={!hasContext}
- isToggled={showContext}
- handleToggle={toggleShowContext}
- />
- </DefaultLineActionButtons>
- </DefaultLineTitleWrapper>
- </DefaultLine>
- {showContext && hasContext && (
- <CodeLocationContext codeLocation={codeLocation} isLast={isLast} />
- )}
- </DefaultLineWrapper>
- </CodeLocationWrapper>
- );
- }
- type ToggleCodeLocationContextButtonProps = {
- disabled: boolean;
- handleToggle: () => void;
- isToggled: boolean;
- };
- function ToggleCodeLocationContextButton({
- disabled,
- isToggled,
- handleToggle,
- }: ToggleCodeLocationContextButtonProps) {
- return (
- <Button
- title={disabled ? t('No context available') : t('Toggle Context')}
- size="zero"
- onClick={event => {
- event.stopPropagation();
- handleToggle();
- }}
- disabled={disabled}
- >
- {/* legacy size is deprecated but the icon is too big without it */}
- <IconChevron direction={isToggled ? 'up' : 'down'} size="xs" legacySize="8px" />
- </Button>
- );
- }
- 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 (
- <SourceContextWrapper isLast={isLast}>
- {preContextLines.map(line => (
- <ContextLine key={`pre-${line[0]}-${line[1]}`} line={line} isActive={false} />
- ))}
- <ContextLine line={[lineNo, codeLocation.contextLine ?? '']} isActive />
- {postContextLines.map(line => (
- <ContextLine key={`post-${line[0]}-${line[1]}`} line={line} isActive={false} />
- ))}
- </SourceContextWrapper>
- );
- }
- 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};
- 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%;
- `;
|