diff --git a/app/vmui/packages/vmui/src/components/CustomPanel/Configurator/Query/QueryConfigurator.tsx b/app/vmui/packages/vmui/src/components/CustomPanel/Configurator/Query/QueryConfigurator.tsx index 78d5fcae6..c8948d315 100644 --- a/app/vmui/packages/vmui/src/components/CustomPanel/Configurator/Query/QueryConfigurator.tsx +++ b/app/vmui/packages/vmui/src/components/CustomPanel/Configurator/Query/QueryConfigurator.tsx @@ -1,4 +1,4 @@ -import React, {FC, useEffect, useRef} from "preact/compat"; +import React, {FC, useState} from "preact/compat"; import Box from "@mui/material/Box"; import IconButton from "@mui/material/IconButton"; import Tooltip from "@mui/material/Tooltip"; @@ -18,15 +18,12 @@ export interface QueryConfiguratorProps { const QueryConfigurator: FC = ({error, queryOptions}) => { const {query, queryHistory, queryControls: {autocomplete}} = useAppState(); + const [stateQuery, setStateQuery] = useState(query || []); const dispatch = useAppDispatch(); - const queryRef = useRef(query); - useEffect(() => { - queryRef.current = query; - }, [query]); const updateHistory = () => { dispatch({ - type: "SET_QUERY_HISTORY", payload: query.map((q, i) => { + type: "SET_QUERY_HISTORY", payload: stateQuery.map((q, i) => { const h = queryHistory[i] || {values: []}; const queryEqual = q === h.values[h.values.length - 1]; return { @@ -39,22 +36,20 @@ const QueryConfigurator: FC = ({error, queryOptions}) => const onRunQuery = () => { updateHistory(); - dispatch({type: "SET_QUERY", payload: query}); + dispatch({type: "SET_QUERY", payload: stateQuery}); dispatch({type: "RUN_QUERY"}); }; - const onAddQuery = () => dispatch({type: "SET_QUERY", payload: [...queryRef.current, ""]}); + const onAddQuery = () => { + setStateQuery(prev => [...prev, ""]); + }; const onRemoveQuery = (index: number) => { - const newQuery = [...queryRef.current]; - newQuery.splice(index, 1); - dispatch({type: "SET_QUERY", payload: newQuery}); + setStateQuery(prev => prev.filter((q, i) => i !== index)); }; const onSetQuery = (value: string, index: number) => { - const newQuery = [...queryRef.current]; - newQuery[index] = value; - dispatch({type: "SET_QUERY", payload: newQuery}); + setStateQuery(prev => prev.map((q, i) => i === index ? value : q)); }; const setHistoryIndex = (step: number, indexQuery: number) => { @@ -69,11 +64,11 @@ const QueryConfigurator: FC = ({error, queryOptions}) => }; return - {query.map((q, i) => + {stateQuery.map((q, i) => + mb={i === stateQuery.length - 1 ? 0 : 2.5}> {i === 0 && @@ -81,7 +76,7 @@ const QueryConfigurator: FC = ({error, queryOptions}) => } - {query.length < 2 && + {stateQuery.length < 2 && diff --git a/app/vmui/packages/vmui/src/state/common/reducer.ts b/app/vmui/packages/vmui/src/state/common/reducer.ts index 2df89c85b..f2ed94ed5 100644 --- a/app/vmui/packages/vmui/src/state/common/reducer.ts +++ b/app/vmui/packages/vmui/src/state/common/reducer.ts @@ -125,6 +125,7 @@ export function reducer(state: AppState, action: Action): AppState { ...state, time: { ...state.time, + duration: action.payload.duration, period: getTimeperiodForDuration(action.payload.duration, new Date(action.payload.until)), relativeTime: action.payload.id, } diff --git a/app/vmui/packages/vmui/src/utils/query-string.ts b/app/vmui/packages/vmui/src/utils/query-string.ts index 4743a1999..012dab01d 100644 --- a/app/vmui/packages/vmui/src/utils/query-string.ts +++ b/app/vmui/packages/vmui/src/utils/query-string.ts @@ -52,9 +52,9 @@ export const setQueryStringWithoutPageReload = (qsValue: string): void => { export const setQueryStringValue = (newValue: Record): void => { const route = window.location.hash.replace("#", ""); - const params = stateToUrlParams[route] || {}; + const params = stateToUrlParams[route] || graphStateToUrlParams; const queryMap = new Map(Object.entries(params)); - const isGraphRoute = route === router.home || route === router.dashboards; + const isGraphRoute = route === router.home || route === router.dashboards || !route; const newQsValue = isGraphRoute ? getGraphQsValue(newValue, queryMap) : getQsValue(newValue, queryMap); setQueryStringWithoutPageReload(newQsValue.join("&")); };