QueryRows.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { createSelector } from '@reduxjs/toolkit';
  2. import React, { useCallback, useMemo } from 'react';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { CoreApp, DataQuery } from '@grafana/data';
  5. import { getNextRefIdChar } from 'app/core/utils/query';
  6. import { ExploreId } from 'app/types/explore';
  7. import { getDatasourceSrv } from '../plugins/datasource_srv';
  8. import { QueryEditorRows } from '../query/components/QueryEditorRows';
  9. import { runQueries, changeQueriesAction } from './state/query';
  10. import { getExploreItemSelector } from './state/selectors';
  11. interface Props {
  12. exploreId: ExploreId;
  13. }
  14. const makeSelectors = (exploreId: ExploreId) => {
  15. const exploreItemSelector = getExploreItemSelector(exploreId);
  16. return {
  17. getQueries: createSelector(exploreItemSelector, (s) => s!.queries),
  18. getQueryResponse: createSelector(exploreItemSelector, (s) => s!.queryResponse),
  19. getHistory: createSelector(exploreItemSelector, (s) => s!.history),
  20. getEventBridge: createSelector(exploreItemSelector, (s) => s!.eventBridge),
  21. getDatasourceInstanceSettings: createSelector(
  22. exploreItemSelector,
  23. (s) => getDatasourceSrv().getInstanceSettings(s!.datasourceInstance?.uid)!
  24. ),
  25. };
  26. };
  27. export const QueryRows = ({ exploreId }: Props) => {
  28. const dispatch = useDispatch();
  29. const { getQueries, getDatasourceInstanceSettings, getQueryResponse, getHistory, getEventBridge } = useMemo(
  30. () => makeSelectors(exploreId),
  31. [exploreId]
  32. );
  33. const queries = useSelector(getQueries)!;
  34. const dsSettings = useSelector(getDatasourceInstanceSettings)!;
  35. const queryResponse = useSelector(getQueryResponse)!;
  36. const history = useSelector(getHistory);
  37. const eventBridge = useSelector(getEventBridge);
  38. const onRunQueries = useCallback(() => {
  39. dispatch(runQueries(exploreId));
  40. }, [dispatch, exploreId]);
  41. const onChange = useCallback(
  42. (newQueries: DataQuery[]) => {
  43. dispatch(changeQueriesAction({ queries: newQueries, exploreId }));
  44. // if we are removing a query we want to run the remaining ones
  45. if (newQueries.length < queries.length) {
  46. onRunQueries();
  47. }
  48. },
  49. [dispatch, exploreId, onRunQueries, queries]
  50. );
  51. const onAddQuery = useCallback(
  52. (query: DataQuery) => {
  53. onChange([...queries, { ...query, refId: getNextRefIdChar(queries) }]);
  54. },
  55. [onChange, queries]
  56. );
  57. return (
  58. <QueryEditorRows
  59. dsSettings={dsSettings}
  60. queries={queries}
  61. onQueriesChange={onChange}
  62. onAddQuery={onAddQuery}
  63. onRunQueries={onRunQueries}
  64. data={queryResponse}
  65. app={CoreApp.Explore}
  66. history={history}
  67. eventBus={eventBridge}
  68. />
  69. );
  70. };