fix(hitl-edit-panel): move duplicate-tag check into functional setTags

Fixes #1248

handleAddTag had tags in its useCallback dependency array only so the
closure-level duplicate check could read it, which forced the callback
to re-create on every tag mutation and compared new additions against
a potentially-stale closure value.

Collapse the duplicate check into the functional setTags updater so
the check always runs against the latest state, and drop tags from
the dependency array - the callback is stable for the component's
lifetime and downstream memoization won't get invalidated on every
keystroke.
This commit is contained in:
Matt Van Horn 2026-04-21 01:43:20 -07:00
parent 3f30b12bd4
commit 7f0a5cd06a
No known key found for this signature in database

View file

@ -65,16 +65,15 @@ function EmailsTagField({
setTags((prev) => (typeof newTags === "function" ? newTags(prev) : newTags));
}, []);
const handleAddTag = useCallback(
(text: string) => {
const trimmed = text.trim();
if (!trimmed) return;
if (tags.some((tag) => tag.text === trimmed)) return;
const handleAddTag = useCallback((text: string) => {
const trimmed = text.trim();
if (!trimmed) return;
setTags((prev) => {
if (prev.some((tag) => tag.text === trimmed)) return prev;
const newTag: TagType = { id: Date.now().toString(), text: trimmed };
setTags((prev) => [...prev, newTag]);
},
[tags]
);
return [...prev, newTag];
});
}, []);
return (
<TagInput