-
Notifications
You must be signed in to change notification settings - Fork 70
fix(Tasks): implement hotkeysEnabled state to control keyboard shortcuts #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,7 +115,9 @@ export const Tasks = ( | |
| new Set() | ||
| ); | ||
| const tableRef = useRef<HTMLDivElement>(null); | ||
| const [hotkeysEnabled, setHotkeysEnabled] = useState(false); | ||
| const [isMouseOver, setIsMouseOver] = useState(false); | ||
| const [activeContext, setActiveContext] = useState<string | null>(null); | ||
| const hotkeysEnabled = activeContext === 'TASKS' || isMouseOver; | ||
| const [selectedIndex, setSelectedIndex] = useState(0); | ||
| const { | ||
| state: editState, | ||
|
|
@@ -150,8 +152,27 @@ export const Tasks = ( | |
| const paginate = (pageNumber: number) => setCurrentPage(pageNumber); | ||
| const totalPages = Math.ceil(tempTasks.length / tasksPerPage) || 1; | ||
|
|
||
| useEffect(() => { | ||
| const handleGlobalPointerDown = (e: PointerEvent) => { | ||
| if (tableRef.current && !tableRef.current.contains(e.target as Node)) { | ||
| setActiveContext(null); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('pointerdown', handleGlobalPointerDown, true); | ||
| return () => { | ||
| document.removeEventListener( | ||
| 'pointerdown', | ||
| handleGlobalPointerDown, | ||
| true | ||
| ); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const handler = (e: KeyboardEvent) => { | ||
| if (!hotkeysEnabled) return; | ||
|
|
||
| const target = e.target as HTMLElement; | ||
| if ( | ||
| target instanceof HTMLInputElement || | ||
|
|
@@ -991,83 +1012,115 @@ export const Tasks = ( | |
| } | ||
| }; | ||
|
|
||
| useHotkeys(['f'], () => { | ||
| if (!showReports) { | ||
| document.getElementById('search')?.focus(); | ||
| } | ||
| }); | ||
| useHotkeys(['a'], () => { | ||
| if (!showReports) { | ||
| document.getElementById('add-new-task')?.click(); | ||
| } | ||
| }); | ||
| useHotkeys(['r'], () => { | ||
| if (!showReports) { | ||
| document.getElementById('sync-task')?.click(); | ||
| } | ||
| }); | ||
| useHotkeys(['p'], () => { | ||
| if (!showReports) { | ||
| document.getElementById('projects')?.click(); | ||
| } | ||
| }); | ||
| useHotkeys(['s'], () => { | ||
| if (!showReports) { | ||
| document.getElementById('status')?.click(); | ||
| } | ||
| }); | ||
| useHotkeys(['t'], () => { | ||
| if (!showReports) { | ||
| document.getElementById('tags')?.click(); | ||
| } | ||
| }); | ||
| useHotkeys(['c'], () => { | ||
| if (!showReports && !_isDialogOpen) { | ||
| const task = currentTasks[selectedIndex]; | ||
| if (!task) return; | ||
| const openBtn = document.getElementById(`task-row-${task.id}`); | ||
| openBtn?.click(); | ||
| setTimeout(() => { | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-complete-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| }, 200); | ||
| } else { | ||
| if (_isDialogOpen) { | ||
| useHotkeys( | ||
| ['f'], | ||
| () => { | ||
| if (!showReports) { | ||
| document.getElementById('search')?.focus(); | ||
| } | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
| useHotkeys( | ||
| ['a'], | ||
| () => { | ||
| if (!showReports) { | ||
| document.getElementById('add-new-task')?.click(); | ||
| } | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
| useHotkeys( | ||
| ['r'], | ||
| () => { | ||
| if (!showReports) { | ||
| document.getElementById('sync-task')?.click(); | ||
| } | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
| useHotkeys( | ||
| ['p'], | ||
| () => { | ||
| if (!showReports) { | ||
| document.getElementById('projects')?.click(); | ||
| } | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
| useHotkeys( | ||
| ['s'], | ||
| () => { | ||
| if (!showReports) { | ||
| document.getElementById('status')?.click(); | ||
| } | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
| useHotkeys( | ||
| ['t'], | ||
| () => { | ||
| if (!showReports) { | ||
| document.getElementById('tags')?.click(); | ||
| } | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
| useHotkeys( | ||
| ['c'], | ||
| () => { | ||
| if (!showReports && !_isDialogOpen) { | ||
| const task = currentTasks[selectedIndex]; | ||
| if (!task) return; | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-complete-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| const openBtn = document.getElementById(`task-row-${task.id}`); | ||
| openBtn?.click(); | ||
| setTimeout(() => { | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-complete-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| }, 200); | ||
| } else { | ||
| if (_isDialogOpen) { | ||
| const task = currentTasks[selectedIndex]; | ||
| if (!task) return; | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-complete-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
|
|
||
| useHotkeys(['d'], () => { | ||
| if (!showReports && !_isDialogOpen) { | ||
| const task = currentTasks[selectedIndex]; | ||
| if (!task) return; | ||
| const openBtn = document.getElementById(`task-row-${task.id}`); | ||
| openBtn?.click(); | ||
| setTimeout(() => { | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-as-deleted-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| }, 200); | ||
| } else { | ||
| if (_isDialogOpen) { | ||
| useHotkeys( | ||
| ['d'], | ||
| () => { | ||
| if (!showReports && !_isDialogOpen) { | ||
| const task = currentTasks[selectedIndex]; | ||
| if (!task) return; | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-as-deleted-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| const openBtn = document.getElementById(`task-row-${task.id}`); | ||
| openBtn?.click(); | ||
| setTimeout(() => { | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-as-deleted-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| }, 200); | ||
| } else { | ||
| if (_isDialogOpen) { | ||
| const task = currentTasks[selectedIndex]; | ||
| if (!task) return; | ||
| const confirmBtn = document.getElementById( | ||
| `mark-task-as-deleted-${task.id}` | ||
| ); | ||
| confirmBtn?.click(); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| hotkeysEnabled | ||
| ); | ||
|
|
||
| return ( | ||
| <section | ||
|
|
@@ -1128,8 +1181,10 @@ export const Tasks = ( | |
| ) : ( | ||
| <div | ||
| ref={tableRef} | ||
| onMouseEnter={() => setHotkeysEnabled(true)} | ||
| onMouseLeave={() => setHotkeysEnabled(false)} | ||
| data-testid="tasks-table-container" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added data-testid for testing hover behavior |
||
| onPointerDown={() => setActiveContext('TASKS')} | ||
| onMouseEnter={() => setIsMouseOver(true)} | ||
| onMouseLeave={() => setIsMouseOver(false)} | ||
| > | ||
| {tasks.length != 0 ? ( | ||
| <> | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests for hotkeysEnabled behavior |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests for enabled |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| import { useEffect } from 'react'; | ||
|
|
||
| export function useHotkeys(keys: string[], callback: () => void) { | ||
| export function useHotkeys( | ||
| keys: string[], | ||
| callback: () => void, | ||
| enabled: boolean = true | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enabled with default true and when it false, hook will return early without adding any event listeners |
||
| ) { | ||
| useEffect(() => { | ||
| if (!enabled) return; | ||
|
|
||
| const handler = (e: KeyboardEvent) => { | ||
| const target = e.target as HTMLElement; | ||
| if ( | ||
|
|
@@ -29,5 +35,5 @@ export function useHotkeys(keys: string[], callback: () => void) { | |
|
|
||
| window.addEventListener('keydown', handler); | ||
| return () => window.removeEventListener('keydown', handler); | ||
| }, [keys, callback]); | ||
| }, [keys, callback, enabled]); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implement checking of hotkeysEnabled state before executing