2025-09-10 21:43:54 +07:00
|
|
|
import { QueryParams } from 'constants/query';
|
|
|
|
|
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
|
|
|
|
|
import { mapQueryDataFromApi } from 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi';
|
2025-09-09 14:56:29 +07:00
|
|
|
import {
|
|
|
|
|
createContext,
|
2025-09-10 21:43:54 +07:00
|
|
|
useCallback,
|
2025-09-09 14:56:29 +07:00
|
|
|
useContext,
|
2025-09-15 22:52:07 +07:00
|
|
|
useEffect,
|
2025-09-09 14:56:29 +07:00
|
|
|
useMemo,
|
|
|
|
|
useReducer,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react';
|
2025-09-10 21:43:54 +07:00
|
|
|
import { useLocation } from 'react-router-dom';
|
|
|
|
|
import { AlertTypes } from 'types/api/alerts/alertTypes';
|
2025-09-09 14:56:29 +07:00
|
|
|
|
2025-09-15 22:52:07 +07:00
|
|
|
import {
|
2025-09-19 13:18:53 +07:00
|
|
|
INITIAL_ADVANCED_OPTIONS_STATE,
|
2025-09-15 22:52:07 +07:00
|
|
|
INITIAL_ALERT_STATE,
|
|
|
|
|
INITIAL_ALERT_THRESHOLD_STATE,
|
2025-09-19 13:18:53 +07:00
|
|
|
INITIAL_EVALUATION_WINDOW_STATE,
|
2025-09-24 10:22:05 +07:00
|
|
|
INITIAL_NOTIFICATION_SETTINGS_STATE,
|
2025-09-15 22:52:07 +07:00
|
|
|
} from './constants';
|
2025-09-10 21:43:54 +07:00
|
|
|
import { ICreateAlertContextProps, ICreateAlertProviderProps } from './types';
|
2025-09-09 14:56:29 +07:00
|
|
|
import {
|
2025-09-19 13:18:53 +07:00
|
|
|
advancedOptionsReducer,
|
2025-09-10 21:43:54 +07:00
|
|
|
alertCreationReducer,
|
2025-09-15 22:52:07 +07:00
|
|
|
alertThresholdReducer,
|
2025-09-10 21:43:54 +07:00
|
|
|
buildInitialAlertDef,
|
2025-09-19 13:18:53 +07:00
|
|
|
evaluationWindowReducer,
|
2025-09-10 21:43:54 +07:00
|
|
|
getInitialAlertTypeFromURL,
|
2025-09-24 10:22:05 +07:00
|
|
|
notificationSettingsReducer,
|
2025-09-10 21:43:54 +07:00
|
|
|
} from './utils';
|
2025-09-09 14:56:29 +07:00
|
|
|
|
|
|
|
|
const CreateAlertContext = createContext<ICreateAlertContextProps | null>(null);
|
|
|
|
|
|
|
|
|
|
// Hook exposing context state for CreateAlert
|
|
|
|
|
export const useCreateAlertState = (): ICreateAlertContextProps => {
|
|
|
|
|
const context = useContext(CreateAlertContext);
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'useCreateAlertState must be used within CreateAlertProvider',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function CreateAlertProvider(
|
|
|
|
|
props: ICreateAlertProviderProps,
|
|
|
|
|
): JSX.Element {
|
|
|
|
|
const { children } = props;
|
|
|
|
|
|
|
|
|
|
const [alertState, setAlertState] = useReducer(
|
|
|
|
|
alertCreationReducer,
|
|
|
|
|
INITIAL_ALERT_STATE,
|
|
|
|
|
);
|
2025-09-10 21:43:54 +07:00
|
|
|
|
|
|
|
|
const { currentQuery, redirectWithQueryBuilderData } = useQueryBuilder();
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
const queryParams = new URLSearchParams(location.search);
|
|
|
|
|
|
|
|
|
|
const [alertType, setAlertType] = useState<AlertTypes>(() =>
|
|
|
|
|
getInitialAlertTypeFromURL(queryParams, currentQuery),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleAlertTypeChange = useCallback(
|
|
|
|
|
(value: AlertTypes): void => {
|
|
|
|
|
const queryToRedirect = buildInitialAlertDef(value);
|
|
|
|
|
const currentQueryToRedirect = mapQueryDataFromApi(
|
|
|
|
|
queryToRedirect.condition.compositeQuery,
|
|
|
|
|
);
|
|
|
|
|
redirectWithQueryBuilderData(
|
|
|
|
|
currentQueryToRedirect,
|
|
|
|
|
{
|
|
|
|
|
[QueryParams.alertType]: value,
|
|
|
|
|
},
|
|
|
|
|
undefined,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
setAlertType(value);
|
|
|
|
|
},
|
|
|
|
|
[redirectWithQueryBuilderData],
|
2025-09-09 14:56:29 +07:00
|
|
|
);
|
|
|
|
|
|
2025-09-15 22:52:07 +07:00
|
|
|
const [thresholdState, setThresholdState] = useReducer(
|
|
|
|
|
alertThresholdReducer,
|
|
|
|
|
INITIAL_ALERT_THRESHOLD_STATE,
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-19 13:18:53 +07:00
|
|
|
const [evaluationWindow, setEvaluationWindow] = useReducer(
|
|
|
|
|
evaluationWindowReducer,
|
|
|
|
|
INITIAL_EVALUATION_WINDOW_STATE,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [advancedOptions, setAdvancedOptions] = useReducer(
|
|
|
|
|
advancedOptionsReducer,
|
|
|
|
|
INITIAL_ADVANCED_OPTIONS_STATE,
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-24 10:22:05 +07:00
|
|
|
const [notificationSettings, setNotificationSettings] = useReducer(
|
|
|
|
|
notificationSettingsReducer,
|
|
|
|
|
INITIAL_NOTIFICATION_SETTINGS_STATE,
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-15 22:52:07 +07:00
|
|
|
useEffect(() => {
|
|
|
|
|
setThresholdState({
|
|
|
|
|
type: 'RESET',
|
|
|
|
|
});
|
|
|
|
|
}, [alertType]);
|
|
|
|
|
|
2025-09-09 14:56:29 +07:00
|
|
|
const contextValue: ICreateAlertContextProps = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
alertState,
|
|
|
|
|
setAlertState,
|
2025-09-10 21:43:54 +07:00
|
|
|
alertType,
|
|
|
|
|
setAlertType: handleAlertTypeChange,
|
2025-09-15 22:52:07 +07:00
|
|
|
thresholdState,
|
|
|
|
|
setThresholdState,
|
2025-09-19 13:18:53 +07:00
|
|
|
evaluationWindow,
|
|
|
|
|
setEvaluationWindow,
|
|
|
|
|
advancedOptions,
|
|
|
|
|
setAdvancedOptions,
|
2025-09-24 10:22:05 +07:00
|
|
|
notificationSettings,
|
|
|
|
|
setNotificationSettings,
|
2025-09-09 14:56:29 +07:00
|
|
|
}),
|
2025-09-19 13:18:53 +07:00
|
|
|
[
|
|
|
|
|
alertState,
|
|
|
|
|
alertType,
|
|
|
|
|
handleAlertTypeChange,
|
|
|
|
|
thresholdState,
|
|
|
|
|
evaluationWindow,
|
|
|
|
|
advancedOptions,
|
2025-09-24 10:22:05 +07:00
|
|
|
notificationSettings,
|
2025-09-19 13:18:53 +07:00
|
|
|
],
|
2025-09-09 14:56:29 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CreateAlertContext.Provider value={contextValue}>
|
|
|
|
|
{children}
|
|
|
|
|
</CreateAlertContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|