Skip to content

Commit

Permalink
[8.x] [Security Solution] Fix incorrect default value for History Win…
Browse files Browse the repository at this point in the history
…dow Size (#207827) (#208178)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Security Solution] Fix incorrect default value for History Window
Size (#207827)](#207827)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nikita
Indik","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-24T11:49:42Z","message":"[Security
Solution] Fix incorrect default value for History Window Size
(#207827)\n\n**Resolves:
https://github.com/elastic/kibana/issues/207348**\n\n## Summary\nThis PR
resolves a bug in the editable History Window Size component,\nwhich
incorrectly displays \"0 seconds\" as the default value instead of\n\"7
days\". The issue was caused by passing an array of units `(['m',
'h',\n'd'])` using an incorrect prop name.\n\nAlso, I fixed stuck last
digit being stuck when trying to remove it\n**Before my changes: can't
remove or reset the last
digit**\n\n\nhttps://github.com/user-attachments/assets/3a7d28ea-ea71-4ee8-8805-902bae1dc3c1\n\n**After
my changes: value resets to minValue on last digit
removal**\n\n\nhttps://github.com/user-attachments/assets/1cba36bb-9127-4197-871e-62b978b3612c\n\n\nWork
started on:
22-Jan-2025","sha":"8894c1f395ce5d30c750db9d5b6c9be4fecac6f7","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Detections
and Resp","Team: SecuritySolution","Team:Detection Rule
Management","Feature:Rule Creation","Feature:Rule
Edit","backport:version","v8.18.0"],"title":"[Security Solution] Fix
incorrect default value for History Window
Size","number":207827,"url":"https://github.com/elastic/kibana/pull/207827","mergeCommit":{"message":"[Security
Solution] Fix incorrect default value for History Window Size
(#207827)\n\n**Resolves:
https://github.com/elastic/kibana/issues/207348**\n\n## Summary\nThis PR
resolves a bug in the editable History Window Size component,\nwhich
incorrectly displays \"0 seconds\" as the default value instead of\n\"7
days\". The issue was caused by passing an array of units `(['m',
'h',\n'd'])` using an incorrect prop name.\n\nAlso, I fixed stuck last
digit being stuck when trying to remove it\n**Before my changes: can't
remove or reset the last
digit**\n\n\nhttps://github.com/user-attachments/assets/3a7d28ea-ea71-4ee8-8805-902bae1dc3c1\n\n**After
my changes: value resets to minValue on last digit
removal**\n\n\nhttps://github.com/user-attachments/assets/1cba36bb-9127-4197-871e-62b978b3612c\n\n\nWork
started on:
22-Jan-2025","sha":"8894c1f395ce5d30c750db9d5b6c9be4fecac6f7"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/207827","number":207827,"mergeCommit":{"message":"[Security
Solution] Fix incorrect default value for History Window Size
(#207827)\n\n**Resolves:
https://github.com/elastic/kibana/issues/207348**\n\n## Summary\nThis PR
resolves a bug in the editable History Window Size component,\nwhich
incorrectly displays \"0 seconds\" as the default value instead of\n\"7
days\". The issue was caused by passing an array of units `(['m',
'h',\n'd'])` using an incorrect prop name.\n\nAlso, I fixed stuck last
digit being stuck when trying to remove it\n**Before my changes: can't
remove or reset the last
digit**\n\n\nhttps://github.com/user-attachments/assets/3a7d28ea-ea71-4ee8-8805-902bae1dc3c1\n\n**After
my changes: value resets to minValue on last digit
removal**\n\n\nhttps://github.com/user-attachments/assets/1cba36bb-9127-4197-871e-62b978b3612c\n\n\nWork
started on:
22-Jan-2025","sha":"8894c1f395ce5d30c750db9d5b6c9be4fecac6f7"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nikita Indik <[email protected]>
  • Loading branch information
kibanamachine and nikitaindik authored Jan 24, 2025
1 parent 57d997f commit 3e02341
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ describe('ScheduleItemField', () => {
expect(mockField.setValue).toHaveBeenCalledWith('5000000s');
});

it(`uses the "units" prop when it's passed`, async () => {
const mockField = useFormFieldMock<string>({ value: '7d' });
const wrapper = mount(
<TestProviders>
<ScheduleItemField
field={mockField}
units={['m', 'h', 'd']}
dataTestSubj="schedule-item"
idAria="idAria"
/>
</TestProviders>
);

expect(wrapper.find('[data-test-subj="interval"]').last().prop('value')).toEqual(7);
expect(wrapper.find('[data-test-subj="timeType"]').last().prop('value')).toEqual('d');

wrapper
.find('[data-test-subj="interval"]')
.last()
.simulate('change', { target: { value: '8' } });

expect(mockField.setValue).toHaveBeenCalledWith('8d');
});

it.each([
[-10, -5],
[-5, 0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface ScheduleItemProps {
isDisabled?: boolean;
minValue?: number;
maxValue?: number;
timeTypes?: string[];
units?: string[];
fullWidth?: boolean;
}

Expand Down Expand Up @@ -74,10 +74,10 @@ export function ScheduleItemField({
idAria,
minValue = Number.MIN_SAFE_INTEGER,
maxValue = Number.MAX_SAFE_INTEGER,
timeTypes = DEFAULT_TIME_DURATION_UNITS,
units = DEFAULT_TIME_DURATION_UNITS,
fullWidth = false,
}: ScheduleItemProps): JSX.Element {
const [timeType, setTimeType] = useState(timeTypes[0]);
const [timeType, setTimeType] = useState(units[0]);
const [timeVal, setTimeVal] = useState<number>(0);
const { isInvalid, errorMessage } = getFieldValidityAndErrorMessage(field);
const { value, setValue } = field;
Expand All @@ -92,7 +92,7 @@ export function ScheduleItemField({

const onChangeTimeVal = useCallback<NonNullable<EuiFieldNumberProps['onChange']>>(
(e) => {
const number = parseInt(e.target.value, 10);
const number = e.target.value === '' ? minValue : parseInt(e.target.value, 10);

if (Number.isNaN(number)) {
return;
Expand All @@ -112,7 +112,7 @@ export function ScheduleItemField({
}

const isNegative = value.startsWith('-');
const durationRegexp = new RegExp(`^\\-?(\\d+)(${timeTypes.join('|')})$`);
const durationRegexp = new RegExp(`^\\-?(\\d+)(${units.join('|')})$`);
const durationMatchArray = value.match(durationRegexp);

if (!durationMatchArray) {
Expand All @@ -124,7 +124,7 @@ export function ScheduleItemField({

setTimeVal(time);
setTimeType(unit);
}, [timeType, timeTypes, timeVal, value]);
}, [timeType, units, timeVal, value]);

const label = useMemo(
() => (
Expand Down Expand Up @@ -154,7 +154,7 @@ export function ScheduleItemField({
append={
<MyEuiSelect
fullWidth
options={timeTypeOptions.filter((type) => timeTypes.includes(type.value))}
options={timeTypeOptions.filter((type) => units.includes(type.value))}
value={timeType}
onChange={onChangeTimeType}
disabled={isDisabled}
Expand Down

0 comments on commit 3e02341

Please sign in to comment.