fix: pre check
This commit is contained in:
@@ -13,7 +13,11 @@ type CreateScriptModalProps = {
|
|||||||
open: boolean;
|
open: boolean;
|
||||||
creating: boolean;
|
creating: boolean;
|
||||||
form: NewScriptForm;
|
form: NewScriptForm;
|
||||||
scripts: Array<{ script_type: ScriptType; script_name: string }>;
|
scripts: Array<{
|
||||||
|
script_type: ScriptType;
|
||||||
|
script_name: string;
|
||||||
|
relative_path?: string;
|
||||||
|
}>;
|
||||||
onFormChange: (form: NewScriptForm) => void;
|
onFormChange: (form: NewScriptForm) => void;
|
||||||
onSubmit: (event: FormEvent) => void;
|
onSubmit: (event: FormEvent) => void;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
@@ -35,10 +39,21 @@ export function CreateScriptModal({
|
|||||||
const normalizedName = requestedName.toLocaleLowerCase().endsWith(suffix)
|
const normalizedName = requestedName.toLocaleLowerCase().endsWith(suffix)
|
||||||
? requestedName
|
? requestedName
|
||||||
: `${requestedName}${suffix}`;
|
: `${requestedName}${suffix}`;
|
||||||
const duplicate = scripts.some((script) =>
|
const duplicate = scripts.some((script) => {
|
||||||
script.script_type === form.scriptType
|
if (script.script_type !== form.scriptType) return false;
|
||||||
&& script.script_name.toLocaleLowerCase() === normalizedName.toLocaleLowerCase()
|
if (script.script_name.toLocaleLowerCase()
|
||||||
);
|
!== normalizedName.toLocaleLowerCase()) return false;
|
||||||
|
// Same name in a different subdirectory is allowed; mirror the
|
||||||
|
// backend name_clash scope (StorageObjects.relative_path JOIN).
|
||||||
|
if (!script.relative_path) return true;
|
||||||
|
const segments = script.relative_path.replaceAll("\\", "/")
|
||||||
|
.split("/").slice(2);
|
||||||
|
const existingUserPath = segments.join("/");
|
||||||
|
const existingParent = existingUserPath.includes("/")
|
||||||
|
? existingUserPath.slice(0, existingUserPath.lastIndexOf("/"))
|
||||||
|
: "";
|
||||||
|
return existingParent === form.parentPath;
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="modal-backdrop" role="presentation">
|
<div className="modal-backdrop" role="presentation">
|
||||||
|
|||||||
@@ -392,6 +392,7 @@ export default function ScriptsPage() {
|
|||||||
scripts={scripts.map((s) => ({
|
scripts={scripts.map((s) => ({
|
||||||
script_type: s.script_type,
|
script_type: s.script_type,
|
||||||
script_name: s.script_name,
|
script_name: s.script_name,
|
||||||
|
relative_path: s.relative_path,
|
||||||
}))}
|
}))}
|
||||||
onFormChange={setCreateForm}
|
onFormChange={setCreateForm}
|
||||||
onSubmit={handleCreateSubmit}
|
onSubmit={handleCreateSubmit}
|
||||||
|
|||||||
@@ -758,12 +758,19 @@ export const useScriptWorkspaceStore = create<State>((set, get) => {
|
|||||||
const normalizedName = requestedName.toLocaleLowerCase().endsWith(suffix)
|
const normalizedName = requestedName.toLocaleLowerCase().endsWith(suffix)
|
||||||
? requestedName
|
? requestedName
|
||||||
: `${requestedName}${suffix}`;
|
: `${requestedName}${suffix}`;
|
||||||
const duplicate = get().scripts.some(
|
const duplicate = get().scripts.some((script) => {
|
||||||
(script) =>
|
if (script.script_type !== form.scriptType) return false;
|
||||||
script.script_type === form.scriptType
|
if (script.script_name.toLocaleLowerCase()
|
||||||
&& script.script_name.toLocaleLowerCase()
|
!== normalizedName.toLocaleLowerCase()) return false;
|
||||||
=== normalizedName.toLocaleLowerCase(),
|
// Same name in a different subdirectory is allowed: mirror the
|
||||||
);
|
// backend name_clash check, which JOINs StorageObjects and scopes
|
||||||
|
// by relative_path.
|
||||||
|
const existingUserPath = ownedScriptPath(script);
|
||||||
|
const existingParent = existingUserPath.includes("/")
|
||||||
|
? existingUserPath.slice(0, existingUserPath.lastIndexOf("/"))
|
||||||
|
: "";
|
||||||
|
return existingParent === form.parentPath;
|
||||||
|
});
|
||||||
if (duplicate) {
|
if (duplicate) {
|
||||||
pushToast("error", `${normalizedName} 已存在,请更换名称`);
|
pushToast("error", `${normalizedName} 已存在,请更换名称`);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user