From 55725922a5d149ed6a5bb0d5976ec3130b14dd96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 6 Nov 2023 15:50:33 +0100 Subject: [PATCH] Ensure that shortcuts aren't duplicate-checked against themselves MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/curvy-carpets-kneel.md | 5 +++++ plugins/shortcuts/src/ShortcutForm.tsx | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 .changeset/curvy-carpets-kneel.md diff --git a/.changeset/curvy-carpets-kneel.md b/.changeset/curvy-carpets-kneel.md new file mode 100644 index 0000000000..e6b0019edb --- /dev/null +++ b/.changeset/curvy-carpets-kneel.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-shortcuts': patch +--- + +Ensure that shortcuts aren't duplicate-checked against themselves diff --git a/plugins/shortcuts/src/ShortcutForm.tsx b/plugins/shortcuts/src/ShortcutForm.tsx index e8cd8144d0..4d6fdcbc27 100644 --- a/plugins/shortcuts/src/ShortcutForm.tsx +++ b/plugins/shortcuts/src/ShortcutForm.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ -import React, { useEffect } from 'react'; +import React, { useEffect, useRef } from 'react'; import useObservable from 'react-use/lib/useObservable'; import { useForm, SubmitHandler, Controller } from 'react-hook-form'; import { @@ -58,6 +58,10 @@ export const ShortcutForm = ({ shortcutApi.shortcut$(), shortcutApi.get(), ); + const { current: originalValues } = useRef({ + url: formValues?.url ?? '', + title: formValues?.title ?? '', + }); const { handleSubmit, reset, @@ -65,20 +69,23 @@ export const ShortcutForm = ({ formState: { errors }, } = useForm({ mode: 'onChange', - defaultValues: { - url: formValues?.url ?? '', - title: formValues?.title ?? '', - }, + defaultValues: originalValues, }); const titleIsUnique = (title: string) => { - if (shortcutData.some(shortcutTitle => shortcutTitle.title === title)) + if ( + title !== originalValues.title && + shortcutData.some(shortcutTitle => shortcutTitle.title === title) + ) return 'A shortcut with this title already exists'; return true; }; const urlIsUnique = (url: string) => { - if (shortcutData.some(shortcutUrl => shortcutUrl.url === url)) + if ( + url !== originalValues.url && + shortcutData.some(shortcutUrl => shortcutUrl.url === url) + ) return 'A shortcut with this url already exists'; return true; };