Skip to content

Commit

Permalink
Merge pull request #2060 from harshalranjhani/harshalranjhani/2053/op…
Browse files Browse the repository at this point in the history
…en-goal-modal-with-spacebar-key

feat: open goal modal with spacebar key
  • Loading branch information
vinaybadgujar102 authored Oct 13, 2024
2 parents e2bf3d8 + 58df1ef commit 9264b7e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/hooks/useGoalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const useGoalSelection = (goals: GoalItem[]): GoalItem | undefined => {
const downPress = useKeyPress("ArrowDown");
const rightPress = useKeyPress("ArrowRight");
const leftPress = useKeyPress("ArrowLeft");
const spacePress = useKeyPress("Space");

const focusIndex = Number(searchParams.get("focus") || -1);

Expand Down Expand Up @@ -62,6 +63,15 @@ export const useGoalSelection = (goals: GoalItem[]): GoalItem | undefined => {
[goals],
);

const openGoalActionsModal = (goal: GoalItem) => {
if (!goal) return;
const newPath = goal.parentGoalId === "root" ? `/goals/root/${goal.id}` : `/goals/${goal.parentGoalId}/${goal.id}`;

navigate(`${newPath}?showOptions=true`, {
state: location.state,
});
};

useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) {
Expand Down Expand Up @@ -90,10 +100,14 @@ export const useGoalSelection = (goals: GoalItem[]): GoalItem | undefined => {
window.history.back();
}

if (spacePress && goals.length > 0) {
openGoalActionsModal(goals[focusIndex]);
}

return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [downPress, upPress, rightPress, leftPress]);
}, [downPress, upPress, rightPress, leftPress, spacePress]);

return goals[focusIndex];
};
2 changes: 1 addition & 1 deletion src/hooks/useKeyPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const useKeyPress = (targetKey: string) => {
const keyHandler = useCallback(
(event: KeyboardEvent) => {
event.stopPropagation();
if (event.key === targetKey) {
if (event.code === targetKey) {
setKeyPressed(event.type === "keydown");
}
},
Expand Down

0 comments on commit 9264b7e

Please sign in to comment.