2023-11-30 13:56:49 +05:30
|
|
|
import './DashboardVariableSelection.styles.scss';
|
|
|
|
|
|
2022-09-09 17:43:25 +05:30
|
|
|
import { orange } from '@ant-design/colors';
|
|
|
|
|
import { WarningOutlined } from '@ant-design/icons';
|
2023-12-15 13:10:02 +05:30
|
|
|
import { Input, Popover, Select, Tooltip, Typography } from 'antd';
|
2023-11-30 13:56:49 +05:30
|
|
|
import dashboardVariablesQuery from 'api/dashboard/variables/dashboardVariablesQuery';
|
|
|
|
|
import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
|
2022-09-09 17:43:25 +05:30
|
|
|
import { commaValuesParser } from 'lib/dashbaordVariables/customCommaValuesParser';
|
|
|
|
|
import sortValues from 'lib/dashbaordVariables/sortVariableValues';
|
2024-01-25 23:12:19 +05:30
|
|
|
import { debounce } from 'lodash-es';
|
2023-05-02 18:51:24 +05:30
|
|
|
import map from 'lodash-es/map';
|
2023-12-15 13:10:02 +05:30
|
|
|
import { useDashboard } from 'providers/Dashboard/Dashboard';
|
2023-11-30 13:56:49 +05:30
|
|
|
import { memo, useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import { useQuery } from 'react-query';
|
2022-09-09 17:43:25 +05:30
|
|
|
import { IDashboardVariable } from 'types/api/dashboard/getAll';
|
2023-11-30 13:56:49 +05:30
|
|
|
import { VariableResponseProps } from 'types/api/dashboard/variables/query';
|
2024-01-27 12:59:28 +05:30
|
|
|
import { popupContainer } from 'utils/selectPopupContainer';
|
2022-09-09 17:43:25 +05:30
|
|
|
|
2023-01-25 13:22:57 +05:30
|
|
|
import { variablePropsToPayloadVariables } from '../utils';
|
2023-11-30 13:56:49 +05:30
|
|
|
import { SelectItemStyle, VariableContainer, VariableValue } from './styles';
|
2023-01-25 13:22:57 +05:30
|
|
|
import { areArraysEqual } from './util';
|
2022-09-09 17:43:25 +05:30
|
|
|
|
|
|
|
|
const ALL_SELECT_VALUE = '__ALL__';
|
|
|
|
|
|
2023-11-30 13:56:49 +05:30
|
|
|
const variableRegexPattern = /\{\{\s*?\.([^\s}]+)\s*?\}\}/g;
|
|
|
|
|
|
2022-09-09 17:43:25 +05:30
|
|
|
interface VariableItemProps {
|
|
|
|
|
variableData: IDashboardVariable;
|
2023-01-25 13:22:57 +05:30
|
|
|
existingVariables: Record<string, IDashboardVariable>;
|
|
|
|
|
onValueUpdate: (
|
2023-05-02 18:51:24 +05:30
|
|
|
name: string,
|
2023-12-15 13:10:02 +05:30
|
|
|
id: string,
|
2023-05-02 18:51:24 +05:30
|
|
|
arg1: IDashboardVariable['selectedValue'],
|
2023-11-30 13:56:49 +05:30
|
|
|
allSelected: boolean,
|
2023-01-25 13:22:57 +05:30
|
|
|
) => void;
|
|
|
|
|
lastUpdatedVar: string;
|
2022-09-09 17:43:25 +05:30
|
|
|
}
|
2023-08-02 12:09:49 +02:00
|
|
|
|
|
|
|
|
const getSelectValue = (
|
|
|
|
|
selectedValue: IDashboardVariable['selectedValue'],
|
|
|
|
|
): string | string[] => {
|
|
|
|
|
if (Array.isArray(selectedValue)) {
|
|
|
|
|
return selectedValue.map((item) => item.toString());
|
|
|
|
|
}
|
|
|
|
|
return selectedValue?.toString() || '';
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-27 12:59:28 +05:30
|
|
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
2022-09-09 17:43:25 +05:30
|
|
|
function VariableItem({
|
|
|
|
|
variableData,
|
2023-01-25 13:22:57 +05:30
|
|
|
existingVariables,
|
2022-09-09 17:43:25 +05:30
|
|
|
onValueUpdate,
|
2023-01-25 13:22:57 +05:30
|
|
|
lastUpdatedVar,
|
2022-09-09 17:43:25 +05:30
|
|
|
}: VariableItemProps): JSX.Element {
|
2023-12-15 13:10:02 +05:30
|
|
|
const { isDashboardLocked } = useDashboard();
|
2023-01-25 13:22:57 +05:30
|
|
|
const [optionsData, setOptionsData] = useState<(string | number | boolean)[]>(
|
|
|
|
|
[],
|
|
|
|
|
);
|
2023-11-30 13:56:49 +05:30
|
|
|
|
2022-09-09 17:43:25 +05:30
|
|
|
const [errorMessage, setErrorMessage] = useState<null | string>(null);
|
2023-01-25 13:22:57 +05:30
|
|
|
|
2023-11-30 13:56:49 +05:30
|
|
|
const getDependentVariables = (queryValue: string): string[] => {
|
|
|
|
|
const matches = queryValue.match(variableRegexPattern);
|
|
|
|
|
|
|
|
|
|
// Extract variable names from the matches array without {{ . }}
|
|
|
|
|
return matches
|
|
|
|
|
? matches.map((match) => match.replace(variableRegexPattern, '$1'))
|
|
|
|
|
: [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getQueryKey = (variableData: IDashboardVariable): string[] => {
|
|
|
|
|
let dependentVariablesStr = '';
|
|
|
|
|
|
|
|
|
|
const dependentVariables = getDependentVariables(
|
|
|
|
|
variableData.queryValue || '',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const variableName = variableData.name || '';
|
|
|
|
|
|
|
|
|
|
dependentVariables?.forEach((element) => {
|
2024-01-27 12:59:28 +05:30
|
|
|
const [, variable] =
|
|
|
|
|
Object.entries(existingVariables).find(
|
|
|
|
|
([, value]) => value.name === element,
|
|
|
|
|
) || [];
|
|
|
|
|
|
|
|
|
|
dependentVariablesStr += `${element}${variable?.selectedValue}`;
|
2023-11-30 13:56:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const variableKey = dependentVariablesStr.replace(/\s/g, '');
|
|
|
|
|
|
|
|
|
|
return [REACT_QUERY_KEY.DASHBOARD_BY_ID, variableName, variableKey];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
|
|
|
const getOptions = (variablesRes: VariableResponseProps | null): void => {
|
|
|
|
|
if (variablesRes && variableData.type === 'QUERY') {
|
2022-09-09 17:43:25 +05:30
|
|
|
try {
|
|
|
|
|
setErrorMessage(null);
|
2023-11-30 13:56:49 +05:30
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
variablesRes?.variableValues &&
|
|
|
|
|
Array.isArray(variablesRes?.variableValues)
|
|
|
|
|
) {
|
2023-01-25 13:22:57 +05:30
|
|
|
const newOptionsData = sortValues(
|
2023-11-30 13:56:49 +05:30
|
|
|
variablesRes?.variableValues,
|
2023-01-25 13:22:57 +05:30
|
|
|
variableData.sort,
|
2022-09-09 17:43:25 +05:30
|
|
|
);
|
2023-11-30 13:56:49 +05:30
|
|
|
|
2023-01-25 13:22:57 +05:30
|
|
|
const oldOptionsData = sortValues(optionsData, variableData.sort) as never;
|
2023-11-30 13:56:49 +05:30
|
|
|
|
2023-01-25 13:22:57 +05:30
|
|
|
if (!areArraysEqual(newOptionsData, oldOptionsData)) {
|
|
|
|
|
/* eslint-disable no-useless-escape */
|
|
|
|
|
const re = new RegExp(`\\{\\{\\s*?\\.${lastUpdatedVar}\\s*?\\}\\}`); // regex for `{{.var}}`
|
|
|
|
|
// If the variable is dependent on the last updated variable
|
|
|
|
|
// and contains the last updated variable in its query (of the form `{{.var}}`)
|
|
|
|
|
// then we need to update the value of the variable
|
|
|
|
|
const queryValue = variableData.queryValue || '';
|
|
|
|
|
const dependVarReMatch = queryValue.match(re);
|
|
|
|
|
if (
|
|
|
|
|
variableData.type === 'QUERY' &&
|
|
|
|
|
dependVarReMatch !== null &&
|
|
|
|
|
dependVarReMatch.length > 0
|
|
|
|
|
) {
|
|
|
|
|
let value = variableData.selectedValue;
|
|
|
|
|
let allSelected = false;
|
|
|
|
|
// The default value for multi-select is ALL and first value for
|
|
|
|
|
// single select
|
|
|
|
|
if (variableData.multiSelect) {
|
|
|
|
|
value = newOptionsData;
|
|
|
|
|
allSelected = true;
|
|
|
|
|
} else {
|
|
|
|
|
[value] = newOptionsData;
|
|
|
|
|
}
|
2023-12-15 13:10:02 +05:30
|
|
|
|
|
|
|
|
if (variableData && variableData?.name && variableData?.id) {
|
|
|
|
|
onValueUpdate(variableData.name, variableData.id, value, allSelected);
|
2023-05-02 18:51:24 +05:30
|
|
|
}
|
2023-01-25 13:22:57 +05:30
|
|
|
}
|
2023-11-30 13:56:49 +05:30
|
|
|
|
2023-01-25 13:22:57 +05:30
|
|
|
setOptionsData(newOptionsData);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-09 17:43:25 +05:30
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
} else if (variableData.type === 'CUSTOM') {
|
2023-12-15 13:10:02 +05:30
|
|
|
const optionsData = sortValues(
|
|
|
|
|
commaValuesParser(variableData.customValue || ''),
|
|
|
|
|
variableData.sort,
|
|
|
|
|
) as never;
|
|
|
|
|
|
|
|
|
|
setOptionsData(optionsData);
|
2022-09-09 17:43:25 +05:30
|
|
|
}
|
2023-11-30 13:56:49 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { isLoading } = useQuery(getQueryKey(variableData), {
|
|
|
|
|
enabled: variableData && variableData.type === 'QUERY',
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
dashboardVariablesQuery({
|
|
|
|
|
query: variableData.queryValue || '',
|
|
|
|
|
variables: variablePropsToPayloadVariables(existingVariables),
|
|
|
|
|
}),
|
|
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
|
onSuccess: (response) => {
|
|
|
|
|
getOptions(response.payload);
|
|
|
|
|
},
|
|
|
|
|
onError: (error: {
|
|
|
|
|
details: {
|
|
|
|
|
error: string;
|
|
|
|
|
};
|
|
|
|
|
}) => {
|
|
|
|
|
const { details } = error;
|
|
|
|
|
|
|
|
|
|
if (details.error) {
|
|
|
|
|
let message = details.error;
|
|
|
|
|
if (details.error.includes('Syntax error:')) {
|
|
|
|
|
message =
|
|
|
|
|
'Please make sure query is valid and dependent variables are selected';
|
|
|
|
|
}
|
|
|
|
|
setErrorMessage(message);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-09-09 17:43:25 +05:30
|
|
|
|
|
|
|
|
const handleChange = (value: string | string[]): void => {
|
2023-05-02 18:51:24 +05:30
|
|
|
if (variableData.name)
|
|
|
|
|
if (
|
|
|
|
|
value === ALL_SELECT_VALUE ||
|
|
|
|
|
(Array.isArray(value) && value.includes(ALL_SELECT_VALUE)) ||
|
|
|
|
|
(Array.isArray(value) && value.length === 0)
|
|
|
|
|
) {
|
2023-12-15 13:10:02 +05:30
|
|
|
onValueUpdate(variableData.name, variableData.id, optionsData, true);
|
2023-05-02 18:51:24 +05:30
|
|
|
} else {
|
2023-12-15 13:10:02 +05:30
|
|
|
onValueUpdate(variableData.name, variableData.id, value, false);
|
2023-05-02 18:51:24 +05:30
|
|
|
}
|
2022-09-09 17:43:25 +05:30
|
|
|
};
|
2023-01-25 13:22:57 +05:30
|
|
|
|
2024-01-25 23:12:19 +05:30
|
|
|
// do not debounce the above function as we do not need debounce in select variables
|
|
|
|
|
const debouncedHandleChange = debounce(handleChange, 500);
|
|
|
|
|
|
2023-08-02 12:09:49 +02:00
|
|
|
const { selectedValue } = variableData;
|
|
|
|
|
const selectedValueStringified = useMemo(() => getSelectValue(selectedValue), [
|
|
|
|
|
selectedValue,
|
|
|
|
|
]);
|
|
|
|
|
|
2023-01-25 13:22:57 +05:30
|
|
|
const selectValue = variableData.allSelected
|
|
|
|
|
? 'ALL'
|
2023-08-02 12:09:49 +02:00
|
|
|
: selectedValueStringified;
|
|
|
|
|
|
2023-01-25 13:22:57 +05:30
|
|
|
const mode =
|
|
|
|
|
variableData.multiSelect && !variableData.allSelected
|
|
|
|
|
? 'multiple'
|
|
|
|
|
: undefined;
|
|
|
|
|
const enableSelectAll = variableData.multiSelect && variableData.showALLOption;
|
2023-11-30 13:56:49 +05:30
|
|
|
|
2023-12-05 16:09:50 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
// Fetch options for CUSTOM Type
|
|
|
|
|
if (variableData.type === 'CUSTOM') {
|
|
|
|
|
getOptions(null);
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-12-15 13:10:02 +05:30
|
|
|
}, [variableData.type, variableData.customValue]);
|
2023-12-05 16:09:50 +05:30
|
|
|
|
2022-09-09 17:43:25 +05:30
|
|
|
return (
|
2023-12-15 13:10:02 +05:30
|
|
|
<Tooltip
|
|
|
|
|
placement="top"
|
|
|
|
|
title={isDashboardLocked ? 'Dashboard is locked' : ''}
|
|
|
|
|
>
|
2024-01-27 12:59:28 +05:30
|
|
|
<VariableContainer className="variable-item">
|
2023-12-15 13:10:02 +05:30
|
|
|
<Typography.Text className="variable-name" ellipsis>
|
|
|
|
|
${variableData.name}
|
|
|
|
|
</Typography.Text>
|
|
|
|
|
<VariableValue>
|
|
|
|
|
{variableData.type === 'TEXTBOX' ? (
|
|
|
|
|
<Input
|
|
|
|
|
placeholder="Enter value"
|
|
|
|
|
disabled={isDashboardLocked}
|
2023-11-30 13:56:49 +05:30
|
|
|
bordered={false}
|
2024-01-27 12:59:28 +05:30
|
|
|
key={variableData.selectedValue?.toString()}
|
2024-01-25 23:12:19 +05:30
|
|
|
defaultValue={variableData.selectedValue?.toString()}
|
2023-12-15 13:10:02 +05:30
|
|
|
onChange={(e): void => {
|
2024-01-25 23:12:19 +05:30
|
|
|
debouncedHandleChange(e.target.value || '');
|
2023-12-15 13:10:02 +05:30
|
|
|
}}
|
|
|
|
|
style={{
|
|
|
|
|
width:
|
|
|
|
|
50 + ((variableData.selectedValue?.toString()?.length || 0) * 7 || 50),
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
!errorMessage &&
|
|
|
|
|
optionsData && (
|
|
|
|
|
<Select
|
2024-01-27 12:59:28 +05:30
|
|
|
key={
|
|
|
|
|
selectValue && Array.isArray(selectValue)
|
|
|
|
|
? selectValue.join(' ')
|
|
|
|
|
: selectValue || variableData.id
|
|
|
|
|
}
|
2024-01-25 23:12:19 +05:30
|
|
|
defaultValue={selectValue}
|
2023-12-15 13:10:02 +05:30
|
|
|
onChange={handleChange}
|
|
|
|
|
bordered={false}
|
|
|
|
|
placeholder="Select value"
|
2024-01-27 12:59:28 +05:30
|
|
|
placement="bottomRight"
|
2023-12-15 13:10:02 +05:30
|
|
|
mode={mode}
|
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
|
style={SelectItemStyle}
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
showSearch
|
|
|
|
|
data-testid="variable-select"
|
2024-01-27 12:59:28 +05:30
|
|
|
className="variable-select"
|
2023-12-15 13:10:02 +05:30
|
|
|
disabled={isDashboardLocked}
|
2024-01-27 12:59:28 +05:30
|
|
|
getPopupContainer={popupContainer}
|
2023-12-15 13:10:02 +05:30
|
|
|
>
|
|
|
|
|
{enableSelectAll && (
|
|
|
|
|
<Select.Option data-testid="option-ALL" value={ALL_SELECT_VALUE}>
|
|
|
|
|
ALL
|
|
|
|
|
</Select.Option>
|
|
|
|
|
)}
|
|
|
|
|
{map(optionsData, (option) => (
|
|
|
|
|
<Select.Option
|
|
|
|
|
data-testid={`option-${option}`}
|
|
|
|
|
key={option.toString()}
|
|
|
|
|
value={option}
|
|
|
|
|
>
|
|
|
|
|
{option.toString()}
|
|
|
|
|
</Select.Option>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
)
|
|
|
|
|
)}
|
|
|
|
|
{variableData.type !== 'TEXTBOX' && errorMessage && (
|
|
|
|
|
<span style={{ margin: '0 0.5rem' }}>
|
|
|
|
|
<Popover
|
|
|
|
|
placement="top"
|
|
|
|
|
content={<Typography>{errorMessage}</Typography>}
|
|
|
|
|
>
|
|
|
|
|
<WarningOutlined style={{ color: orange[5] }} />
|
|
|
|
|
</Popover>
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</VariableValue>
|
|
|
|
|
</VariableContainer>
|
|
|
|
|
</Tooltip>
|
2022-09-09 17:43:25 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 13:53:53 +05:30
|
|
|
export default memo(VariableItem);
|