123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- import type React from 'react';
- import styled from '@emotion/styled';
- import type {Location} from 'history';
- import {openDashboardWidgetQuerySelectorModal} from 'sentry/actionCreators/modal';
- import Tag from 'sentry/components/badge/tag';
- import {Button} from 'sentry/components/button';
- import {openConfirmModal} from 'sentry/components/confirm';
- import type {MenuItemProps} from 'sentry/components/dropdownMenu';
- import {DropdownMenu} from 'sentry/components/dropdownMenu';
- import {isWidgetViewerPath} from 'sentry/components/modals/widgetViewerModal/utils';
- import {Tooltip} from 'sentry/components/tooltip';
- import {IconEllipsis, IconExpand, IconInfo} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import type {PageFilters} from 'sentry/types/core';
- import type {Series} from 'sentry/types/echarts';
- import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
- import type {Organization} from 'sentry/types/organization';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
- import type {AggregationOutputType} from 'sentry/utils/discover/fields';
- import {
- MEPState,
- useMEPSettingContext,
- } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
- import useOrganization from 'sentry/utils/useOrganization';
- import {
- getWidgetDiscoverUrl,
- getWidgetIssueUrl,
- getWidgetMetricsUrl,
- hasDatasetSelector,
- isUsingPerformanceScore,
- performanceScoreTooltip,
- } from 'sentry/views/dashboards/utils';
- import {getWidgetExploreUrl} from 'sentry/views/dashboards/utils/getWidgetExploreUrl';
- import type {Widget} from '../types';
- import {WidgetType} from '../types';
- import {WidgetViewerContext} from '../widgetViewer/widgetViewerContext';
- import {useDashboardsMEPContext} from './dashboardsMEPContext';
- type Props = {
- location: Location;
- organization: Organization;
- router: InjectedRouter;
- selection: PageFilters;
- widget: Widget;
- widgetLimitReached: boolean;
- description?: string;
- hasEditAccess?: boolean;
- index?: string;
- isPreview?: boolean;
- onDelete?: () => void;
- onDuplicate?: () => void;
- onEdit?: () => void;
- pageLinks?: string;
- seriesData?: Series[];
- seriesResultsType?: Record<string, AggregationOutputType>;
- showContextMenu?: boolean;
- tableData?: TableDataWithTitle[];
- title?: string | React.ReactNode;
- totalIssuesCount?: string;
- };
- export const useIndexedEventsWarning = (): string | null => {
- const {isMetricsData} = useDashboardsMEPContext();
- const organization = useOrganization();
- const metricSettingContext = useMEPSettingContext();
- return !organization.features.includes('performance-mep-bannerless-ui') &&
- isMetricsData === false &&
- metricSettingContext &&
- metricSettingContext.metricSettingState !== MEPState.TRANSACTIONS_ONLY
- ? t('Indexed')
- : null;
- };
- function WidgetCardContextMenu({
- organization,
- selection,
- widget,
- widgetLimitReached,
- hasEditAccess,
- onDelete,
- onDuplicate,
- onEdit,
- showContextMenu,
- isPreview,
- router,
- location,
- index,
- seriesData,
- tableData,
- pageLinks,
- totalIssuesCount,
- seriesResultsType,
- description,
- title,
- }: Props) {
- const indexedEventsWarning = useIndexedEventsWarning();
- const {isMetricsData} = useDashboardsMEPContext();
- if (!showContextMenu) {
- return null;
- }
- const openWidgetViewerPath = (id: string | undefined) => {
- if (!isWidgetViewerPath(location.pathname)) {
- router.push({
- pathname: `${location.pathname}${
- location.pathname.endsWith('/') ? '' : '/'
- }widget/${id}/`,
- query: location.query,
- });
- }
- };
- if (isPreview) {
- return (
- <WidgetViewerContext.Consumer>
- {({setData}) => (
- <ContextWrapper>
- {indexedEventsWarning ? (
- <SampledTag tooltipText={indexedEventsWarning}>{t('Indexed')}</SampledTag>
- ) : null}
- {title && (
- <Tooltip
- title={
- <span>
- <WidgetTooltipTitle>{title}</WidgetTooltipTitle>
- {description && (
- <WidgetTooltipDescription>{description}</WidgetTooltipDescription>
- )}
- </span>
- }
- containerDisplayMode="grid"
- isHoverable
- >
- <WidgetTooltipButton
- aria-label={t('Widget description')}
- borderless
- size="xs"
- icon={<IconInfo />}
- />
- </Tooltip>
- )}
- <StyledDropdownMenuControl
- items={[
- {
- key: 'preview',
- label: t(
- 'This is a preview only. To edit, you must add this dashboard.'
- ),
- disabled: true,
- },
- ]}
- triggerProps={{
- 'aria-label': t('Widget actions'),
- size: 'xs',
- borderless: true,
- showChevron: false,
- icon: <IconEllipsis direction="down" size="sm" />,
- }}
- position="bottom-end"
- />
- <Button
- aria-label={t('Open Widget Viewer')}
- borderless
- size="xs"
- icon={<IconExpand />}
- onClick={() => {
- (seriesData || tableData) &&
- setData({
- seriesData,
- tableData,
- pageLinks,
- totalIssuesCount,
- seriesResultsType,
- });
- openWidgetViewerPath(index);
- }}
- />
- </ContextWrapper>
- )}
- </WidgetViewerContext.Consumer>
- );
- }
- const menuOptions = getMenuOptions(
- organization,
- selection,
- widget,
- Boolean(isMetricsData),
- widgetLimitReached,
- hasEditAccess,
- onDelete,
- onDuplicate,
- onEdit
- );
- if (!menuOptions.length) {
- return null;
- }
- return (
- <WidgetViewerContext.Consumer>
- {({setData}) => (
- <ContextWrapper>
- {indexedEventsWarning ? (
- <SampledTag tooltipText={indexedEventsWarning}>{t('Indexed')}</SampledTag>
- ) : null}
- {title && (
- <Tooltip
- title={
- <span>
- <WidgetTooltipTitle>{title}</WidgetTooltipTitle>
- {description && (
- <WidgetTooltipDescription>{description}</WidgetTooltipDescription>
- )}
- </span>
- }
- containerDisplayMode="grid"
- isHoverable
- >
- <WidgetTooltipButton
- aria-label={t('Widget description')}
- borderless
- size="xs"
- icon={<IconInfo />}
- />
- </Tooltip>
- )}
- <StyledDropdownMenuControl
- items={menuOptions}
- triggerProps={{
- 'aria-label': t('Widget actions'),
- size: 'xs',
- borderless: true,
- showChevron: false,
- icon: <IconEllipsis direction="down" size="sm" />,
- }}
- position="bottom-end"
- />
- <Button
- aria-label={t('Open Widget Viewer')}
- borderless
- size="xs"
- icon={<IconExpand />}
- onClick={() => {
- setData({
- seriesData,
- tableData,
- pageLinks,
- totalIssuesCount,
- seriesResultsType,
- });
- openWidgetViewerPath(widget.id ?? index);
- }}
- />
- </ContextWrapper>
- )}
- </WidgetViewerContext.Consumer>
- );
- }
- export function getMenuOptions(
- organization: Organization,
- selection: PageFilters,
- widget: Widget,
- isMetricsData: boolean,
- widgetLimitReached: boolean,
- hasEditAccess: boolean = true,
- onDelete?: () => void,
- onDuplicate?: () => void,
- onEdit?: () => void
- ) {
- const menuOptions: MenuItemProps[] = [];
- if (
- organization.features.includes('discover-basic') &&
- widget.widgetType &&
- [WidgetType.DISCOVER, WidgetType.ERRORS, WidgetType.TRANSACTIONS].includes(
- widget.widgetType
- )
- ) {
- const optionDisabled =
- (hasDatasetSelector(organization) && widget.widgetType === WidgetType.DISCOVER) ||
- isUsingPerformanceScore(widget);
- // Open Widget in Discover
- if (widget.queries.length) {
- const discoverPath = getWidgetDiscoverUrl(
- widget,
- selection,
- organization,
- 0,
- isMetricsData
- );
- menuOptions.push({
- key: 'open-in-discover',
- label: t('Open in Discover'),
- to: optionDisabled
- ? undefined
- : widget.queries.length === 1
- ? discoverPath
- : undefined,
- tooltip: isUsingPerformanceScore(widget)
- ? performanceScoreTooltip
- : t(
- 'We are splitting datasets to make them easier to digest. Please confirm the dataset for this widget by clicking Edit Widget.'
- ),
- tooltipOptions: {disabled: !optionDisabled},
- disabled: optionDisabled,
- showDetailsInOverlay: true,
- onAction: () => {
- if (widget.queries.length === 1) {
- trackAnalytics('dashboards_views.open_in_discover.opened', {
- organization,
- widget_type: widget.displayType,
- });
- return;
- }
- trackAnalytics('dashboards_views.query_selector.opened', {
- organization,
- widget_type: widget.displayType,
- });
- openDashboardWidgetQuerySelectorModal({organization, widget, isMetricsData});
- },
- });
- }
- }
- if (widget.widgetType === WidgetType.SPANS) {
- menuOptions.push({
- key: 'open-in-explore',
- label: t('Open in Explore'),
- to: getWidgetExploreUrl(widget, selection, organization),
- });
- }
- if (widget.widgetType === WidgetType.ISSUE) {
- const issuesLocation = getWidgetIssueUrl(widget, selection, organization);
- menuOptions.push({
- key: 'open-in-issues',
- label: t('Open in Issues'),
- to: issuesLocation,
- });
- }
- if (widget.widgetType === WidgetType.METRICS) {
- const metricsLocation = getWidgetMetricsUrl(widget, selection, organization);
- menuOptions.push({
- key: 'open-in-metrics',
- label: t('Open in Metrics'),
- to: metricsLocation,
- });
- }
- if (organization.features.includes('dashboards-edit')) {
- menuOptions.push({
- key: 'duplicate-widget',
- label: t('Duplicate Widget'),
- onAction: () => onDuplicate?.(),
- disabled: widgetLimitReached || !hasEditAccess,
- });
- menuOptions.push({
- key: 'edit-widget',
- label: t('Edit Widget'),
- onAction: () => onEdit?.(),
- disabled: !hasEditAccess,
- });
- menuOptions.push({
- key: 'delete-widget',
- label: t('Delete Widget'),
- priority: 'danger',
- onAction: () => {
- openConfirmModal({
- message: t('Are you sure you want to delete this widget?'),
- priority: 'danger',
- onConfirm: () => onDelete?.(),
- });
- },
- disabled: !hasEditAccess,
- });
- }
- return menuOptions;
- }
- export default WidgetCardContextMenu;
- const ContextWrapper = styled('div')`
- display: flex;
- align-items: center;
- height: ${space(3)};
- margin-left: ${space(1)};
- gap: ${space(0.25)};
- `;
- const StyledDropdownMenuControl = styled(DropdownMenu)`
- display: flex;
- & > button {
- z-index: auto;
- }
- `;
- const SampledTag = styled(Tag)`
- margin-right: ${space(0.5)};
- `;
- const WidgetTooltipTitle = styled('div')`
- font-weight: bold;
- font-size: ${p => p.theme.fontSizeMedium};
- text-align: left;
- `;
- const WidgetTooltipDescription = styled('div')`
- margin-top: ${space(0.5)};
- font-size: ${p => p.theme.fontSizeSmall};
- text-align: left;
- `;
- // We're using a button here to preserve tab accessibility
- const WidgetTooltipButton = styled(Button)`
- pointer-events: none;
- `;
|