Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest Manager] Convert select agent config step to use combo box #73172

Merged
merged 10 commits into from
Jul 28, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { useEffect, useState, Fragment } from 'react';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import {
EuiFlexGroup,
EuiFlexItem,
EuiSelectable,
EuiSpacer,
EuiComboBox,
EuiComboBoxOptionOption,
EuiTextColor,
EuiPortal,
EuiButtonEmpty,
EuiFormRow,
EuiLink,
} from '@elastic/eui';
import { Error } from '../../../components';
import { AgentConfig, PackageInfo, GetAgentConfigsResponseItem } from '../../../types';
Expand All @@ -23,16 +25,35 @@ import {
useGetAgentConfigs,
sendGetOneAgentConfig,
useCapabilities,
useFleetStatus,
} from '../../../hooks';
import { CreateAgentConfigFlyout } from '../list_page/components';

const AgentConfigWrapper = styled(EuiFormRow)`
.euiFormRow__label {
width: 100%;
}
`;

const AgentConfigNameColumn = styled(EuiFlexItem)`
max-width: ${(props) => `${((props.grow as number) / 9) * 100}%`};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this additional calculation needed? Would it make sense to add a comment about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. the max-width property is added to prevent long config names/descriptions from overflowing the flex items
  2. we build it from the grow property on the flex items because that property changes based on if Fleet is enabled/setup or not (when Fleet is not available, the "agents enrolled" column is not displayed)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed up a change with comments added

overflow: hidden;
`;

const AgentConfigDescriptionColumn = styled(EuiFlexItem)`
max-width: ${(props) => `${((props.grow as number) / 9) * 100}%`};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

overflow: hidden;
`;

export const StepSelectConfig: React.FunctionComponent<{
pkgkey: string;
updatePackageInfo: (packageInfo: PackageInfo | undefined) => void;
agentConfig: AgentConfig | undefined;
updateAgentConfig: (config: AgentConfig | undefined) => void;
setIsLoadingSecondStep: (isLoading: boolean) => void;
}> = ({ pkgkey, updatePackageInfo, agentConfig, updateAgentConfig, setIsLoadingSecondStep }) => {
const { isReady: isFleetReady } = useFleetStatus();

// Selected config state
const [selectedConfigId, setSelectedConfigId] = useState<string | undefined>(
agentConfig ? agentConfig.id : undefined
Expand Down Expand Up @@ -106,6 +127,40 @@ export const StepSelectConfig: React.FunctionComponent<{
}
}, [selectedConfigId, agentConfig, updateAgentConfig, setIsLoadingSecondStep]);

const agentConfigOptions: Array<EuiComboBoxOptionOption<string>> = packageInfoData
? agentConfigs.map((agentConf) => {
const alreadyHasLimitedPackage =
(isLimitedPackage &&
doesAgentConfigAlreadyIncludePackage(agentConf, packageInfoData.response.name)) ||
false;
return {
label: agentConf.name,
value: agentConf.id,
disabled: alreadyHasLimitedPackage,
'data-test-subj': 'agentConfigItem',
};
})
: [];

const selectedConfigOption = agentConfigOptions.find(
(option) => option.value === selectedConfigId
);

// Try to select default agent config
useEffect(() => {
if (!selectedConfigId && agentConfigs.length && agentConfigOptions.length) {
const defaultAgentConfig = agentConfigs.find((config) => config.is_default);
if (defaultAgentConfig) {
const defaultAgentConfigOption = agentConfigOptions.find(
(option) => option.value === defaultAgentConfig.id
);
if (defaultAgentConfigOption && !defaultAgentConfigOption.disabled) {
setSelectedConfigId(defaultAgentConfig.id);
}
}
}
}, [agentConfigs, agentConfigOptions, selectedConfigId]);

