TimeRangePicker: do not swap time ranges when to is before from (#78915)

This commit is contained in:
Laura Fernández 2023-12-13 14:40:09 +01:00 committed by GitHub
parent 65535a3b3f
commit d28284c8f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 23 deletions

View File

@ -111,6 +111,9 @@ export function TimeRangePicker(props: TimeRangePickerProps) {
const variant = isSynced ? 'active' : isOnCanvas ? 'canvas' : 'default'; const variant = isSynced ? 'active' : isOnCanvas ? 'canvas' : 'default';
const isFromAfterTo = value?.to?.isBefore(value.from);
const timePickerIcon = isFromAfterTo ? 'exclamation-triangle' : 'clock-nine';
const currentTimeRange = formattedRange(value, timeZone); const currentTimeRange = formattedRange(value, timeZone);
return ( return (
@ -138,7 +141,7 @@ export function TimeRangePicker(props: TimeRangePickerProps) {
})} })}
aria-controls="TimePickerContent" aria-controls="TimePickerContent"
onClick={onToolbarButtonSwitch} onClick={onToolbarButtonSwitch}
icon="clock-nine" icon={timePickerIcon}
isOpen={isOpen} isOpen={isOpen}
variant={variant} variant={variant}
> >

View File

@ -210,26 +210,26 @@ describe('timeSrv', () => {
expect(time.to.valueOf()).toEqual(1410337650000); expect(time.to.valueOf()).toEqual(1410337650000);
}); });
it('corrects inverted from/to dates in ms', () => { it('does not correct inverted from/to dates in ms', () => {
locationService.push('/d/id?from=1621436828909&to=1621436818909'); locationService.push('/d/id?from=1621436828909&to=1621436818909');
timeSrv = new TimeSrv(new ContextSrvStub()); timeSrv = new TimeSrv(new ContextSrvStub());
timeSrv.init(_dashboard); timeSrv.init(_dashboard);
const time = timeSrv.timeRange(); const time = timeSrv.timeRange();
expect(time.from.valueOf()).toEqual(1621436818909); expect(time.from.valueOf()).toEqual(1621436828909);
expect(time.to.valueOf()).toEqual(1621436828909); expect(time.to.valueOf()).toEqual(1621436818909);
}); });
it('corrects inverted from/to dates as relative times', () => { it('does not correct inverted from/to dates as relative times', () => {
locationService.push('/d/id?from=now&to=now-1h'); locationService.push('/d/id?from=now&to=now-1h');
timeSrv = new TimeSrv(new ContextSrvStub()); timeSrv = new TimeSrv(new ContextSrvStub());
timeSrv.init(_dashboard); timeSrv.init(_dashboard);
const time = timeSrv.timeRange(); const time = timeSrv.timeRange();
expect(time.raw.from).toBe('now-1h'); expect(time.raw.from).toBe('now');
expect(time.raw.to).toBe('now'); expect(time.raw.to).toBe('now-1h');
}); });
}); });
}); });

View File

@ -70,22 +70,6 @@ export class TimeSrv {
// remember time at load so we can go back to it // remember time at load so we can go back to it
this.timeAtLoad = cloneDeep(this.time); this.timeAtLoad = cloneDeep(this.time);
const range = rangeUtil.convertRawToRange(
this.time,
this.timeModel?.getTimezone(),
this.timeModel?.fiscalYearStartMonth
);
if (range.to.isBefore(range.from)) {
this.setTime(
{
from: range.raw.to,
to: range.raw.from,
},
false
);
}
if (this.refresh) { if (this.refresh) {
this.setAutoRefresh(this.refresh); this.setAutoRefresh(this.refresh);
} }