2023-05-18 12:36:02 +05:30
|
|
|
import { Popover } from 'antd';
|
|
|
|
|
import ROUTES from 'constants/routes';
|
|
|
|
|
import history from 'lib/history';
|
2022-08-11 11:45:28 +05:30
|
|
|
import { generateFilterQuery } from 'lib/logs/generateFilterQuery';
|
|
|
|
|
import React, { memo, useCallback, useMemo } from 'react';
|
2023-05-18 12:36:02 +05:30
|
|
|
import { useSelector } from 'react-redux';
|
2022-08-11 11:45:28 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
2022-08-19 17:16:04 +05:30
|
|
|
import { ILogsReducer } from 'types/reducer/logs';
|
2022-08-11 11:45:28 +05:30
|
|
|
|
2023-05-18 12:36:02 +05:30
|
|
|
import { ButtonContainer } from './styles';
|
|
|
|
|
|
2022-08-11 11:45:28 +05:30
|
|
|
function AddToQueryHOC({
|
|
|
|
|
fieldKey,
|
|
|
|
|
fieldValue,
|
|
|
|
|
children,
|
2022-08-19 17:16:04 +05:30
|
|
|
}: AddToQueryHOCProps): JSX.Element {
|
2022-08-11 11:45:28 +05:30
|
|
|
const {
|
|
|
|
|
searchFilter: { queryString },
|
|
|
|
|
} = useSelector<AppState, ILogsReducer>((store) => store.logs);
|
|
|
|
|
|
|
|
|
|
const generatedQuery = useMemo(
|
|
|
|
|
() => generateFilterQuery({ fieldKey, fieldValue, type: 'IN' }),
|
|
|
|
|
[fieldKey, fieldValue],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleQueryAdd = useCallback(() => {
|
|
|
|
|
let updatedQueryString = queryString || '';
|
|
|
|
|
|
|
|
|
|
if (updatedQueryString.length === 0) {
|
|
|
|
|
updatedQueryString += `${generatedQuery}`;
|
|
|
|
|
} else {
|
|
|
|
|
updatedQueryString += ` AND ${generatedQuery}`;
|
|
|
|
|
}
|
2023-05-18 12:36:02 +05:30
|
|
|
|
|
|
|
|
history.replace(`${ROUTES.LOGS}?q=${updatedQueryString}`);
|
|
|
|
|
}, [generatedQuery, queryString]);
|
2022-12-26 16:20:34 +05:30
|
|
|
|
|
|
|
|
const popOverContent = useMemo(() => <span>Add to query: {fieldKey}</span>, [
|
|
|
|
|
fieldKey,
|
2022-08-11 11:45:28 +05:30
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return (
|
2023-05-18 12:36:02 +05:30
|
|
|
<ButtonContainer size="small" type="text" onClick={handleQueryAdd}>
|
2022-08-11 11:45:28 +05:30
|
|
|
<Popover placement="top" content={popOverContent}>
|
|
|
|
|
{children}
|
|
|
|
|
</Popover>
|
2023-05-18 12:36:02 +05:30
|
|
|
</ButtonContainer>
|
2022-08-11 11:45:28 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 16:20:34 +05:30
|
|
|
interface AddToQueryHOCProps {
|
|
|
|
|
fieldKey: string;
|
|
|
|
|
fieldValue: string;
|
|
|
|
|
children: React.ReactNode;
|
2022-08-11 11:45:28 +05:30
|
|
|
}
|
|
|
|
|
|
2022-12-26 16:20:34 +05:30
|
|
|
export default memo(AddToQueryHOC);
|