@grafana/scenes 5.15.1 → 5.16.0--canary.915.11034968022.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,10 @@
1
1
  import React from 'react';
2
+ import { useLocalStorage } from 'react-use';
3
+ import { uniqBy } from 'lodash';
4
+ import { toUtc, rangeUtil, isDateTime } from '@grafana/data';
2
5
  import { TimeRangePicker } from '@grafana/ui';
3
6
  import { SceneObjectBase } from '../core/SceneObjectBase.js';
4
7
  import { sceneGraph } from '../core/sceneGraph/index.js';
5
- import { toUtc } from '@grafana/data';
6
8
 
7
9
  class SceneTimePicker extends SceneObjectBase {
8
10
  constructor() {
@@ -45,13 +47,23 @@ function SceneTimePickerRenderer({ model }) {
45
47
  const timeRange = sceneGraph.getTimeRange(model);
46
48
  const timeZone = timeRange.getTimeZone();
47
49
  const timeRangeState = timeRange.useState();
50
+ const [timeRangeHistory, setTimeRangeHistory] = useLocalStorage(HISTORY_LOCAL_STORAGE_KEY, [], {
51
+ raw: false,
52
+ serializer: serializeHistory,
53
+ deserializer: deserializeHistory
54
+ });
48
55
  if (hidePicker) {
49
56
  return null;
50
57
  }
51
58
  return /* @__PURE__ */ React.createElement(TimeRangePicker, {
52
59
  isOnCanvas: isOnCanvas != null ? isOnCanvas : true,
53
60
  value: timeRangeState.value,
54
- onChange: timeRange.onTimeRangeChange,
61
+ onChange: (range) => {
62
+ if (isAbsolute(range)) {
63
+ setTimeRangeHistory([range, ...timeRangeHistory != null ? timeRangeHistory : []]);
64
+ }
65
+ timeRange.onTimeRangeChange(range);
66
+ },
55
67
  timeZone,
56
68
  fiscalYearStartMonth: timeRangeState.fiscalYearStartMonth,
57
69
  onMoveBackward: model.onMoveBackward,
@@ -59,7 +71,8 @@ function SceneTimePickerRenderer({ model }) {
59
71
  onZoom: model.onZoom,
60
72
  onChangeTimeZone: timeRange.onTimeZoneChange,
61
73
  onChangeFiscalYearStartMonth: model.onChangeFiscalYearStartMonth,
62
- weekStart: timeRangeState.weekStart
74
+ weekStart: timeRangeState.weekStart,
75
+ history: timeRangeHistory
63
76
  });
64
77
  }
65
78
  function getZoomedTimeRange(timeRange, factor) {
@@ -100,6 +113,27 @@ function getShiftedTimeRange(dir, timeRange, upperLimit) {
100
113
  raw: { from, to }
101
114
  };
102
115
  }
116
+ const HISTORY_LOCAL_STORAGE_KEY = "grafana.dashboard.timepicker.history";
117
+ function deserializeHistory(value) {
118
+ const values = JSON.parse(value);
119
+ return values.map((item) => rangeUtil.convertRawToRange(item, "utc", void 0, "YYYY-MM-DD HH:mm:ss"));
120
+ }
121
+ function serializeHistory(values) {
122
+ return JSON.stringify(
123
+ limit(
124
+ values.map((v) => ({
125
+ from: typeof v.raw.from === "string" ? v.raw.from : v.raw.from.toISOString(),
126
+ to: typeof v.raw.to === "string" ? v.raw.to : v.raw.to.toISOString()
127
+ }))
128
+ )
129
+ );
130
+ }
131
+ function limit(value) {
132
+ return uniqBy(value, (v) => v.from + v.to).slice(0, 4);
133
+ }
134
+ function isAbsolute(value) {
135
+ return isDateTime(value.raw.from) || isDateTime(value.raw.to);
136
+ }
103
137
 
104
138
  export { SceneTimePicker, TimeRangeDirection, getShiftedTimeRange, getZoomedTimeRange };
105
139
  //# sourceMappingURL=SceneTimePicker.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SceneTimePicker.js","sources":["../../../src/components/SceneTimePicker.tsx"],"sourcesContent":["import React from 'react';\n\nimport { TimeRangePicker } from '@grafana/ui';\n\nimport { SceneObjectBase } from '../core/SceneObjectBase';\nimport { sceneGraph } from '../core/sceneGraph';\nimport { SceneComponentProps, SceneObjectState } from '../core/types';\nimport { TimeRange, toUtc } from '@grafana/data';\n\nexport interface SceneTimePickerState extends SceneObjectState {\n hidePicker?: boolean;\n isOnCanvas?: boolean;\n}\n\nexport class SceneTimePicker extends SceneObjectBase<SceneTimePickerState> {\n public static Component = SceneTimePickerRenderer;\n\n public onZoom = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const zoomedTimeRange = getZoomedTimeRange(timeRange.state.value, 2);\n timeRange.onTimeRangeChange(zoomedTimeRange);\n };\n\n public onChangeFiscalYearStartMonth = (month: number) => {\n const timeRange = sceneGraph.getTimeRange(this);\n timeRange.setState({ fiscalYearStartMonth: month });\n };\n\n public toAbsolute = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const timeRangeVal = timeRange.state.value;\n const from = toUtc(timeRangeVal.from);\n const to = toUtc(timeRangeVal.to);\n timeRange.onTimeRangeChange({ from, to, raw: { from, to } });\n };\n\n public onMoveBackward = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const {\n state: { value: range },\n } = timeRange;\n\n timeRange.onTimeRangeChange(getShiftedTimeRange(TimeRangeDirection.Backward, range, Date.now()));\n };\n\n public onMoveForward = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const {\n state: { value: range },\n } = timeRange;\n\n timeRange.onTimeRangeChange(getShiftedTimeRange(TimeRangeDirection.Forward, range, Date.now()));\n };\n}\n\nfunction SceneTimePickerRenderer({ model }: SceneComponentProps<SceneTimePicker>) {\n const { hidePicker, isOnCanvas } = model.useState();\n const timeRange = sceneGraph.getTimeRange(model);\n const timeZone = timeRange.getTimeZone();\n const timeRangeState = timeRange.useState();\n\n if (hidePicker) {\n return null;\n }\n\n return (\n <TimeRangePicker\n isOnCanvas={isOnCanvas ?? true}\n value={timeRangeState.value}\n onChange={timeRange.onTimeRangeChange}\n timeZone={timeZone}\n fiscalYearStartMonth={timeRangeState.fiscalYearStartMonth}\n onMoveBackward={model.onMoveBackward}\n onMoveForward={model.onMoveForward}\n onZoom={model.onZoom}\n onChangeTimeZone={timeRange.onTimeZoneChange}\n onChangeFiscalYearStartMonth={model.onChangeFiscalYearStartMonth}\n // @ts-ignore TODO remove after grafana/ui update to 11.2.0\n weekStart={timeRangeState.weekStart}\n />\n );\n}\n\nexport function getZoomedTimeRange(timeRange: TimeRange, factor: number): TimeRange {\n const timespan = timeRange.to.valueOf() - timeRange.from.valueOf();\n const center = timeRange.to.valueOf() - timespan / 2;\n // If the timepsan is 0, zooming out would do nothing, so we force a zoom out to 30s\n const newTimespan = timespan === 0 ? 30000 : timespan * factor;\n\n const to = center + newTimespan / 2;\n const from = center - newTimespan / 2;\n\n return { from: toUtc(from), to: toUtc(to), raw: { from: toUtc(from), to: toUtc(to) } };\n}\n\nexport enum TimeRangeDirection {\n Backward,\n Forward,\n}\n\nexport function getShiftedTimeRange(dir: TimeRangeDirection, timeRange: TimeRange, upperLimit: number): TimeRange {\n const oldTo = timeRange.to.valueOf();\n const oldFrom = timeRange.from.valueOf();\n const halfSpan = (oldTo - oldFrom) / 2;\n\n let fromRaw: number;\n let toRaw: number;\n if (dir === TimeRangeDirection.Backward) {\n fromRaw = oldFrom - halfSpan;\n toRaw = oldTo - halfSpan;\n } else {\n fromRaw = oldFrom + halfSpan;\n toRaw = oldTo + halfSpan;\n\n if (toRaw > upperLimit && oldTo < upperLimit) {\n toRaw = upperLimit;\n fromRaw = oldFrom;\n }\n }\n\n const from = toUtc(fromRaw);\n const to = toUtc(toRaw);\n return {\n from,\n to,\n raw: { from, to },\n };\n}\n"],"names":["TimeRangeDirection"],"mappings":";;;;;;AAcO,MAAM,wBAAwB,eAAsC,CAAA;AAAA,EAApE,WAAA,GAAA;AAAA,IAAA,KAAA,CAAA,GAAA,SAAA,CAAA,CAAA;AAGL,IAAA,IAAA,CAAO,SAAS,MAAM;AACpB,MAAM,MAAA,SAAA,GAAY,UAAW,CAAA,YAAA,CAAa,IAAI,CAAA,CAAA;AAC9C,MAAA,MAAM,eAAkB,GAAA,kBAAA,CAAmB,SAAU,CAAA,KAAA,CAAM,OAAO,CAAC,CAAA,CAAA;AACnE,MAAA,SAAA,CAAU,kBAAkB,eAAe,CAAA,CAAA;AAAA,KAC7C,CAAA;AAEA,IAAO,IAAA,CAAA,4BAAA,GAA+B,CAAC,KAAkB,KAAA;AACvD,MAAM,MAAA,SAAA,GAAY,UAAW,CAAA,YAAA,CAAa,IAAI,CAAA,CAAA;AAC9C,MAAA,SAAA,CAAU,QA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AACpB,IAAA,KAAA,GAAQ,KAAQ,GAAA,QAAA,CAAA;AAEhB,IAAI,IAAA,KAAA,GAAQ,UAAc,IAAA,KAAA,GAAQ,UAAY,EAAA;AAC5C,MAAQ,KAAA,GAAA,UAAA,CAAA;AACR,MAAU,OAAA,GAAA,OAAA,CAAA;AAAA,KACZ;AAAA,GACF;AAEA,EAAM,MAAA,IAAA,GAAO,MAAM,OAAO,CAAA,CAAA;AAC1B,EAAM,MAAA,EAAA,GAAK,MAAM,KAAK,CAAA,CAAA;AACtB,EAAO,OAAA;AAAA,IACL,IAAA;AAAA,IACA,EAAA;AAAA,IACA,GAAA,EAAK,EAAE,IAAA,EAAM,EAAG,EAAA;AAAA,GAClB,CAAA;AACF;;;;"}
1
+ {"version":3,"file":"SceneTimePicker.js","sources":["../../../src/components/SceneTimePicker.tsx"],"sourcesContent":["import React from 'react';\nimport { useLocalStorage } from 'react-use';\nimport { uniqBy } from 'lodash';\n\nimport { TimeRange, isDateTime, rangeUtil, toUtc } from '@grafana/data';\nimport { TimeRangePicker } from '@grafana/ui';\n\nimport { SceneObjectBase } from '../core/SceneObjectBase';\nimport { sceneGraph } from '../core/sceneGraph';\nimport { SceneComponentProps, SceneObjectState } from '../core/types';\n\nexport interface SceneTimePickerState extends SceneObjectState {\n hidePicker?: boolean;\n isOnCanvas?: boolean;\n}\n\nexport class SceneTimePicker extends SceneObjectBase<SceneTimePickerState> {\n public static Component = SceneTimePickerRenderer;\n\n public onZoom = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const zoomedTimeRange = getZoomedTimeRange(timeRange.state.value, 2);\n timeRange.onTimeRangeChange(zoomedTimeRange);\n };\n\n public onChangeFiscalYearStartMonth = (month: number) => {\n const timeRange = sceneGraph.getTimeRange(this);\n timeRange.setState({ fiscalYearStartMonth: month });\n };\n\n public toAbsolute = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const timeRangeVal = timeRange.state.value;\n const from = toUtc(timeRangeVal.from);\n const to = toUtc(timeRangeVal.to);\n timeRange.onTimeRangeChange({ from, to, raw: { from, to } });\n };\n\n public onMoveBackward = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const {\n state: { value: range },\n } = timeRange;\n\n timeRange.onTimeRangeChange(getShiftedTimeRange(TimeRangeDirection.Backward, range, Date.now()));\n };\n\n public onMoveForward = () => {\n const timeRange = sceneGraph.getTimeRange(this);\n const {\n state: { value: range },\n } = timeRange;\n\n timeRange.onTimeRangeChange(getShiftedTimeRange(TimeRangeDirection.Forward, range, Date.now()));\n };\n}\n\nfunction SceneTimePickerRenderer({ model }: SceneComponentProps<SceneTimePicker>) {\n const { hidePicker, isOnCanvas } = model.useState();\n const timeRange = sceneGraph.getTimeRange(model);\n const timeZone = timeRange.getTimeZone();\n const timeRangeState = timeRange.useState();\n const [timeRangeHistory, setTimeRangeHistory] = useLocalStorage<TimeRange[]>(HISTORY_LOCAL_STORAGE_KEY, [], {\n raw: false,\n serializer: serializeHistory,\n deserializer: deserializeHistory,\n });\n\n if (hidePicker) {\n return null;\n }\n\n return (\n <TimeRangePicker\n isOnCanvas={isOnCanvas ?? true}\n value={timeRangeState.value}\n onChange={(range) => {\n if (isAbsolute(range)) {\n setTimeRangeHistory([range, ...(timeRangeHistory ?? [])]);\n }\n\n timeRange.onTimeRangeChange(range);\n }}\n timeZone={timeZone}\n fiscalYearStartMonth={timeRangeState.fiscalYearStartMonth}\n onMoveBackward={model.onMoveBackward}\n onMoveForward={model.onMoveForward}\n onZoom={model.onZoom}\n onChangeTimeZone={timeRange.onTimeZoneChange}\n onChangeFiscalYearStartMonth={model.onChangeFiscalYearStartMonth}\n // @ts-ignore TODO remove after grafana/ui update to 11.2.0\n weekStart={timeRangeState.weekStart}\n history={timeRangeHistory}\n />\n );\n}\n\nexport function getZoomedTimeRange(timeRange: TimeRange, factor: number): TimeRange {\n const timespan = timeRange.to.valueOf() - timeRange.from.valueOf();\n const center = timeRange.to.valueOf() - timespan / 2;\n // If the timepsan is 0, zooming out would do nothing, so we force a zoom out to 30s\n const newTimespan = timespan === 0 ? 30000 : timespan * factor;\n\n const to = center + newTimespan / 2;\n const from = center - newTimespan / 2;\n\n return { from: toUtc(from), to: toUtc(to), raw: { from: toUtc(from), to: toUtc(to) } };\n}\n\nexport enum TimeRangeDirection {\n Backward,\n Forward,\n}\n\nexport function getShiftedTimeRange(dir: TimeRangeDirection, timeRange: TimeRange, upperLimit: number): TimeRange {\n const oldTo = timeRange.to.valueOf();\n const oldFrom = timeRange.from.valueOf();\n const halfSpan = (oldTo - oldFrom) / 2;\n\n let fromRaw: number;\n let toRaw: number;\n if (dir === TimeRangeDirection.Backward) {\n fromRaw = oldFrom - halfSpan;\n toRaw = oldTo - halfSpan;\n } else {\n fromRaw = oldFrom + halfSpan;\n toRaw = oldTo + halfSpan;\n\n if (toRaw > upperLimit && oldTo < upperLimit) {\n toRaw = upperLimit;\n fromRaw = oldFrom;\n }\n }\n\n const from = toUtc(fromRaw);\n const to = toUtc(toRaw);\n return {\n from,\n to,\n raw: { from, to },\n };\n}\n\nconst HISTORY_LOCAL_STORAGE_KEY = 'grafana.dashboard.timepicker.history';\n\n// Simplified object to store in local storage\ninterface TimePickerHistoryItem {\n from: string;\n to: string;\n}\n\nfunction deserializeHistory(value: string): TimeRange[] {\n const values: TimePickerHistoryItem[] = JSON.parse(value);\n // The history is saved in UTC and with the default date format, so we need to pass those values to the convertRawToRange\n return values.map((item) => rangeUtil.convertRawToRange(item, 'utc', undefined, 'YYYY-MM-DD HH:mm:ss'));\n}\n\nfunction serializeHistory(values: TimeRange[]) {\n return JSON.stringify(\n limit(\n values.map((v) => ({\n from: typeof v.raw.from === 'string' ? v.raw.from : v.raw.from.toISOString(),\n to: typeof v.raw.to === 'string' ? v.raw.to : v.raw.to.toISOString(),\n }))\n )\n );\n}\n\nfunction limit(value: TimePickerHistoryItem[]): TimePickerHistoryItem[] {\n return uniqBy(value, (v) => v.from + v.to).slice(0, 4);\n}\n\nfunction isAbsolute(value: TimeRange): boolean {\n return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CAAC,CAAM,KAAA,CAAA,CAAE,IAAO,GAAA,CAAA,CAAE,EAAE,CAAA,CAAE,KAAM,CAAA,CAAA,EAAG,CAAC,CAAA,CAAA;AACvD,CAAA;AAEA,SAAS,WAAW,KAA2B,EAAA;AAC7C,EAAO,OAAA,UAAA,CAAW,MAAM,GAAI,CAAA,IAAI,KAAK,UAAW,CAAA,KAAA,CAAM,IAAI,EAAE,CAAA,CAAA;AAC9D;;;;"}
package/dist/index.js CHANGED
@@ -10611,13 +10611,23 @@ function SceneTimePickerRenderer({ model }) {
10611
10611
  const timeRange = sceneGraph.getTimeRange(model);
10612
10612
  const timeZone = timeRange.getTimeZone();
10613
10613
  const timeRangeState = timeRange.useState();
10614
+ const [timeRangeHistory, setTimeRangeHistory] = reactUse.useLocalStorage(HISTORY_LOCAL_STORAGE_KEY, [], {
10615
+ raw: false,
10616
+ serializer: serializeHistory,
10617
+ deserializer: deserializeHistory
10618
+ });
10614
10619
  if (hidePicker) {
10615
10620
  return null;
10616
10621
  }
10617
10622
  return /* @__PURE__ */ React__default["default"].createElement(ui.TimeRangePicker, {
10618
10623
  isOnCanvas: isOnCanvas != null ? isOnCanvas : true,
10619
10624
  value: timeRangeState.value,
10620
- onChange: timeRange.onTimeRangeChange,
10625
+ onChange: (range) => {
10626
+ if (isAbsolute(range)) {
10627
+ setTimeRangeHistory([range, ...timeRangeHistory != null ? timeRangeHistory : []]);
10628
+ }
10629
+ timeRange.onTimeRangeChange(range);
10630
+ },
10621
10631
  timeZone,
10622
10632
  fiscalYearStartMonth: timeRangeState.fiscalYearStartMonth,
10623
10633
  onMoveBackward: model.onMoveBackward,
@@ -10625,7 +10635,8 @@ function SceneTimePickerRenderer({ model }) {
10625
10635
  onZoom: model.onZoom,
10626
10636
  onChangeTimeZone: timeRange.onTimeZoneChange,
10627
10637
  onChangeFiscalYearStartMonth: model.onChangeFiscalYearStartMonth,
10628
- weekStart: timeRangeState.weekStart
10638
+ weekStart: timeRangeState.weekStart,
10639
+ history: timeRangeHistory
10629
10640
  });
10630
10641
  }
10631
10642
  function getZoomedTimeRange(timeRange, factor) {
@@ -10666,6 +10677,27 @@ function getShiftedTimeRange(dir, timeRange, upperLimit) {
10666
10677
  raw: { from, to }
10667
10678
  };
10668
10679
  }
10680
+ const HISTORY_LOCAL_STORAGE_KEY = "grafana.dashboard.timepicker.history";
10681
+ function deserializeHistory(value) {
10682
+ const values = JSON.parse(value);
10683
+ return values.map((item) => data.rangeUtil.convertRawToRange(item, "utc", void 0, "YYYY-MM-DD HH:mm:ss"));
10684
+ }
10685
+ function serializeHistory(values) {
10686
+ return JSON.stringify(
10687
+ limit(
10688
+ values.map((v) => ({
10689
+ from: typeof v.raw.from === "string" ? v.raw.from : v.raw.from.toISOString(),
10690
+ to: typeof v.raw.to === "string" ? v.raw.to : v.raw.to.toISOString()
10691
+ }))
10692
+ )
10693
+ );
10694
+ }
10695
+ function limit(value) {
10696
+ return lodash.uniqBy(value, (v) => v.from + v.to).slice(0, 4);
10697
+ }
10698
+ function isAbsolute(value) {
10699
+ return data.isDateTime(value.raw.from) || data.isDateTime(value.raw.to);
10700
+ }
10669
10701
 
10670
10702
  var __defProp$6 = Object.defineProperty;
10671
10703
  var __defProps$3 = Object.defineProperties;