feat: hide examples via button

89510/native-histogram-banner
Nick Richmond 6 months ago
parent 2ce0e5cac2
commit 2d3075ebc1
  1. 113
      public/app/features/trails/banners/NativeHistogramBanner.tsx

@ -1,18 +1,18 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { useState } from 'react'; import { useState, type Dispatch, type SetStateAction } from 'react';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';
import { useStyles2, useTheme, Alert, Button } from '@grafana/ui'; import { useStyles2, useTheme2, Alert, Button } from '@grafana/ui';
import { Trans } from '@grafana/ui/src/utils/i18n'; import { Trans } from '@grafana/ui/src/utils/i18n';
import { DataTrail } from '../DataTrail'; import { DataTrail } from '../DataTrail';
import { MetricSelectedEvent } from '../shared'; import { MetricSelectedEvent } from '../shared';
type NativeHistogramInfoProps = { interface NativeHistogramInfoProps {
histogramsLoaded: boolean; histogramsLoaded: boolean;
nativeHistograms: string[]; nativeHistograms: string[];
trail: DataTrail; trail: DataTrail;
}; }
export function NativeHistogramBanner(props: NativeHistogramInfoProps) { export function NativeHistogramBanner(props: NativeHistogramInfoProps) {
const { histogramsLoaded, nativeHistograms, trail } = props; const { histogramsLoaded, nativeHistograms, trail } = props;
@ -20,29 +20,13 @@ export function NativeHistogramBanner(props: NativeHistogramInfoProps) {
const [showHistogramExamples, setShowHistogramExamples] = useState(false); const [showHistogramExamples, setShowHistogramExamples] = useState(false);
const styles = useStyles2(getStyles, 0); const styles = useStyles2(getStyles, 0);
const isDark = useTheme().isDark; if (!histogramsLoaded || nativeHistograms.length === 0 || !histogramMessage) {
const images = { return null;
nativeHeatmap: isDark }
? 'public/img/native-histograms/DarkModeHeatmapNativeHistogram.png'
: 'public/img/native-histograms/LightModeHeatmapNativeHistogram.png',
classicHeatmap: isDark
? 'public/img/native-histograms/DarkModeHeatmapClassicHistogram.png'
: 'public/img/native-histograms/LightModeHeatmapClassicHistogram.png',
nativeHistogram: isDark
? 'public/img/native-histograms/DarkModeHistogramNativehistogram.png'
: 'public/img/native-histograms/LightModeHistogramClassicHistogram.png',
classicHistogram: isDark
? 'public/img/native-histograms/DarkModeHistogramClassicHistogram.png'
: 'public/img/native-histograms/LightModeHistogramClassicHistogram.png',
};
const selectNativeHistogram = (metric: string) => {
trail.publishEvent(new MetricSelectedEvent(metric), true);
};
return ( return (
<> <>
{histogramsLoaded && (nativeHistograms ?? []).length > 0 && histogramMessage && ( {
<Alert <Alert
title={'Native Histogram Support'} title={'Native Histogram Support'}
severity={'info'} severity={'info'}
@ -70,22 +54,75 @@ export function NativeHistogramBanner(props: NativeHistogramInfoProps) {
</div> </div>
</div> </div>
</div> </div>
{!showHistogramExamples && ( <NativeHistogramExamplesButton
showHistogramExamples={showHistogramExamples}
setShowHistogramExamples={setShowHistogramExamples}
/>
{showHistogramExamples && (
<NativeHistogramExamples
trail={trail}
nativeHistograms={nativeHistograms}
setHistogramMessage={setHistogramMessage}
/>
)}
</Alert>
}
</>
);
}
interface NativeHistogramExamplesButtonProps {
showHistogramExamples: boolean;
setShowHistogramExamples: Dispatch<SetStateAction<boolean>>;
}
const NativeHistogramExamplesButton = ({
showHistogramExamples,
setShowHistogramExamples,
}: NativeHistogramExamplesButtonProps) => {
const styles = useStyles2(getStyles, 0);
return (
<div> <div>
<Button <Button
className={styles.seeExamplesButton} className={styles.seeExamplesButton}
type="button" type="button"
fill="text" fill="text"
variant="primary" variant="primary"
onClick={() => { onClick={() => setShowHistogramExamples(!showHistogramExamples)}
setShowHistogramExamples(true);
}}
> >
{`> See examples`} {showHistogramExamples ? `Hide examples` : `> See examples`}
</Button> </Button>
</div> </div>
)} );
{showHistogramExamples && ( };
type NativeHistogramExamplesProps = Pick<NativeHistogramInfoProps, 'trail' | 'nativeHistograms'> & {
setHistogramMessage: Dispatch<SetStateAction<boolean>>;
};
const NativeHistogramExamples = ({ trail, nativeHistograms, setHistogramMessage }: NativeHistogramExamplesProps) => {
const styles = useStyles2(getStyles, 0);
const isDark = useTheme2().isDark;
const selectNativeHistogram = (metric: string) => {
trail.publishEvent(new MetricSelectedEvent(metric), true);
};
const images = {
nativeHeatmap: isDark
? 'public/img/native-histograms/DarkModeHeatmapNativeHistogram.png'
: 'public/img/native-histograms/LightModeHeatmapNativeHistogram.png',
classicHeatmap: isDark
? 'public/img/native-histograms/DarkModeHeatmapClassicHistogram.png'
: 'public/img/native-histograms/LightModeHeatmapClassicHistogram.png',
nativeHistogram: isDark
? 'public/img/native-histograms/DarkModeHistogramNativehistogram.png'
: 'public/img/native-histograms/LightModeHistogramClassicHistogram.png',
classicHistogram: isDark
? 'public/img/native-histograms/DarkModeHistogramClassicHistogram.png'
: 'public/img/native-histograms/LightModeHistogramClassicHistogram.png',
};
return (
<> <>
<div className={`${styles.histogramRow} ${styles.seeExamplesRow}`}> <div className={`${styles.histogramRow} ${styles.seeExamplesRow}`}>
<div className={styles.histogramImageCol}> <div className={styles.histogramImageCol}>
@ -143,11 +180,7 @@ export function NativeHistogramBanner(props: NativeHistogramInfoProps) {
</Trans> </Trans>
</div> </div>
<div> <div>
<img <img width="100%" src={images.classicHistogram} alt="Classic Histogram displayed as histogram" />
width="100%"
src={images.classicHistogram}
alt="Classic Histogram displayed as histogram"
/>
</div> </div>
</div> </div>
</div> </div>
@ -180,14 +213,10 @@ export function NativeHistogramBanner(props: NativeHistogramInfoProps) {
})} })}
</div> </div>
</> </>
)}
</Alert>
)}
</>
); );
} };
function getStyles(theme: GrafanaTheme2, chromeHeaderHeight: number) { function getStyles(theme: GrafanaTheme2, _chromeHeaderHeight: number) {
return { return {
histogramRow: css({ histogramRow: css({
display: 'flex', display: 'flex',

Loading…
Cancel
Save