Skip to content

Commit

Permalink
[Dashboards] Delay update in timeslider control
Browse files Browse the repository at this point in the history
  • Loading branch information
paulinashakirova committed Nov 25, 2024
1 parent ad5a8ef commit 5f49f41
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import React, { useEffect, useMemo } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import { BehaviorSubject, debounceTime, first, map } from 'rxjs';

import { EuiInputPopover } from '@elastic/eui';
Expand All @@ -22,6 +22,7 @@ import {
useBatchedPublishingSubjects,
} from '@kbn/presentation-publishing';

import { debounce } from 'lodash';
import { TIME_SLIDER_CONTROL } from '../../../common';
import { initializeDefaultControlApi } from '../initialize_default_control_api';
import { ControlFactory } from '../types';
Expand Down Expand Up @@ -112,6 +113,10 @@ export const getTimesliderControlFactory = (): ControlFactory<
setSelectedRange(nextSelectedRange);
}

const debouncedOnChange = debounce((updateTimeslice) => {
onChange(updateTimeslice);
}, 300);

function onPrevious() {
const { ticks, timeRangeMax, timeRangeMin } = timeRangeMeta$.value;
const value = timeslice$.value;
Expand Down Expand Up @@ -270,7 +275,10 @@ export const getTimesliderControlFactory = (): ControlFactory<
Component: (controlPanelClassNames) => {
const [isAnchored, isPopoverOpen, timeRangeMeta, timeslice] =
useBatchedPublishingSubjects(isAnchored$, isPopoverOpen$, timeRangeMeta$, timeslice$);

const [localTimeslice, setLocalTimeslice] = useState<Timeslice>([
timeRangeMeta.timeRangeMin,
timeRangeMeta.timeRangeMax,
]);
useEffect(() => {
return () => {
cleanupTimeRangeSubscription();
Expand Down Expand Up @@ -306,8 +314,11 @@ export const getTimesliderControlFactory = (): ControlFactory<
<TimeSliderPopoverContent
isAnchored={typeof isAnchored === 'boolean' ? isAnchored : false}
setIsAnchored={setIsAnchored}
value={[from, to]}
onChange={onChange}
value={localTimeslice}
onChange={(value) => {
setLocalTimeslice(value as Timeslice);
debouncedOnChange(value);
}}
stepSize={timeRangeMeta.stepSize}
ticks={timeRangeMeta.ticks}
timeRangeMin={timeRangeMeta.timeRangeMin}
Expand Down

0 comments on commit 5f49f41

Please sign in to comment.