state.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { useCallback, useState } from 'react';
  2. import { CoreApp } from '@grafana/data';
  3. import store from 'app/core/store';
  4. import { LegendFormatMode, PromQuery } from '../types';
  5. import { QueryEditorMode } from './shared/types';
  6. const queryEditorModeDefaultLocalStorageKey = 'PrometheusQueryEditorModeDefault';
  7. export function changeEditorMode(query: PromQuery, editorMode: QueryEditorMode, onChange: (query: PromQuery) => void) {
  8. // If empty query store new mode as default
  9. if (query.expr === '') {
  10. store.set(queryEditorModeDefaultLocalStorageKey, editorMode);
  11. }
  12. onChange({ ...query, editorMode });
  13. }
  14. function getDefaultEditorMode(expr: string) {
  15. // If we already have an expression default to code view
  16. if (expr != null && expr !== '') {
  17. return QueryEditorMode.Code;
  18. }
  19. const value = store.get(queryEditorModeDefaultLocalStorageKey) as QueryEditorMode;
  20. switch (value) {
  21. case QueryEditorMode.Builder:
  22. case QueryEditorMode.Code:
  23. case QueryEditorMode.Explain:
  24. return value;
  25. default:
  26. return QueryEditorMode.Builder;
  27. }
  28. }
  29. /**
  30. * Returns query with defaults, and boolean true/false depending on change was required
  31. */
  32. export function getQueryWithDefaults(query: PromQuery, app: CoreApp | undefined): PromQuery {
  33. let result = query;
  34. if (!query.editorMode) {
  35. result = { ...query, editorMode: getDefaultEditorMode(query.expr) };
  36. }
  37. if (query.expr == null) {
  38. result = { ...result, expr: '', legendFormat: LegendFormatMode.Auto };
  39. }
  40. if (query.range == null && query.instant == null) {
  41. // Default to range query
  42. result = { ...result, range: true };
  43. // In explore we default to both instant & range
  44. if (app === CoreApp.Explore) {
  45. result.instant = true;
  46. }
  47. }
  48. return result;
  49. }
  50. const queryEditorRawQueryLocalStorageKey = 'PrometheusQueryEditorRawQueryDefault';
  51. function getRawQueryVisibility(): boolean {
  52. const val = store.get(queryEditorRawQueryLocalStorageKey);
  53. return val === undefined ? true : Boolean(parseInt(val, 10));
  54. }
  55. function setRawQueryVisibility(value: boolean) {
  56. store.set(queryEditorRawQueryLocalStorageKey, value ? '1' : '0');
  57. }
  58. /**
  59. * Use and store value of raw query switch in local storage.
  60. * Needs to be a hook with local state to trigger rerenders.
  61. */
  62. export function useRawQuery(): [boolean, (val: boolean) => void] {
  63. const [rawQuery, setRawQuery] = useState(getRawQueryVisibility());
  64. const setter = useCallback((value: boolean) => {
  65. setRawQueryVisibility(value);
  66. setRawQuery(value);
  67. }, []);
  68. return [rawQuery, setter];
  69. }