-
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 all 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 |
|---|---|---|
|
|
@@ -26,6 +26,38 @@ import { Form, UseField, useForm } from '@kbn/es-ui-shared-plugin/static/forms/h | |
|
|
||
| const { emptyField, startsWithField, containsCharsField } = fieldValidators; | ||
|
|
||
| const MAX_POLICY_NAME_BYTES = 255; | ||
|
|
||
| const getPolicyNameIsBeyondMaxBytes = (policyName: string) => { | ||
| return ( | ||
| window.TextEncoder && new window.TextEncoder().encode(policyName).length > MAX_POLICY_NAME_BYTES | ||
| ); | ||
| }; | ||
|
|
||
| const getFirstAvailableCopyName = ({ | ||
| originalPolicyName, | ||
| policyNames, | ||
| }: { | ||
| originalPolicyName: string; | ||
| policyNames: string[]; | ||
| }) => { | ||
| const existing = new Set(policyNames); | ||
|
|
||
| // If the shortest candidate is already too long, any other single-digit suffix will be too. | ||
| if (getPolicyNameIsBeyondMaxBytes(`${originalPolicyName}-2`)) { | ||
| return ''; | ||
| } | ||
|
|
||
| for (let i = 2; i <= 9; i++) { | ||
| const candidate = `${originalPolicyName}-${i}`; | ||
| if (!existing.has(candidate) && !getPolicyNameIsBeyondMaxBytes(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', { | ||
|
|
@@ -83,7 +115,7 @@ export const createPolicyNameValidations = ({ | |
| { | ||
| validator: (arg) => { | ||
| const policyName = arg.value; | ||
| if (window.TextEncoder && new window.TextEncoder().encode(policyName).length > 255) { | ||
| if (getPolicyNameIsBeyondMaxBytes(policyName)) { | ||
| return { | ||
| message: i18nTexts.errors.policyNameTooLongErrorMessage, | ||
| }; | ||
|
|
@@ -108,18 +140,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 }), | ||
| }, | ||
| }); | ||
|
|
||
|
|
@@ -191,7 +225,7 @@ export function CreatePolicyModal({ | |
| data-test-subj="createPolicyModal-saveButton" | ||
| fill | ||
| onClick={handleSave} | ||
| disabled={!form.isValid || isLoading} | ||
| disabled={form.isValid === false || isLoading} | ||
| isLoading={isLoading} | ||
| > | ||
| {i18n.translate('xpack.streams.createPolicyModal.saveButton', { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.