import React, { FC, useEffect, useState } from 'react'; import { connect } from 'react-redux'; import { featureEnabled, reportExperimentView } from '@grafana/runtime'; import { Drawer, ToolbarButton, useTheme2 } from '@grafana/ui'; import { UpgradeBox, UpgradeContentVertical } from 'app/core/components/Upgrade/UpgradeBox'; import { highlightTrial } from 'app/features/admin/utils'; import { addCustomRightAction } from 'app/features/dashboard/components/DashNav/DashNav'; import { DashboardModel } from 'app/features/dashboard/state'; import { EnterpriseStoreState } from '../../types'; import { buildExperimentID, ExperimentGroup } from '../../utils/featureHighlights'; import { setDrawerOpen } from '../state/reducers'; import AnalyticsDrawer from './AnalyticsDrawer'; type AnalyticsToolbarButtonProps = { onClick(): void; isHighlighted?: boolean; }; const AnalyticsToolbarButton = ({ onClick, isHighlighted }: AnalyticsToolbarButtonProps) => { return ( ); }; type AnalyticsContentProps = { dashboard?: DashboardModel; isDrawerOpen: boolean; setDrawerOpen: typeof setDrawerOpen; }; const AnalyticsContent: FC = ({ dashboard, isDrawerOpen, setDrawerOpen }) => { const showContent = dashboard?.id && dashboard.meta.url; const showHighlight = highlightTrial(); useEffect(() => { if (showContent && showHighlight) { reportExperimentView(buildExperimentID('dashboard-insights-dot'), ExperimentGroup.Test, 'trial'); } }, [showContent, showHighlight]); return ( showContent && ( <> { setDrawerOpen(true); }} isHighlighted={showHighlight} /> {isDrawerOpen && } ) ); }; function mapStateToProps(state: EnterpriseStoreState) { return { isDrawerOpen: state.metaAnalytics.isDrawerOpen, }; } const mapDispatchToProps = { setDrawerOpen, }; type AnalyticsContentUpgradeProps = { dashboard?: DashboardModel; }; const AnalyticsContentUpgrade = ({ dashboard }: AnalyticsContentUpgradeProps) => { const showContent = dashboard?.id && dashboard.meta.url; const [isDrawerOpen, setIsDrawerOpen] = useState(false); useEffect(() => { if (showContent) { reportExperimentView(buildExperimentID('dashboard-insights-dot'), ExperimentGroup.Test, ''); } }, [showContent]); return ( showContent && ( <> { setIsDrawerOpen(true); }} /> {isDrawerOpen && setIsDrawerOpen(false)} />} ) ); }; export const initAnalyticsDrawer = () => { addCustomRightAction({ show: () => true, component: featureEnabled('analytics') ? connect(mapStateToProps, mapDispatchToProps)(AnalyticsContent) : AnalyticsContentUpgrade, index: -1, }); }; interface AnalyticsUpgradeDrawerProps { onClose: () => void; } export const AnalyticsUpgradeDrawer = ({ onClose, dashboard, }: AnalyticsUpgradeDrawerProps & AnalyticsContentUpgradeProps) => { const theme = useTheme2(); return ( ); };