2021-06-11 21:03:58 +08:00
|
|
|
import { GrafanaTheme2 } from '@grafana/data';
|
|
|
|
|
import { IconButton, InlineLabel, Tooltip, useStyles2 } from '@grafana/ui';
|
2021-04-01 14:15:23 +02:00
|
|
|
import { css, cx } from '@emotion/css';
|
2021-03-16 13:58:51 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
import { PrometheusDatasource } from '../datasource';
|
2021-01-15 16:20:20 +01:00
|
|
|
|
|
|
|
|
interface Props {
|
2021-04-06 18:35:00 +02:00
|
|
|
isEnabled: boolean;
|
|
|
|
|
onChange: (isEnabled: boolean) => void;
|
2021-03-16 13:58:51 +01:00
|
|
|
datasource: PrometheusDatasource;
|
2021-01-15 16:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-06 18:35:00 +02:00
|
|
|
export function PromExemplarField({ datasource, onChange, isEnabled }: Props) {
|
|
|
|
|
const [error, setError] = useState<string>();
|
2021-06-11 21:03:58 +08:00
|
|
|
const styles = useStyles2(getStyles);
|
2021-03-16 13:58:51 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-06 18:35:00 +02:00
|
|
|
const subscription = datasource.exemplarErrors.subscribe((err) => {
|
2021-03-16 13:58:51 +01:00
|
|
|
setError(err);
|
|
|
|
|
});
|
|
|
|
|
return () => {
|
|
|
|
|
subscription.unsubscribe();
|
|
|
|
|
};
|
2021-04-06 18:35:00 +02:00
|
|
|
}, [datasource]);
|
2021-03-16 13:58:51 +01:00
|
|
|
|
|
|
|
|
const iconButtonStyles = cx(
|
|
|
|
|
{
|
2021-04-06 18:35:00 +02:00
|
|
|
[styles.activeIcon]: isEnabled,
|
2021-03-16 13:58:51 +01:00
|
|
|
},
|
|
|
|
|
styles.eyeIcon
|
|
|
|
|
);
|
|
|
|
|
|
2021-01-15 16:20:20 +01:00
|
|
|
return (
|
2021-03-16 13:58:51 +01:00
|
|
|
<InlineLabel width="auto">
|
2021-04-06 18:35:00 +02:00
|
|
|
<Tooltip content={error ?? ''}>
|
2021-03-16 13:58:51 +01:00
|
|
|
<div className={styles.iconWrapper}>
|
|
|
|
|
Exemplars
|
|
|
|
|
<IconButton
|
|
|
|
|
name="eye"
|
2021-04-06 18:35:00 +02:00
|
|
|
tooltip={isEnabled ? 'Disable query with exemplars' : 'Enable query with exemplars'}
|
2021-03-16 13:58:51 +01:00
|
|
|
disabled={!!error}
|
|
|
|
|
className={iconButtonStyles}
|
|
|
|
|
onClick={() => {
|
2021-04-06 18:35:00 +02:00
|
|
|
onChange(!isEnabled);
|
2021-03-16 13:58:51 +01:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</InlineLabel>
|
2021-01-15 16:20:20 +01:00
|
|
|
);
|
|
|
|
|
}
|
2021-03-16 13:58:51 +01:00
|
|
|
|
2021-06-11 21:03:58 +08:00
|
|
|
function getStyles(theme: GrafanaTheme2) {
|
2021-03-16 13:58:51 +01:00
|
|
|
return {
|
|
|
|
|
eyeIcon: css`
|
2021-06-11 21:03:58 +08:00
|
|
|
margin-left: ${theme.spacing(2)};
|
2021-03-16 13:58:51 +01:00
|
|
|
`,
|
|
|
|
|
activeIcon: css`
|
2021-06-11 21:03:58 +08:00
|
|
|
color: ${theme.colors.primary.main};
|
2021-03-16 13:58:51 +01:00
|
|
|
`,
|
|
|
|
|
iconWrapper: css`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
`,
|
|
|
|
|
};
|
|
|
|
|
}
|