Submit selection to command on unmount of selection arg input (#7047)

* Submit selection to command on unmount of selection arg input

This fixes #7024 by saving the user's selection to the command even if
they click another argument in the command palette's header. It does no
harm to save the selection to the argument, even if it's being torn down
because the user dismissed it has no negative effect.

* Refactor to not auto-submit before selection is cleared on mount

Thanks E2E test suite

* Update failing E2E tests with new behavior, which allows skip with preselection

---------

Co-authored-by: Pierre Jacquier <pierrejacquier39@gmail.com>
This commit is contained in:
Frank Noirot
2025-05-19 13:21:43 -04:00
committed by GitHub
parent 5b4cddd0b0
commit 49b78d726a
7 changed files with 59 additions and 108 deletions

View File

@ -54,6 +54,7 @@ function CommandBarSelectionInput({
const inputRef = useRef<HTMLInputElement>(null)
const commandBarState = useCommandBarState()
const [hasSubmitted, setHasSubmitted] = useState(false)
const [hasClearedSelection, setHasClearedSelection] = useState(false)
const selection = useSelector(arg.machineActor, selectionSelector)
const selectionsByType = useMemo(() => {
return getSelectionCountByType(selection)
@ -102,7 +103,7 @@ function CommandBarSelectionInput({
if (canSubmitSelection && arg.skip && argValue === undefined) {
handleSubmit()
}
}, [canSubmitSelection])
}, [arg.name, canSubmitSelection])
function handleChange() {
inputRef.current?.focus()
@ -139,7 +140,30 @@ function CommandBarSelectionInput({
selectionType: 'singleCodeCursor',
},
})
}, [arg.clearSelectionFirst])
setHasClearedSelection(true)
}, [arg])
// Watch for outside teardowns of this component
// (such as clicking another argument in the command palette header)
// and quickly save the current selection if we can
useEffect(() => {
return () => {
const resolvedSelection: Selections | undefined = isArgRequired
? selection
: selection || {
graphSelections: [],
otherSelections: [],
}
if (
!(arg.clearSelectionFirst && !hasClearedSelection) &&
canSubmitSelection &&
resolvedSelection
) {
onSubmit(resolvedSelection)
}
}
}, [hasClearedSelection])
// Set selection filter if needed, and reset it when the component unmounts
useEffect(() => {