fix(profile): update editor sequences through functional state

The rule, group and proxy editors mixed functional updaters with writes
computed from render-captured arrays, so two clicks in one React batch
(the second before re-render) read a pre-batch array: deletes dropped
an entry queued by the sibling updater, reorders resolved indices
against a stale list, and add buttons could duplicate past their
uniqueness checks. Every sequence mutation now derives from the live
previous state; the group add buttons keep their translated duplicate
notice and recheck inside the updater.
This commit is contained in:
Tunglies
2026-11-19 22:58:39 +08:00
parent f2c70c5180
commit cc202fa312
3 changed files with 75 additions and 58 deletions
+33 -20
View File
@@ -218,7 +218,7 @@ export const GroupsEditorViewer = (props: Props) => {
group={item}
onDelete={() => {
if (isDeleted) {
setDeleteSeq(deleteSeq.filter((v) => v !== item.name))
setDeleteSeq((prev) => prev.filter((v) => v !== item.name))
} else {
setDeleteSeq((prev) => [...prev, item.name])
}
@@ -233,7 +233,7 @@ export const GroupsEditorViewer = (props: Props) => {
type="prepend"
group={item}
onDelete={() => {
setPrependSeq(prependSeq.filter((v) => v.name !== item.name))
setPrependSeq((prev) => prev.filter((v) => v.name !== item.name))
}}
/>
)
@@ -244,7 +244,7 @@ export const GroupsEditorViewer = (props: Props) => {
type="append"
group={item}
onDelete={() => {
setAppendSeq(appendSeq.filter((v) => v.name !== item.name))
setAppendSeq((prev) => prev.filter((v) => v.name !== item.name))
}}
/>
)
@@ -255,21 +255,22 @@ export const GroupsEditorViewer = (props: Props) => {
activeIndex: number,
overIndex: number,
) => {
const list = category === 'prepend' ? prependSeq : appendSeq
const filtered =
category === 'prepend' ? filteredPrependSeq : filteredAppendSeq
const setList = category === 'prepend' ? setPrependSeq : setAppendSeq
const activeRealIndex = findRealIndex(list, filtered, activeIndex)
const overRealIndex = findRealIndex(list, filtered, overIndex)
if (
activeRealIndex < 0 ||
overRealIndex < 0 ||
activeRealIndex === overRealIndex
) {
return
}
// Recompute inside the updater: two writes in one batch must not read the same pre-batch array.
setList((prev) => {
const filtered = prev.filter((v) => match(v.name))
const activeRealIndex = findRealIndex(prev, filtered, activeIndex)
const overRealIndex = findRealIndex(prev, filtered, overIndex)
if (
activeRealIndex < 0 ||
overRealIndex < 0 ||
activeRealIndex === overRealIndex
) {
return prev
}
setList(arrayMove(list, activeRealIndex, overRealIndex))
return arrayMove(prev, activeRealIndex, overRealIndex)
})
}
const fetchContent = useCallback(async () => {
@@ -1019,14 +1020,20 @@ export const GroupsEditorViewer = (props: Props) => {
onClick={() => {
try {
validateGroup()
const values = formIns.getValues()
for (const item of [...prependSeq, ...groupList]) {
if (item.name === formIns.getValues().name) {
if (item.name === values.name) {
throw new Error(
t('profiles.modals.groupsEditor.errors.nameExists'),
)
}
}
setPrependSeq([formIns.getValues(), ...prependSeq])
// Recheck inside the updater so a same-batch second click cannot duplicate the entry.
setPrependSeq((prev) =>
prev.some((v) => v.name === values.name)
? prev
: [values, ...prev],
)
} catch (err) {
showNotice.error(err)
}
@@ -1043,14 +1050,20 @@ export const GroupsEditorViewer = (props: Props) => {
onClick={() => {
try {
validateGroup()
const values = formIns.getValues()
for (const item of [...appendSeq, ...groupList]) {
if (item.name === formIns.getValues().name) {
if (item.name === values.name) {
throw new Error(
t('profiles.modals.groupsEditor.errors.nameExists'),
)
}
}
setAppendSeq([...appendSeq, formIns.getValues()])
// Recheck inside the updater so a same-batch second click cannot duplicate the entry.
setAppendSeq((prev) =>
prev.some((v) => v.name === values.name)
? prev
: [...prev, values],
)
} catch (err) {
showNotice.error(err)
}
@@ -123,7 +123,7 @@ export const ProxiesEditorViewer = (props: Props) => {
proxy={item}
onDelete={() => {
if (isDeleted) {
setDeleteSeq(deleteSeq.filter((v) => v !== item.name))
setDeleteSeq((prev) => prev.filter((v) => v !== item.name))
} else {
setDeleteSeq((prev) => [...prev, item.name])
}
@@ -138,7 +138,7 @@ export const ProxiesEditorViewer = (props: Props) => {
type="prepend"
proxy={item}
onDelete={() => {
setPrependSeq(prependSeq.filter((v) => v.name !== item.name))
setPrependSeq((prev) => prev.filter((v) => v.name !== item.name))
}}
/>
)
@@ -149,7 +149,7 @@ export const ProxiesEditorViewer = (props: Props) => {
type="append"
proxy={item}
onDelete={() => {
setAppendSeq(appendSeq.filter((v) => v.name !== item.name))
setAppendSeq((prev) => prev.filter((v) => v.name !== item.name))
}}
/>
)
@@ -160,21 +160,22 @@ export const ProxiesEditorViewer = (props: Props) => {
activeIndex: number,
overIndex: number,
) => {
const list = category === 'prepend' ? prependSeq : appendSeq
const filtered =
category === 'prepend' ? filteredPrependSeq : filteredAppendSeq
const setList = category === 'prepend' ? setPrependSeq : setAppendSeq
const activeRealIndex = findRealIndex(list, filtered, activeIndex)
const overRealIndex = findRealIndex(list, filtered, overIndex)
if (
activeRealIndex < 0 ||
overRealIndex < 0 ||
activeRealIndex === overRealIndex
) {
return
}
// Recompute inside the updater: two writes in one batch must not read the same pre-batch array.
setList((prev) => {
const filtered = prev.filter((v) => hasValidName(v) && match(v.name))
const activeRealIndex = findRealIndex(prev, filtered, activeIndex)
const overRealIndex = findRealIndex(prev, filtered, overIndex)
if (
activeRealIndex < 0 ||
overRealIndex < 0 ||
activeRealIndex === overRealIndex
) {
return prev
}
setList(arrayMove(list, activeRealIndex, overRealIndex))
return arrayMove(prev, activeRealIndex, overRealIndex)
})
}
// 优化:异步分片解析,避免主线程阻塞,解析完成后批量setState
+25 -22
View File
@@ -318,7 +318,7 @@ export const RulesEditorViewer = (props: Props) => {
ruleRaw={item}
onDelete={() => {
if (isDeleted) {
setDeleteSeq(deleteSeq.filter((v) => v !== item))
setDeleteSeq((prev) => prev.filter((v) => v !== item))
} else {
setDeleteSeq((prev) => [...prev, item])
}
@@ -333,13 +333,13 @@ export const RulesEditorViewer = (props: Props) => {
type="prepend"
ruleRaw={item}
onDelete={() => {
setPrependSeq(prependSeq.filter((v) => v !== item))
setPrependSeq((prev) => prev.filter((v) => v !== item))
}}
onAppend={() => {
setAppendSeq((prev) =>
prev.includes(item) ? prev : [...prev, item],
)
setPrependSeq(prependSeq.filter((v) => v !== item))
setPrependSeq((prev) => prev.filter((v) => v !== item))
}}
/>
)
@@ -350,13 +350,13 @@ export const RulesEditorViewer = (props: Props) => {
type="append"
ruleRaw={item}
onDelete={() => {
setAppendSeq(appendSeq.filter((v) => v !== item))
setAppendSeq((prev) => prev.filter((v) => v !== item))
}}
onPrepend={() => {
setPrependSeq((prev) =>
prev.includes(item) ? prev : [...prev, item],
)
setAppendSeq(appendSeq.filter((v) => v !== item))
setAppendSeq((prev) => prev.filter((v) => v !== item))
}}
/>
)
@@ -367,21 +367,22 @@ export const RulesEditorViewer = (props: Props) => {
activeIndex: number,
overIndex: number,
) => {
const list = category === 'prepend' ? prependSeq : appendSeq
const filtered =
category === 'prepend' ? filteredPrependSeq : filteredAppendSeq
const setList = category === 'prepend' ? setPrependSeq : setAppendSeq
const activeRealIndex = findRealIndex(list, filtered, activeIndex)
const overRealIndex = findRealIndex(list, filtered, overIndex)
if (
activeRealIndex < 0 ||
overRealIndex < 0 ||
activeRealIndex === overRealIndex
) {
return
}
// Recompute inside the updater: two writes in one batch must not read the same pre-batch array.
setList((prev) => {
const filtered = prev.filter(match)
const activeRealIndex = findRealIndex(prev, filtered, activeIndex)
const overRealIndex = findRealIndex(prev, filtered, overIndex)
if (
activeRealIndex < 0 ||
overRealIndex < 0 ||
activeRealIndex === overRealIndex
) {
return prev
}
setList(arrayMove(list, activeRealIndex, overRealIndex))
return arrayMove(prev, activeRealIndex, overRealIndex)
})
}
const fetchContent = useCallback(async () => {
@@ -748,8 +749,9 @@ export const RulesEditorViewer = (props: Props) => {
onClick={() => {
try {
const raw = validateRule()
if (prependSeq.includes(raw)) return
setPrependSeq([raw, ...prependSeq])
setPrependSeq((prev) =>
prev.includes(raw) ? prev : [raw, ...prev],
)
} catch (err: any) {
showNotice.error(err)
}
@@ -766,8 +768,9 @@ export const RulesEditorViewer = (props: Props) => {
onClick={() => {
try {
const raw = validateRule()
if (appendSeq.includes(raw)) return
setAppendSeq([...appendSeq, raw])
setAppendSeq((prev) =>
prev.includes(raw) ? prev : [...prev, raw],
)
} catch (err: any) {
showNotice.error(err)
}