-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Streams] Add suggested name #253652
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
[Streams] Add suggested name #253652
Changes from 2 commits
a7cf598
b46d150
7d59ef2
a7fd1ab
4f60825
240c9e1
ca03acd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| * 2.0. | ||
| */ | ||
|
|
||
| import React, { useMemo } from 'react'; | ||
| import React, { useEffect, useMemo } from 'react'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import { | ||
| EuiButton, | ||
|
|
@@ -26,6 +26,23 @@ import { Form, UseField, useForm } from '@kbn/es-ui-shared-plugin/static/forms/h | |
|
|
||
| const { emptyField, startsWithField, containsCharsField } = fieldValidators; | ||
|
|
||
| const getFirstAvailableCopyName = ({ | ||
| originalPolicyName, | ||
| policyNames, | ||
| }: { | ||
| originalPolicyName: string; | ||
| policyNames: string[]; | ||
| }) => { | ||
| const existing = new Set(policyNames); | ||
|
|
||
| for (let i = 2; i <= 9; i++) { | ||
| const candidate = `${originalPolicyName}-${i}`; | ||
|
SoniaSanzV marked this conversation as resolved.
|
||
| if (!existing.has(candidate)) return candidate; | ||
| } | ||
|
Comment on lines
+46
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check both before and inside the loop?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that's needed. {name}-2 and {name}-9 (max number) would have the same UTF‑8 byte length, so checking inside the loop is redundan |
||
|
|
||
| return ''; | ||
| }; | ||
|
|
||
| const i18nTexts = { | ||
| errors: { | ||
| policyNameRequiredMessage: i18n.translate('xpack.streams.createPolicyModal.emptyNameError', { | ||
|
|
@@ -108,18 +125,20 @@ export interface CreatePolicyModalProps { | |
| onBack: () => void; | ||
| onSave: (policyName: string) => void; | ||
| isLoading?: boolean; | ||
| originalPolicyName: string; | ||
| } | ||
|
|
||
| export function CreatePolicyModal({ | ||
| policyNames, | ||
| onBack, | ||
| onSave, | ||
| isLoading = false, | ||
| originalPolicyName, | ||
| }: CreatePolicyModalProps) { | ||
| const modalTitleId = useGeneratedHtmlId(); | ||
| const { form } = useForm({ | ||
| defaultValue: { | ||
| policyName: '', | ||
| policyName: getFirstAvailableCopyName({ originalPolicyName, policyNames }), | ||
| }, | ||
| }); | ||
|
|
||
|
|
@@ -128,6 +147,11 @@ export function CreatePolicyModal({ | |
| [policyNames] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| // Ensure the pre-populated value updates form.isValid so Save enables when appropriate. | ||
| void form.validate(); | ||
| }, [form]); | ||
|
SoniaSanzV marked this conversation as resolved.
Outdated
|
||
|
|
||
| const handleSave = async () => { | ||
| const { isValid, data } = await form.submit(); | ||
| if (!isValid) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: It's not really the original policy name but original + "-2" so we can change it for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed with a7fd1ab