vmui: prevent reset relative time (#2543)

* fix: prevent time picker reset to previous time

* fix: add default display type
This commit is contained in:
Yury Molodov 2022-05-05 21:21:02 +03:00 committed by Aliaksandr Valialkin
parent eba02163fe
commit 7b37662a80
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
2 changed files with 21 additions and 13 deletions

View File

@ -56,19 +56,21 @@ export type Action =
| { type: "TOGGLE_AUTOCOMPLETE"}
| { type: "NO_CACHE"}
const {relativeDuration, relativeUntil, relativeTimeId} = getRelativeTime();
const duration = relativeDuration || getQueryStringValue("g0.range_input", "1h") as string;
const endInput = relativeUntil || formatDateToLocal(getQueryStringValue("g0.end_input", getDateNowUTC()) as Date);
const {duration, endInput, relativeTimeId} = getRelativeTime({
defaultDuration: getQueryStringValue("g0.range_input", "1h") as string,
defaultEndInput: new Date(formatDateToLocal(getQueryStringValue("g0.end_input", getDateNowUTC()) as Date)),
});
const query = getQueryArray();
export const initialState: AppState = {
serverUrl: getDefaultServer(),
displayType: getQueryStringValue("g0.tab", "chart") as DisplayType,
displayType: getQueryStringValue("g0.tab", "chart") as DisplayType || "chart",
query: query, // demo_memory_usage_bytes
queryHistory: query.map(q => ({index: 0, values: [q]})),
time: {
duration,
period: getTimeperiodForDuration(duration, new Date(endInput)),
period: getTimeperiodForDuration(duration, endInput),
relativeTime: relativeTimeId,
},
queryControls: {
@ -152,7 +154,7 @@ export function reducer(state: AppState, action: Action): AppState {
};
case "SET_PERIOD":
// eslint-disable-next-line no-case-declarations
const duration = getDurationFromPeriod(action.payload);
const durationPeriod = getDurationFromPeriod(action.payload);
return {
...state,
queryControls: {
@ -161,8 +163,8 @@ export function reducer(state: AppState, action: Action): AppState {
},
time: {
...state.time,
duration,
period: getTimeperiodForDuration(duration, action.payload.to),
duration: durationPeriod,
period: getTimeperiodForDuration(durationPeriod, action.payload.to),
relativeTime: ""
}
};
@ -191,11 +193,17 @@ export function reducer(state: AppState, action: Action): AppState {
}
};
case "RUN_QUERY":
// eslint-disable-next-line no-case-declarations
const {duration: durationRunQuery, endInput} = getRelativeTime({
relativeTimeId: state.time.relativeTime,
defaultDuration: state.time.duration,
defaultEndInput: dateFromSeconds(state.time.period.end),
});
return {
...state,
time: {
...state.time,
period: getTimeperiodForDuration(state.time.duration, dateFromSeconds(state.time.period.end))
period: getTimeperiodForDuration(durationRunQuery, endInput)
}
};
case "RUN_QUERY_TO_NOW":

View File

@ -131,13 +131,13 @@ export const relativeTimeOptions: RelativeTimeOption[] = [
...o
}));
export const getRelativeTime = (relativeTimeId?: string) => {
export const getRelativeTime = ({relativeTimeId, defaultDuration, defaultEndInput}:
{ relativeTimeId?: string, defaultDuration: string, defaultEndInput: Date }) => {
const id = relativeTimeId || getQueryStringValue("g0.relative_time", "") as string;
const target = relativeTimeOptions.find(d => d.id === id);
if (!target) return {};
return {
relativeTimeId: id,
relativeDuration: target.duration,
relativeUntil: target.until()
duration: target ? target.duration : defaultDuration,
endInput: target ? target.until() : defaultEndInput
};
};