AnalyticsDashNav.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { FC, useEffect, useState } from 'react';
  2. import { connect } from 'react-redux';
  3. import { featureEnabled, reportExperimentView } from '@grafana/runtime';
  4. import { Drawer, ToolbarButton, useTheme2 } from '@grafana/ui';
  5. import { UpgradeBox, UpgradeContentVertical } from 'app/core/components/Upgrade/UpgradeBox';
  6. import { highlightTrial } from 'app/features/admin/utils';
  7. import { addCustomRightAction } from 'app/features/dashboard/components/DashNav/DashNav';
  8. import { DashboardModel } from 'app/features/dashboard/state';
  9. import { EnterpriseStoreState } from '../../types';
  10. import { buildExperimentID, ExperimentGroup } from '../../utils/featureHighlights';
  11. import { setDrawerOpen } from '../state/reducers';
  12. import AnalyticsDrawer from './AnalyticsDrawer';
  13. type AnalyticsToolbarButtonProps = {
  14. onClick(): void;
  15. isHighlighted?: boolean;
  16. };
  17. const AnalyticsToolbarButton = ({ onClick, isHighlighted }: AnalyticsToolbarButtonProps) => {
  18. return (
  19. <ToolbarButton icon="info-circle" tooltip="Dashboard insights" onClick={onClick} isHighlighted={isHighlighted} />
  20. );
  21. };
  22. type AnalyticsContentProps = {
  23. dashboard?: DashboardModel;
  24. isDrawerOpen: boolean;
  25. setDrawerOpen: typeof setDrawerOpen;
  26. };
  27. const AnalyticsContent: FC<AnalyticsContentProps> = ({ dashboard, isDrawerOpen, setDrawerOpen }) => {
  28. const showContent = dashboard?.id && dashboard.meta.url;
  29. const showHighlight = highlightTrial();
  30. useEffect(() => {
  31. if (showContent && showHighlight) {
  32. reportExperimentView(buildExperimentID('dashboard-insights-dot'), ExperimentGroup.Test, 'trial');
  33. }
  34. }, [showContent, showHighlight]);
  35. return (
  36. showContent && (
  37. <>
  38. <AnalyticsToolbarButton
  39. onClick={() => {
  40. setDrawerOpen(true);
  41. }}
  42. isHighlighted={showHighlight}
  43. />
  44. {isDrawerOpen && <AnalyticsDrawer dashboard={dashboard} />}
  45. </>
  46. )
  47. );
  48. };
  49. function mapStateToProps(state: EnterpriseStoreState) {
  50. return {
  51. isDrawerOpen: state.metaAnalytics.isDrawerOpen,
  52. };
  53. }
  54. const mapDispatchToProps = {
  55. setDrawerOpen,
  56. };
  57. type AnalyticsContentUpgradeProps = {
  58. dashboard?: DashboardModel;
  59. };
  60. const AnalyticsContentUpgrade = ({ dashboard }: AnalyticsContentUpgradeProps) => {
  61. const showContent = dashboard?.id && dashboard.meta.url;
  62. const [isDrawerOpen, setIsDrawerOpen] = useState(false);
  63. useEffect(() => {
  64. if (showContent) {
  65. reportExperimentView(buildExperimentID('dashboard-insights-dot'), ExperimentGroup.Test, '');
  66. }
  67. }, [showContent]);
  68. return (
  69. showContent && (
  70. <>
  71. <AnalyticsToolbarButton
  72. isHighlighted
  73. onClick={() => {
  74. setIsDrawerOpen(true);
  75. }}
  76. />
  77. {isDrawerOpen && <AnalyticsUpgradeDrawer dashboard={dashboard} onClose={() => setIsDrawerOpen(false)} />}
  78. </>
  79. )
  80. );
  81. };
  82. export const initAnalyticsDrawer = () => {
  83. addCustomRightAction({
  84. show: () => true,
  85. component: featureEnabled('analytics')
  86. ? connect(mapStateToProps, mapDispatchToProps)(AnalyticsContent)
  87. : AnalyticsContentUpgrade,
  88. index: -1,
  89. });
  90. };
  91. interface AnalyticsUpgradeDrawerProps {
  92. onClose: () => void;
  93. }
  94. export const AnalyticsUpgradeDrawer = ({
  95. onClose,
  96. dashboard,
  97. }: AnalyticsUpgradeDrawerProps & AnalyticsContentUpgradeProps) => {
  98. const theme = useTheme2();
  99. return (
  100. <Drawer onClose={onClose} title={`${dashboard?.title} - analytics`} width={'50%'}>
  101. <UpgradeBox featureName={'dashboard usage insights'} featureId={'dashboard-insights'} />
  102. <UpgradeContentVertical
  103. featureName={'dashboard usage insights'}
  104. image={`usage-insights-${theme.isLight ? 'light' : 'dark'}.png`}
  105. featureUrl={'https://grafana.com/docs/grafana/latest/enterprise/usage-insights/dashboard-datasource-insights'}
  106. description={
  107. 'Usage Insights provide detailed information about dashboard usage, like the number of views, queries, and errors users have experienced. You can use this to improve users’ experience and troubleshoot issues.'
  108. }
  109. />
  110. </Drawer>
  111. );
  112. };