// Display package error if there is one
if (packageInfoError) {
return (
Expand Down Expand Up @@ -154,77 +209,95 @@ export const StepSelectConfig: React.FunctionComponent<{
) : null}
<EuiFlexGroup direction="column" gutterSize="m">
<EuiFlexItem>
<EuiSelectable
searchable
allowExclusions={false}
singleSelection={true}
isLoading={isAgentConfigsLoading || isPackageInfoLoading}
options={agentConfigs.map((agentConf) => {
const alreadyHasLimitedPackage =
(isLimitedPackage &&
packageInfoData &&
doesAgentConfigAlreadyIncludePackage(agentConf, packageInfoData.response.name)) ||
false;
return {
label: agentConf.name,
key: agentConf.id,
checked: selectedConfigId === agentConf.id ? 'on' : undefined,
disabled: alreadyHasLimitedPackage,
'data-test-subj': 'agentConfigItem',
};
})}
renderOption={(option) => (
<EuiFlexGroup>
<EuiFlexItem grow={false}>{option.label}</EuiFlexItem>
<AgentConfigWrapper
fullWidth={true}
label={
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem>
<EuiTextColor color="subdued">
{agentConfigsById[option.key!].description}
</EuiTextColor>
<FormattedMessage
id="xpack.ingestManager.createPackageConfig.StepSelectConfig.agentConfigLabel"
defaultMessage="Agent configuration"
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiTextColor color="subdued">
<FormattedMessage
id="xpack.ingestManager.createPackageConfig.StepSelectConfig.agentConfigAgentsCountText"
defaultMessage="{count, plural, one {# agent} other {# agents}}"
values={{
count: agentConfigsById[option.key!].agents || 0,
}}
/>
</EuiTextColor>
<div>
<EuiLink
disabled={!hasWriteCapabilites}
onClick={() => setIsCreateAgentConfigFlyoutOpen(true)}
>
<FormattedMessage
id="xpack.ingestManager.createPackageConfig.StepSelectConfig.addButton"
defaultMessage="Create agent configuration"
/>
</EuiLink>
</div>
</EuiFlexItem>
</EuiFlexGroup>
)}
listProps={{
bordered: true,
}}
searchProps={{
placeholder: i18n.translate(
'xpack.ingestManager.createPackageConfig.StepSelectConfig.filterAgentConfigsInputPlaceholder',
}
helpText={
isFleetReady && selectedConfigId ? (
<FormattedMessage
id="xpack.ingestManager.createPackageConfig.StepSelectConfig.agentConfigAgentsDescriptionText"
defaultMessage="{count, plural, one {# agent} other {# agents}} are enrolled with the selected agent configuration."
values={{
count: agentConfigsById[selectedConfigId].agents || 0,
}}
/>
) : null
}
>
<EuiComboBox
placeholder={i18n.translate(
'xpack.ingestManager.createPackageConfig.StepSelectConfig.agentConfigPlaceholderText',
{
defaultMessage: 'Search for agent configurations',
defaultMessage: 'Select an agent configuration to add this integration to',
}
),
}}
height={180}
onChange={(options) => {
const selectedOption = options.find((option) => option.checked === 'on');
if (selectedOption) {
if (selectedOption.key !== selectedConfigId) {
setSelectedConfigId(selectedOption.key);
)}
singleSelection={{ asPlainText: true }}
isClearable={false}
fullWidth={true}
isLoading={isAgentConfigsLoading || isPackageInfoLoading}
options={agentConfigOptions}
renderOption={(option: EuiComboBoxOptionOption<string>) => {
return (
<EuiFlexGroup>
<AgentConfigNameColumn grow={2}>
<span className="eui-textTruncate">{option.label}</span>
</AgentConfigNameColumn>
<AgentConfigDescriptionColumn grow={isFleetReady ? 5 : 7}>
<EuiTextColor className="eui-textTruncate" color="subdued">
{agentConfigsById[option.value!].description}
</EuiTextColor>
</AgentConfigDescriptionColumn>
{isFleetReady ? (
<EuiFlexItem grow={2} className="eui-textRight">
<EuiTextColor color="subdued">
<FormattedMessage
id="xpack.ingestManager.createPackageConfig.StepSelectConfig.agentConfigAgentsCountText"
defaultMessage="{count, plural, one {# agent} other {# agents}} enrolled"
values={{
count: agentConfigsById[option.value!].agents || 0,
}}
/>
</EuiTextColor>
</EuiFlexItem>
) : null}
</EuiFlexGroup>
);
}}
selectedOptions={selectedConfigOption ? [selectedConfigOption] : []}
onChange={(options) => {
const selectedOption = options[0] || undefined;
if (selectedOption) {
if (selectedOption.value !== selectedConfigId) {
setSelectedConfigId(selectedOption.value);
}
} else {
setSelectedConfigId(undefined);
}
} else {
setSelectedConfigId(undefined);
}
}}
>
{(list, search) => (
<Fragment>
{search}
<EuiSpacer size="m" />
{list}
</Fragment>
)}
</EuiSelectable>
}}
/>
</AgentConfigWrapper>
</EuiFlexItem>
{/* Display selected agent config error if there is one */}
{selectedConfigError ? (
Expand All @@ -240,22 +313,6 @@ export const StepSelectConfig: React.FunctionComponent<{
/>
</EuiFlexItem>
) : null}
<EuiFlexItem>
<div>
<EuiButtonEmpty
iconType="plusInCircle"
isDisabled={!hasWriteCapabilites}
onClick={() => setIsCreateAgentConfigFlyoutOpen(true)}
flush="left"
size="s"
>
<FormattedMessage
id="xpack.ingestManager.createPackageConfig.StepSelectConfig.addButton"
defaultMessage="New agent configuration"
/>
</EuiButtonEmpty>
</div>
</EuiFlexItem>
</EuiFlexGroup>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const CreateAgentConfigFlyout: React.FunctionComponent<Props> = ({
);

return (
<FlyoutWithHigherZIndex onClose={onClose} size="l" maxWidth={400} {...restOfProps}>
<FlyoutWithHigherZIndex onClose={() => onClose()} size="l" maxWidth={400} {...restOfProps}>
{header}
{body}
{footer}
Expand Down