grafana/public/app/features/explore/TableContainer.tsx
Andre Pereira 183b279274
Explore: Sub-tables support for Table component (#58682)
* First commit with working version of sub-tables using subData array

* Update TableContainer and query result to support a dataframe array for the table result

* Fix border issue by moving the subtable to above the cells in the DOM

* Allow header to be configurable using custom options.

* Update TablePanel to support sub-tables

* Fix main row links

* Added tests

* Fix TablePanel correctly splitting frames and sub-frames by using refId
2022-11-23 17:49:32 +00:00

113 lines
3.6 KiB
TypeScript

import React, { PureComponent } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import { ValueLinkConfig, applyFieldOverrides, TimeZone, SplitOpen, DataFrame } from '@grafana/data';
import { Collapse, Table } from '@grafana/ui';
import { FilterItem } from '@grafana/ui/src/components/Table/types';
import { config } from 'app/core/config';
import { PANEL_BORDER } from 'app/core/constants';
import { StoreState } from 'app/types';
import { ExploreId, ExploreItemState } from 'app/types/explore';
import { MetaInfoText } from './MetaInfoText';
import { getFieldLinksForExplore } from './utils/links';
interface TableContainerProps {
ariaLabel?: string;
exploreId: ExploreId;
width: number;
timeZone: TimeZone;
onCellFilterAdded?: (filter: FilterItem) => void;
splitOpenFn: SplitOpen;
}
function mapStateToProps(state: StoreState, { exploreId }: TableContainerProps) {
const explore = state.explore;
// @ts-ignore
const item: ExploreItemState = explore[exploreId];
const { loading: loadingInState, tableResult, range } = item;
const loading = tableResult && tableResult.length > 0 ? false : loadingInState;
return { loading, tableResult, range };
}
const connector = connect(mapStateToProps, {});
type Props = TableContainerProps & ConnectedProps<typeof connector>;
export class TableContainer extends PureComponent<Props> {
getMainFrame(frames: DataFrame[] | null) {
return frames?.find((df) => df.meta?.custom?.parentRowIndex === undefined) || frames?.[0];
}
getTableHeight() {
const { tableResult } = this.props;
const mainFrame = this.getMainFrame(tableResult);
if (!mainFrame || mainFrame.length === 0) {
return 200;
}
// tries to estimate table height
return Math.max(Math.min(600, mainFrame.length * 35) + 35);
}
render() {
const { loading, onCellFilterAdded, tableResult, width, splitOpenFn, range, ariaLabel, timeZone } = this.props;
const height = this.getTableHeight();
const tableWidth = width - config.theme.panelPadding * 2 - PANEL_BORDER;
let dataFrames = tableResult;
if (dataFrames?.length) {
dataFrames = applyFieldOverrides({
data: dataFrames,
timeZone,
theme: config.theme2,
replaceVariables: (v: string) => v,
fieldConfig: {
defaults: {},
overrides: [],
},
});
// Bit of code smell here. We need to add links here to the frame modifying the frame on every render.
// Should work fine in essence but still not the ideal way to pass props. In logs container we do this
// differently and sidestep this getLinks API on a dataframe
for (const frame of dataFrames) {
for (const field of frame.fields) {
field.getLinks = (config: ValueLinkConfig) => {
return getFieldLinksForExplore({
field,
rowIndex: config.valueRowIndex!,
splitOpenFn,
range,
dataFrame: frame!,
});
};
}
}
}
const mainFrame = this.getMainFrame(dataFrames);
const subFrames = dataFrames?.filter((df) => df.meta?.custom?.parentRowIndex !== undefined);
return (
<Collapse label="Table" loading={loading} isOpen>
{mainFrame?.length ? (
<Table
ariaLabel={ariaLabel}
data={mainFrame}
subData={subFrames}
width={tableWidth}
height={height}
onCellFilterAdded={onCellFilterAdded}
/>
) : (
<MetaInfoText metaItems={[{ value: '0 series returned' }]} />
)}
</Collapse>
);
}
}
export default connector(TableContainer);