2021-01-19 07:04:54 -06:00
|
|
|
import React, { FC, useMemo, useState } from 'react';
|
2021-04-01 07:15:23 -05:00
|
|
|
import { css } from '@emotion/css';
|
2021-03-15 04:17:21 -05:00
|
|
|
import { getFrameDisplayName, GrafanaTheme, PanelData, SelectableValue } from '@grafana/data';
|
|
|
|
import { Button, Select, stylesFactory, Table, useTheme } from '@grafana/ui';
|
|
|
|
import { EmptyState } from './EmptyState';
|
2021-01-19 07:04:54 -06:00
|
|
|
|
|
|
|
interface Props {
|
2021-03-15 04:17:21 -05:00
|
|
|
data: PanelData;
|
2021-01-19 07:04:54 -06:00
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
}
|
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
export const PreviewQueryTab: FC<Props> = ({ data, height, width }) => {
|
2021-01-19 07:04:54 -06:00
|
|
|
const [currentSeries, setSeries] = useState<number>(0);
|
|
|
|
const theme = useTheme();
|
|
|
|
const styles = getStyles(theme, height);
|
2021-02-15 06:56:59 -06:00
|
|
|
const series = useMemo<Array<SelectableValue<number>>>(() => {
|
|
|
|
if (data?.series) {
|
|
|
|
return data.series.map((frame, index) => ({ value: index, label: getFrameDisplayName(frame) }));
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}, [data]);
|
2021-01-19 07:04:54 -06:00
|
|
|
|
|
|
|
// Select padding
|
|
|
|
const padding = 16;
|
|
|
|
|
2021-03-15 04:17:21 -05:00
|
|
|
if (!data) {
|
|
|
|
return (
|
|
|
|
<EmptyState title="Run queries to view data.">
|
2021-04-21 06:57:17 -05:00
|
|
|
<Button>Run queries</Button>
|
2021-03-15 04:17:21 -05:00
|
|
|
</EmptyState>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.series) {
|
|
|
|
return null;
|
2021-02-15 06:56:59 -06:00
|
|
|
}
|
|
|
|
|
2021-01-19 07:04:54 -06:00
|
|
|
if (data.series.length > 1) {
|
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<div style={{ height: height - theme.spacing.formInputHeight - 16 }}>
|
|
|
|
<Table
|
|
|
|
data={data.series[currentSeries]}
|
|
|
|
height={height - theme.spacing.formInputHeight - padding}
|
|
|
|
width={width}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.selectWrapper}>
|
2021-01-20 00:59:48 -06:00
|
|
|
<Select
|
|
|
|
onChange={(selectedValue) => setSeries(selectedValue.value!)}
|
|
|
|
options={series}
|
|
|
|
value={currentSeries}
|
|
|
|
/>
|
2021-01-19 07:04:54 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-03-15 04:17:21 -05:00
|
|
|
|
2021-01-19 07:04:54 -06:00
|
|
|
return <Table data={data.series[0]} height={height} width={width} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme, height: number) => {
|
|
|
|
return {
|
|
|
|
wrapper: css`
|
2021-03-15 04:17:21 -05:00
|
|
|
label: preview-wrapper;
|
2021-01-19 07:04:54 -06:00
|
|
|
height: ${height}px;
|
|
|
|
`,
|
|
|
|
selectWrapper: css`
|
|
|
|
padding: ${theme.spacing.md};
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
});
|