feat: feature/mvp-sprint-1 Extracted execute command into separate function in Commander

This commit is contained in:
Nikolaj Frey 2023-08-09 08:13:09 +10:00
parent 4edb9af1c7
commit fe3b9eb496

View File

@ -211,47 +211,7 @@ export const Commander = (props) => {
} }
}, [thingtime?.settings?.commanderActive, closeCommander, openCommander]) }, [thingtime?.settings?.commanderActive, closeCommander, openCommander])
const allCommanderKeyListener = React.useCallback( const executeCommand = React.useCallback(() => {
(e: any) => {
console.log("commander key listener e?.code", e?.code)
thingtimeRef.current = thingtime
if (e?.metaKey && e?.code === "KeyP") {
e.preventDefault()
e.stopPropagation()
toggleCommander()
}
// if key escape close all modals
else if (e?.code === "Escape") {
closeCommander()
}
// only run these if commander active
if (commanderActive) {
// if arrow keys then move selection
if (e?.code === "ArrowUp") {
// move selection up
const curSuggestionIdx =
typeof hoveredSuggestion === "number"
? hoveredSuggestion
: suggestions?.length
const newSuggestionIdx = curSuggestionIdx - 1
if (newSuggestionIdx >= 0) {
setHoveredSuggestion(newSuggestionIdx)
} else {
setHoveredSuggestion(suggestions?.length - 1)
}
} else if (e?.code === "ArrowDown") {
// move selection down
const curSuggestionIdx =
typeof hoveredSuggestion === "number" ? hoveredSuggestion : -1
const newSuggestionIdx = curSuggestionIdx + 1
if (newSuggestionIdx < suggestions?.length) {
setHoveredSuggestion(newSuggestionIdx)
} else {
setHoveredSuggestion(0)
}
} else if (e?.code === "Enter") {
// if selection is active then select it // if selection is active then select it
const curSuggestionIdx = hoveredSuggestion const curSuggestionIdx = hoveredSuggestion
if (curSuggestionIdx !== null) { if (curSuggestionIdx !== null) {
@ -310,17 +270,9 @@ export const Commander = (props) => {
console.error("Caught error on commander onEnter", err) console.error("Caught error on commander onEnter", err)
} }
} }
} }, [
}
},
[
closeCommander,
toggleCommander,
hoveredSuggestion, hoveredSuggestion,
selectSuggestion, selectSuggestion,
suggestions,
thingtime,
thingtimeRef,
commanderActive, commanderActive,
commandIsAction, commandIsAction,
commandPath, commandPath,
@ -328,7 +280,64 @@ export const Commander = (props) => {
escapedCommandValue, escapedCommandValue,
getThingtime, getThingtime,
setThingtime, setThingtime,
setContextPath,
setShowContext, setShowContext,
])
const allCommanderKeyListener = React.useCallback(
(e: any) => {
console.log("commander key listener e?.code", e?.code)
thingtimeRef.current = thingtime
if (e?.metaKey && e?.code === "KeyP") {
e.preventDefault()
e.stopPropagation()
toggleCommander()
}
// if key escape close all modals
else if (e?.code === "Escape") {
closeCommander()
}
// only run these if commander active
if (commanderActive) {
// if arrow keys then move selection
if (e?.code === "ArrowUp") {
// move selection up
const curSuggestionIdx =
typeof hoveredSuggestion === "number"
? hoveredSuggestion
: suggestions?.length
const newSuggestionIdx = curSuggestionIdx - 1
if (newSuggestionIdx >= 0) {
setHoveredSuggestion(newSuggestionIdx)
} else {
setHoveredSuggestion(suggestions?.length - 1)
}
} else if (e?.code === "ArrowDown") {
// move selection down
const curSuggestionIdx =
typeof hoveredSuggestion === "number" ? hoveredSuggestion : -1
const newSuggestionIdx = curSuggestionIdx + 1
if (newSuggestionIdx < suggestions?.length) {
setHoveredSuggestion(newSuggestionIdx)
} else {
setHoveredSuggestion(0)
}
} else if (e?.code === "Enter") {
executeCommand()
}
}
},
[
closeCommander,
toggleCommander,
hoveredSuggestion,
suggestions,
thingtime,
thingtimeRef,
commanderActive,
executeCommand,
] ]
) )