import React, { PureComponent } from 'react'; import { VizOrientation } from '@grafana/ui'; import { VizRepeater } from '@grafana/ui'; export interface Props { width: number; height: number; orientation: VizOrientation; source: any; // If this changes, the values will be processed processFlag?: boolean; // change to force processing getProcessedValues: () => T[]; renderValue: (value: T, width: number, height: number) => JSX.Element; } interface State { values: T[]; } /** * This is essentially a cache of processed values. This checks for changes * to the source and then saves the processed values in the State */ export class ProcessedValuesRepeater extends PureComponent, State> { constructor(props: Props) { super(props); this.state = { values: props.getProcessedValues(), }; } componentDidUpdate(prevProps: Props) { const { processFlag, source } = this.props; if (processFlag !== prevProps.processFlag || source !== prevProps.source) { this.setState({ values: this.props.getProcessedValues() }); } } render() { const { orientation, height, width, renderValue } = this.props; const { values } = this.state; return ( {({ vizHeight, vizWidth, value }) => renderValue(value, vizWidth, vizHeight)} ); } }