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,
|
|
|
|
|
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';
|
|
|
|
|
import { AlertDef } from 'types/api/alerts/def';
|
2025-09-09 14:56:29 +07:00
|
|
|
|
|
|
|
|
import { INITIAL_ALERT_STATE } 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-10 21:43:54 +07:00
|
|
|
alertCreationReducer,
|
|
|
|
|
buildInitialAlertDef,
|
|
|
|
|
getInitialAlertTypeFromURL,
|
|
|
|
|
} 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 [alertDef] = useState<AlertDef>(buildInitialAlertDef(alertType));
|
|
|
|
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const contextValue: ICreateAlertContextProps = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
alertState,
|
|
|
|
|
setAlertState,
|
2025-09-10 21:43:54 +07:00
|
|
|
alertType,
|
|
|
|
|
setAlertType: handleAlertTypeChange,
|
|
|
|
|
alertDef,
|
2025-09-09 14:56:29 +07:00
|
|
|
}),
|
2025-09-10 21:43:54 +07:00
|
|
|
[alertState, alertType, handleAlertTypeChange, alertDef],
|
2025-09-09 14:56:29 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CreateAlertContext.Provider value={contextValue}>
|
|
|
|
|
{children}
|
|
|
|
|
</CreateAlertContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|