import {Fragment, memo, useCallback} from 'react';
import {useTheme} from '@emotion/react';
import styled from '@emotion/styled';
import * as Sentry from '@sentry/react';
import emptyStateImg from 'sentry-images/spot/custom-metrics-empty-state.svg';
import Alert from 'sentry/components/alert';
import GuideAnchor from 'sentry/components/assistant/guideAnchor';
import FeatureBadge from 'sentry/components/badge/featureBadge';
import Banner from 'sentry/components/banner';
import {Button} from 'sentry/components/button';
import ButtonBar from 'sentry/components/buttonBar';
import FloatingFeedbackWidget from 'sentry/components/feedback/widget/floatingFeedbackWidget';
import * as Layout from 'sentry/components/layouts/thirds';
import LoadingIndicator from 'sentry/components/loadingIndicator';
import OnboardingPanel from 'sentry/components/onboardingPanel';
import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
import {t} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import {trackAnalytics} from 'sentry/utils/analytics';
import {METRICS_DOCS_URL} from 'sentry/utils/metrics/constants';
import {hasCustomMetrics} from 'sentry/utils/metrics/features';
import useDismissAlert from 'sentry/utils/useDismissAlert';
import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
import useMedia from 'sentry/utils/useMedia';
import useOrganization from 'sentry/utils/useOrganization';
import usePageFilters from 'sentry/utils/usePageFilters';
import BackgroundSpace from 'sentry/views/discover/backgroundSpace';
import {useMetricsContext} from 'sentry/views/metrics/context';
import {useMetricsOnboardingSidebar} from 'sentry/views/metrics/ddmOnboarding/useMetricsOnboardingSidebar';
import {IntervalSelect} from 'sentry/views/metrics/intervalSelect';
import {MetricsApiChangeAlert} from 'sentry/views/metrics/metricsApiChangeAlert';
import {PageHeaderActions} from 'sentry/views/metrics/pageHeaderActions';
import {Queries} from 'sentry/views/metrics/queries';
import {MetricScratchpad} from 'sentry/views/metrics/scratchpad';
import {WidgetDetails} from 'sentry/views/metrics/widgetDetails';
export const MetricsLayout = memo(() => {
const organization = useOrganization();
const pageFilters = usePageFilters();
const selectedProjects = pageFilters.selection.projects.join();
const {
hasCustomMetrics: hasSentCustomMetrics,
hasPerformanceMetrics,
isHasMetricsLoading,
} = useMetricsContext();
const {activateSidebar} = useMetricsOnboardingSidebar();
const {dismiss: emptyStateDismiss, isDismissed: isEmptyStateDismissed} =
useDismissAlert({
key: `${organization.id}:${selectedProjects}:metrics-empty-state-dismissed`,
});
const theme = useTheme();
const isSmallBanner = useMedia(`(max-width: ${theme.breakpoints.medium})`);
const [isBannerDismissed] = useLocalStorageState('metrics-banner-dismissed', false);
const showOnboardingPanel = !isEmptyStateDismissed && !hasSentCustomMetrics;
const addCustomMetric = useCallback(
(referrer: 'header' | 'onboarding_panel' | 'banner') => {
Sentry.metrics.increment('ddm.add_custom_metric', 1, {
tags: {
referrer,
},
});
trackAnalytics('ddm.open-onboarding', {
organization,
source: referrer,
});
activateSidebar();
},
[activateSidebar, organization]
);
const viewPerformanceMetrics = useCallback(() => {
Sentry.metrics.increment('ddm.view_performance_metrics', 1);
trackAnalytics('ddm.view_performance_metrics', {
organization,
});
emptyStateDismiss();
}, [emptyStateDismiss, organization]);
if (!hasCustomMetrics(organization)) {
return (
{t("You don't have access to this feature")}
);
}
return (
{t('Metrics')}
{!showOnboardingPanel ? (
addCustomMetric('header')}
/>
) : null}
{isEmptyStateDismissed && !hasSentCustomMetrics && (
}
dismissKey="metrics"
>
)}
{isHasMetricsLoading ? (
) : !showOnboardingPanel ? (
) : (
}>
{t('Track and solve what matters')}
{t(
'Create custom metrics to track and visualize the data points you care about over time, like processing time, checkout conversion rate, or user signups. See correlated trace exemplars and metrics if used together with Performance Monitoring.'
)}