Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 76
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?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
its-me-abhishek marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -116,7 +116,9 @@ export const Tasks = ( | ||
| ); | ||
| const [autoSyncOnEdit, setAutoSyncOnEdit] = useState(true); | ||
| 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, | ||
| @@ -151,8 +153,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 || | ||
| @@ -1022,83 +1043,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 | ||
| @@ -1159,8 +1212,10 @@ export const Tasks = ( | ||
| ) : ( | ||
| <div | ||
| ref={tableRef} | ||
| onMouseEnter={() => setHotkeysEnabled(true)} | ||
| onMouseLeave={() => setHotkeysEnabled(false)} | ||
| data-testid="tasks-table-container" | ||
its-me-abhishek marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| onPointerDown={() => setActiveContext('TASKS')} | ||
| onMouseEnter={() => setIsMouseOver(true)} | ||
| onMouseLeave={() => setIsMouseOver(false)} | ||
| > | ||
| {tasks.length != 0 ? ( | ||
| <> | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.