feat: introduce application mode

This commit is contained in:
Yury Moladau 2021-12-14 15:38:19 +03:00
parent 91beca3cc9
commit 040d8481ab
4 changed files with 39 additions and 7 deletions

View File

@ -1,9 +1,10 @@
import React, {FC, useState} from "react"; import React, {FC, useEffect, useState} from "react";
import {Box, TextField, Tooltip, IconButton} from "@mui/material"; import {Box, TextField, Tooltip, IconButton} from "@mui/material";
import SecurityIcon from "@mui/icons-material/Security"; import SecurityIcon from "@mui/icons-material/Security";
import {useAppDispatch, useAppState} from "../../../../state/common/StateContext"; import {useAppDispatch, useAppState} from "../../../../state/common/StateContext";
import {AuthDialog} from "../Auth/AuthDialog"; import {AuthDialog} from "../Auth/AuthDialog";
import {ErrorTypes} from "../../../../types"; import {ErrorTypes} from "../../../../types";
import {getAppModeEnable, getAppModeParams} from "../../../../utils/app-mode";
export interface ServerConfiguratorProps { export interface ServerConfiguratorProps {
error?: ErrorTypes | string; error?: ErrorTypes | string;
@ -11,6 +12,9 @@ export interface ServerConfiguratorProps {
const ServerConfigurator: FC<ServerConfiguratorProps> = ({error}) => { const ServerConfigurator: FC<ServerConfiguratorProps> = ({error}) => {
const appModeEnable = getAppModeEnable();
const {serverURL: appServerUrl} = getAppModeParams();
const {serverUrl} = useAppState(); const {serverUrl} = useAppState();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
@ -19,9 +23,13 @@ const ServerConfigurator: FC<ServerConfiguratorProps> = ({error}) => {
}; };
const [dialogOpen, setDialogOpen] = useState(false); const [dialogOpen, setDialogOpen] = useState(false);
useEffect(() => {
if (appModeEnable) dispatch({type: "SET_SERVER", payload: appServerUrl});
}, [appServerUrl]);
return <> return <>
<Box display="grid" gridTemplateColumns="1fr auto" gap="4px" alignItems="center" width="100%" mb={2} minHeight={50}> <Box display="grid" gridTemplateColumns="1fr auto" gap="4px" alignItems="center" width="100%" mb={2} minHeight={50}>
<TextField variant="outlined" fullWidth label="Server URL" value={serverUrl} <TextField variant="outlined" fullWidth label="Server URL" value={serverUrl || ""} disabled={appModeEnable}
error={error === ErrorTypes.validServer || error === ErrorTypes.emptyServer} error={error === ErrorTypes.validServer || error === ErrorTypes.emptyServer}
inputProps={{style: {fontFamily: "Monospace"}}} inputProps={{style: {fontFamily: "Monospace"}}}
onChange={onSetServer}/> onChange={onSetServer}/>

View File

@ -6,6 +6,10 @@ import {isValidHttpUrl} from "../../../../utils/url";
import {useAuthState} from "../../../../state/auth/AuthStateContext"; import {useAuthState} from "../../../../state/auth/AuthStateContext";
import {ErrorTypes, TimeParams} from "../../../../types"; import {ErrorTypes, TimeParams} from "../../../../types";
import {useGraphState} from "../../../../state/graph/GraphStateContext"; import {useGraphState} from "../../../../state/graph/GraphStateContext";
import {getAppModeEnable, getAppModeParams} from "../../../../utils/app-mode";
const appModeEnable = getAppModeEnable();
const {serverURL: appServerUrl} = getAppModeParams();
export const useFetchQuery = (): { export const useFetchQuery = (): {
fetchUrl?: string[], fetchUrl?: string[],
@ -80,18 +84,19 @@ export const useFetchQuery = (): {
}; };
const fetchUrl = useMemo(() => { const fetchUrl = useMemo(() => {
const server = appModeEnable ? appServerUrl : serverUrl;
if (!period) return; if (!period) return;
if (!serverUrl) { if (!server) {
setError(ErrorTypes.emptyServer); setError(ErrorTypes.emptyServer);
} else if (query.every(q => !q.trim())) { } else if (query.every(q => !q.trim())) {
setError(ErrorTypes.validQuery); setError(ErrorTypes.validQuery);
} else if (isValidHttpUrl(serverUrl)) { } else if (isValidHttpUrl(server)) {
const duration = (period.end - period.start) / 2; const duration = (period.end - period.start) / 2;
const bufferPeriod = {...period, start: period.start - duration, end: period.end + duration}; const bufferPeriod = {...period, start: period.start - duration, end: period.end + duration};
if (customStep.enable) bufferPeriod.step = customStep.value; if (customStep.enable) bufferPeriod.step = customStep.value;
return query.filter(q => q.trim()).map(q => displayType === "chart" return query.filter(q => q.trim()).map(q => displayType === "chart"
? getQueryRangeUrl(serverUrl, q, bufferPeriod, nocache) ? getQueryRangeUrl(server, q, bufferPeriod, nocache)
: getQueryUrl(serverUrl, q, period)); : getQueryUrl(server, q, period));
} else { } else {
setError(ErrorTypes.validServer); setError(ErrorTypes.validServer);
} }

View File

@ -0,0 +1,12 @@
export interface AppParams {
serverURL: string
}
const getAppModeParams = (): AppParams => {
const dataParams = document.getElementById("root")?.dataset.params || "{}";
return JSON.parse(dataParams);
};
const getAppModeEnable = (): boolean => !!Object.keys(getAppModeParams()).length;
export {getAppModeEnable, getAppModeParams};

View File

@ -1,5 +1,6 @@
import qs from "qs"; import qs from "qs";
import get from "lodash.get"; import get from "lodash.get";
import {getAppModeEnable} from "./app-mode";
const stateToUrlParams = { const stateToUrlParams = {
"time.duration": "range_input", "time.duration": "range_input",
@ -8,6 +9,8 @@ const stateToUrlParams = {
"displayType": "tab" "displayType": "tab"
}; };
const appModeEnable = getAppModeEnable();
// TODO need function for detect types. // TODO need function for detect types.
// const decoder = (value: string) => { // const decoder = (value: string) => {
// This function does not parse dates // This function does not parse dates
@ -31,12 +34,16 @@ const stateToUrlParams = {
export const setQueryStringWithoutPageReload = (qsValue: string): void => { export const setQueryStringWithoutPageReload = (qsValue: string): void => {
const w = window; const w = window;
if (w) { if (w) {
const newurl = `${w.location.protocol}//${w.location.host}${w.location.pathname}?${qsValue}`; const newurl = `${w.location.protocol}//${w.location.host}${w.location.pathname}${qsValue ? "?" : ""}${qsValue}`;
w.history.pushState({ path: newurl }, "", newurl); w.history.pushState({ path: newurl }, "", newurl);
} }
}; };
export const setQueryStringValue = (newValue: Record<string, unknown>): void => { export const setQueryStringValue = (newValue: Record<string, unknown>): void => {
if (appModeEnable) {
setQueryStringWithoutPageReload("");
return;
}
const queryMap = new Map(Object.entries(stateToUrlParams)); const queryMap = new Map(Object.entries(stateToUrlParams));
const query = get(newValue, "query", "") as string[]; const query = get(newValue, "query", "") as string[];
const newQsValue: string[] = []; const newQsValue: string[] = [];