Skip to content

Commit

Permalink
fix: relative time param from the url not respected (#5545)
Browse files Browse the repository at this point in the history
* fix: relative time param from the url not respected

* chore: added code comments and the priorities of the params

* fix: added validity checks for the relativeTime in the url
  • Loading branch information
vikrantgupta25 authored Jul 25, 2024
1 parent d60daef commit 05bd6d5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions frontend/src/container/TopNav/DateTimeSelectionV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import NewExplorerCTA from 'container/NewExplorerCTA';
import dayjs, { Dayjs } from 'dayjs';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import useUrlQuery from 'hooks/useUrlQuery';
import GetMinMax from 'lib/getMinMax';
import GetMinMax, { isValidTimeFormat } from 'lib/getMinMax';
import getTimeString from 'lib/getTimeString';
import history from 'lib/history';
import { isObject } from 'lodash-es';
Expand Down Expand Up @@ -73,6 +73,7 @@ function DateTimeSelection({
const urlQuery = useUrlQuery();
const searchStartTime = urlQuery.get('startTime');
const searchEndTime = urlQuery.get('endTime');
const relativeTimeFromUrl = urlQuery.get(QueryParams.relativeTime);
const queryClient = useQueryClient();
const [enableAbsoluteTime, setEnableAbsoluteTime] = useState(false);
const [isValidteRelativeTime, setIsValidteRelativeTime] = useState(false);
Expand Down Expand Up @@ -404,16 +405,26 @@ function DateTimeSelection({
time: Time,
currentRoute: string,
): Time | CustomTimeType => {
// if the relativeTime param is present in the url give top most preference to the same
// if the relativeTime param is not valid then move to next preference
if (relativeTimeFromUrl != null && isValidTimeFormat(relativeTimeFromUrl)) {
return relativeTimeFromUrl as Time;
}

// if the startTime and endTime params are present in the url give next preference to the them.
if (searchEndTime !== null && searchStartTime !== null) {
return 'custom';
}

// if nothing is present in the url for time range then rely on the local storage values
if (
(localstorageEndTime === null || localstorageStartTime === null) &&
time === 'custom'
) {
return getDefaultOption(currentRoute);
}

// if not present in the local storage as well then rely on the defaults set for the page
if (OLD_RELATIVE_TIME_VALUES.indexOf(time) > -1) {
return convertOldTimeToNewValidCustomTimeFormat(time);
}
Expand Down Expand Up @@ -448,7 +459,11 @@ function DateTimeSelection({

setRefreshButtonHidden(updatedTime === 'custom');

updateTimeInterval(updatedTime, [preStartTime, preEndTime]);
if (updatedTime !== 'custom') {
updateTimeInterval(updatedTime);
} else {
updateTimeInterval(updatedTime, [preStartTime, preEndTime]);
}

if (updatedTime !== 'custom') {
urlQuery.delete('startTime');
Expand Down

0 comments on commit 05bd6d5

Please sign in to comment.