AdHocVariableEditor.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { DataSourceRef, SelectableValue } from '@grafana/data';
  4. import { Alert, InlineFieldRow, VerticalGroup } from '@grafana/ui';
  5. import { StoreState } from 'app/types';
  6. import { VariableSectionHeader } from '../editor/VariableSectionHeader';
  7. import { VariableSelectField } from '../editor/VariableSelectField';
  8. import { initialVariableEditorState } from '../editor/reducer';
  9. import { getAdhocVariableEditorState } from '../editor/selectors';
  10. import { VariableEditorProps } from '../editor/types';
  11. import { getVariablesState } from '../state/selectors';
  12. import { AdHocVariableModel } from '../types';
  13. import { toKeyedVariableIdentifier } from '../utils';
  14. import { changeVariableDatasource, initAdHocVariableEditor } from './actions';
  15. const mapStateToProps = (state: StoreState, ownProps: OwnProps) => {
  16. const { rootStateKey } = ownProps.variable;
  17. if (!rootStateKey) {
  18. console.error('AdHocVariableEditor: variable has no rootStateKey');
  19. return {
  20. extended: getAdhocVariableEditorState(initialVariableEditorState),
  21. };
  22. }
  23. const { editor } = getVariablesState(rootStateKey, state);
  24. return {
  25. extended: getAdhocVariableEditorState(editor),
  26. };
  27. };
  28. const mapDispatchToProps = {
  29. initAdHocVariableEditor,
  30. changeVariableDatasource,
  31. };
  32. const connector = connect(mapStateToProps, mapDispatchToProps);
  33. export interface OwnProps extends VariableEditorProps<AdHocVariableModel> {}
  34. type Props = OwnProps & ConnectedProps<typeof connector>;
  35. export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
  36. componentDidMount() {
  37. const { rootStateKey } = this.props.variable;
  38. if (!rootStateKey) {
  39. console.error('AdHocVariableEditor: variable has no rootStateKey');
  40. return;
  41. }
  42. this.props.initAdHocVariableEditor(rootStateKey);
  43. }
  44. onDatasourceChanged = (option: SelectableValue<DataSourceRef>) => {
  45. this.props.changeVariableDatasource(toKeyedVariableIdentifier(this.props.variable), option.value);
  46. };
  47. render() {
  48. const { variable, extended } = this.props;
  49. const dataSources = extended?.dataSources ?? [];
  50. const infoText = extended?.infoText ?? null;
  51. const options = dataSources.map((ds) => ({ label: ds.text, value: ds.value }));
  52. const value = options.find((o) => o.value?.uid === variable.datasource?.uid) ?? options[0];
  53. return (
  54. <VerticalGroup spacing="xs">
  55. <VariableSectionHeader name="Options" />
  56. <VerticalGroup spacing="sm">
  57. <InlineFieldRow>
  58. <VariableSelectField
  59. name="Data source"
  60. value={value}
  61. options={options}
  62. onChange={this.onDatasourceChanged}
  63. labelWidth={10}
  64. />
  65. </InlineFieldRow>
  66. {infoText ? <Alert title={infoText} severity="info" /> : null}
  67. </VerticalGroup>
  68. </VerticalGroup>
  69. );
  70. }
  71. }
  72. export const AdHocVariableEditor = connector(AdHocVariableEditorUnConnected);