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

[Security Solution] Add VersionPicker component #188302

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import * as i18n from './translations';

export enum SelectedVersions {
BaseTarget = 'base_target',
BaseCurrent = 'base_current',
BaseFinal = 'base_final',
CurrentTarget = 'current_target',
CurrentFinal = 'current_final',
TargetFinal = 'target_final',
}

export const BASE_OPTIONS = [
nikitaindik marked this conversation as resolved.
Show resolved Hide resolved
{
value: SelectedVersions.BaseTarget,
text: i18n.BASE_VS_TARGET,
},
{
value: SelectedVersions.BaseCurrent,
text: i18n.BASE_VS_CURRENT,
},
{
value: SelectedVersions.BaseFinal,
text: i18n.BASE_VS_FINAL,
},
];

export const CURRENT_OPTIONS = [
{
value: SelectedVersions.CurrentTarget,
text: i18n.CURRENT_VS_TARGET,
},
{
value: SelectedVersions.CurrentFinal,
text: i18n.CURRENT_VS_FINAL,
},
];

export const TARGET_OPTIONS = [
{
value: SelectedVersions.TargetFinal,
text: i18n.TARGET_VS_FINAL,
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';

export const BASE_VS_TARGET = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.baseVsTargetLabel',
{
defaultMessage: 'Base vs Target',
}
);

export const BASE_VS_CURRENT = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.baseVsCurrentLabel',
{
defaultMessage: 'Base vs Current',
}
);

export const BASE_VS_FINAL = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.baseVsFinalLabel',
{
defaultMessage: 'Base vs Final',
}
);

export const CURRENT_VS_TARGET = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.currentVsTargetLabel',
{
defaultMessage: 'Current vs Target',
}
);

export const CURRENT_VS_FINAL = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.currentVsFinalLabel',
{
defaultMessage: 'Current vs Final',
}
);

export const TARGET_VS_FINAL = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.targetVsFinalLabel',
{
defaultMessage: 'Target vs Final',
}
);

export const VERSION_PICKER_ARIA_LABEL = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.upgradeRules.versionsPicker.ariaLabel',
{
defaultMessage: 'Select versions to compare',
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { useState } from 'react';
import type { Story } from '@storybook/react';
import { VersionsPicker } from './versions_picker';
import { SelectedVersions } from './constants';

export default {
component: VersionsPicker,
title: 'ThreeWayDiff/VersionsPicker',
banderror marked this conversation as resolved.
Show resolved Hide resolved
argTypes: {
hasBaseVersion: {
control: 'boolean',
description: 'Indicates whether the base version of a field is available',
defaultValue: true,
},
},
};

const Template: Story<{ hasBaseVersion: boolean }> = (args) => {
const [selectedVersions, setSelectedVersions] = useState<SelectedVersions>(
SelectedVersions.BaseTarget
);

return (
<VersionsPicker
hasBaseVersion={args.hasBaseVersion}
selectedVersions={selectedVersions}
onChange={setSelectedVersions}
/>
);
};

export const Default = Template.bind({});
Default.args = {
hasBaseVersion: true,
};

export const NoBaseVersion = Template.bind({});
NoBaseVersion.args = {
hasBaseVersion: false,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { useCallback, useMemo } from 'react';
import { EuiSelect } from '@elastic/eui';
import type { EuiSelectOption } from '@elastic/eui';
import { BASE_OPTIONS, CURRENT_OPTIONS, TARGET_OPTIONS } from './constants';
import type { SelectedVersions } from './constants';
import * as i18n from './translations';

interface VersionsPickerProps {
hasBaseVersion: boolean;
selectedVersions: SelectedVersions;
onChange: (pickedVersions: SelectedVersions) => void;
}

export function VersionsPicker({
hasBaseVersion,
selectedVersions,
onChange,
}: VersionsPickerProps) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we agree on a canonical way of defining components? The previous one was defined as an FC<...>:

export const CustomizedPrebuiltRuleBadge: React.FC<CustomizedPrebuiltRuleBadgeProps> = ({

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a strong preference. One very minor downside of using React.FC is that it adds children prop to type even for childless components like this one. In React v18 children are removed. We are on v17 now.

function-style components are a slightly more readable and can be ordered in any way, so they are a bit more flexible.

In the future we could probably add an ESLint rule to enforce for canonical way if we decide what the way is. But I am fine with either approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

@nikitaindik I'm fine with either too. Could be a bikeshedding topic for Tech Time when we won't have an agenda :)

const options: EuiSelectOption[] = useMemo(
() => [...(hasBaseVersion ? BASE_OPTIONS : []), ...CURRENT_OPTIONS, ...TARGET_OPTIONS],
[hasBaseVersion]
);
banderror marked this conversation as resolved.
Show resolved Hide resolved

const handleChange = useCallback(
(changeEvent) => {
onChange(changeEvent.target.value as SelectedVersions);
},
[onChange]
);

return (
<EuiSelect
options={options}
value={selectedVersions}
onChange={handleChange}
aria-label={i18n.VERSION_PICKER_ARIA_LABEL}
/>
);
}