Skip to content

Commit 465c7bc

Browse files
authored
fix(batch-rename): fix invalid regex exception (#331)
* fix(batch-rename): fix invalid regex exception * fix(batch-rename): clear validation errors when switching type * fix(batch-rename): add missing translation for invalid regular expression
1 parent 88100f6 commit 465c7bc

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/lang/en/global.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"no_support_now": "Not currently supported",
3030
"empty_input": "Please enter",
3131
"invalid_filename_chars": "Filename cannot contain: / \\ ? < > * : | \"",
32+
"invalid_regex": "Invalid regular expression",
3233
"name": "Name",
3334
"go_to_storages": "Go to Storages"
3435
}

src/pages/home/toolbar/BatchRename.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,24 @@ export const BatchRename = () => {
4747
const [validationErrorNew, setValidationErrorNew] = createSignal<string>("")
4848
const t = useT()
4949

50+
const validateRegex = (pattern: string) => {
51+
try {
52+
// eslint-disable-next-line no-new
53+
new RegExp(pattern)
54+
return { valid: true as const }
55+
} catch (e) {
56+
return { valid: false as const, error: "invalid_regex" }
57+
}
58+
}
59+
5060
const handleInputSrc = (newValue: string) => {
5161
setSrcName(newValue)
5262
if (type() === "2" || type() === "3") {
5363
const validation = validateFilename(newValue)
5464
setValidationErrorSrc(validation.valid ? "" : validation.error || "")
65+
} else if (type() === "1") {
66+
const validation = validateRegex(newValue)
67+
setValidationErrorSrc(validation.valid ? "" : validation.error || "")
5568
} else {
5669
setValidationErrorSrc("")
5770
}
@@ -92,6 +105,13 @@ export const BatchRename = () => {
92105
notify.warning(t("global.empty_input"))
93106
return
94107
}
108+
if (type() === "1") {
109+
const validationSrc = validateRegex(srcName())
110+
if (!validationSrc.valid) {
111+
notify.warning(t(`global.${validationSrc.error}`))
112+
return
113+
}
114+
}
95115
if (type() === "2" || type() === "3") {
96116
const validationSrc = validateFilename(srcName())
97117
if (!validationSrc.valid) {
@@ -104,10 +124,10 @@ export const BatchRename = () => {
104124
return
105125
}
106126
}
107-
const replaceRegexp = new RegExp(srcName(), "g")
108-
109127
let matchNames: RenameObj[]
110128
if (type() === "1") {
129+
const replaceRegexp = new RegExp(srcName(), "g")
130+
111131
matchNames = selectedObjs()
112132
.filter((obj) => obj.name.match(srcName()))
113133
.map((obj) => {
@@ -239,6 +259,9 @@ export const BatchRename = () => {
239259
} else if (event === "2") {
240260
setNewNameType("number")
241261
}
262+
// Clear validation errors when switching type
263+
setValidationErrorSrc("")
264+
setValidationErrorNew("")
242265
}}
243266
>
244267
<HStack spacing="$4">
@@ -292,6 +315,11 @@ export const BatchRename = () => {
292315
}
293316
}}
294317
/>
318+
<Show when={validationErrorNew()}>
319+
<Text color="$danger9" fontSize="$sm">
320+
{t(`global.${validationErrorNew()}`)}
321+
</Text>
322+
</Show>
295323
<Show when={type() === "2"}>
296324
<Input
297325
id="modal-input3"
@@ -312,11 +340,6 @@ export const BatchRename = () => {
312340
}}
313341
/>
314342
</Show>
315-
<Show when={validationErrorNew()}>
316-
<Text color="$danger9" fontSize="$sm">
317-
{t(`global.${validationErrorNew()}`)}
318-
</Text>
319-
</Show>
320343
</VStack>
321344
</ModalBody>
322345
<ModalFooter display="flex" gap="$2">

0 commit comments

Comments
 (0)