diff --git a/src/lang/en/global.json b/src/lang/en/global.json index caec81d..7561b2d 100644 --- a/src/lang/en/global.json +++ b/src/lang/en/global.json @@ -29,6 +29,7 @@ "no_support_now": "Not currently supported", "empty_input": "Please enter", "invalid_filename_chars": "Filename cannot contain: / \\ ? < > * : | \"", + "invalid_regex": "Invalid regular expression", "name": "Name", "go_to_storages": "Go to Storages" } diff --git a/src/pages/home/toolbar/BatchRename.tsx b/src/pages/home/toolbar/BatchRename.tsx index abc5099..e74137c 100644 --- a/src/pages/home/toolbar/BatchRename.tsx +++ b/src/pages/home/toolbar/BatchRename.tsx @@ -47,11 +47,24 @@ export const BatchRename = () => { const [validationErrorNew, setValidationErrorNew] = createSignal("") const t = useT() + const validateRegex = (pattern: string) => { + try { + // eslint-disable-next-line no-new + new RegExp(pattern) + return { valid: true as const } + } catch (e) { + return { valid: false as const, error: "invalid_regex" } + } + } + const handleInputSrc = (newValue: string) => { setSrcName(newValue) if (type() === "2" || type() === "3") { const validation = validateFilename(newValue) setValidationErrorSrc(validation.valid ? "" : validation.error || "") + } else if (type() === "1") { + const validation = validateRegex(newValue) + setValidationErrorSrc(validation.valid ? "" : validation.error || "") } else { setValidationErrorSrc("") } @@ -92,6 +105,13 @@ export const BatchRename = () => { notify.warning(t("global.empty_input")) return } + if (type() === "1") { + const validationSrc = validateRegex(srcName()) + if (!validationSrc.valid) { + notify.warning(t(`global.${validationSrc.error}`)) + return + } + } if (type() === "2" || type() === "3") { const validationSrc = validateFilename(srcName()) if (!validationSrc.valid) { @@ -104,10 +124,10 @@ export const BatchRename = () => { return } } - const replaceRegexp = new RegExp(srcName(), "g") - let matchNames: RenameObj[] if (type() === "1") { + const replaceRegexp = new RegExp(srcName(), "g") + matchNames = selectedObjs() .filter((obj) => obj.name.match(srcName())) .map((obj) => { @@ -239,6 +259,9 @@ export const BatchRename = () => { } else if (event === "2") { setNewNameType("number") } + // Clear validation errors when switching type + setValidationErrorSrc("") + setValidationErrorNew("") }} > @@ -292,6 +315,11 @@ export const BatchRename = () => { } }} /> + + + {t(`global.${validationErrorNew()}`)} + + { }} /> - - - {t(`global.${validationErrorNew()}`)} - -