1- import { useEffect } from "react" ;
2- import { useHotkeys } from "react-hotkeys-hook" ;
3- import { useNavigate } from "react-router-dom" ;
4- import { Key } from "ts-keycode-enum" ;
1+ import { useCallback } from "react" ;
52import {
63 SOMEDAY_MONTH_LIMIT_MSG ,
74 SOMEDAY_WEEK_LIMIT_MSG ,
85} from "@core/constants/core.constants" ;
96import { Categories_Event } from "@core/types/event.types" ;
107import { Dayjs } from "@core/util/date/dayjs" ;
11- import { ROOT_ROUTES } from "@web/common/constants/routes" ;
12- import { ID_REMINDER_INPUT } from "@web/common/constants/web.constants" ;
8+ import {
9+ useKeyDownEvent ,
10+ useKeyUpEvent ,
11+ } from "@web/common/hooks/useKeyboardEvent" ;
1312import {
1413 createAlldayDraft ,
1514 createTimedDraft ,
@@ -42,8 +41,6 @@ export interface ShortcutProps {
4241}
4342
4443export const useWeekShortcuts = ( {
45- today,
46- dateCalcs,
4744 isCurrentWeek,
4845 startOfView,
4946 endOfView,
@@ -52,22 +49,16 @@ export const useWeekShortcuts = ({
5249} : ShortcutProps ) => {
5350 const dispatch = useAppDispatch ( ) ;
5451 const context = useSidebarContext ( true ) ;
55- const navigate = useNavigate ( ) ;
5652
5753 const isAtMonthlyLimit = useAppSelector ( selectIsAtMonthlyLimit ) ;
5854 const isAtWeeklyLimit = useAppSelector ( selectIsAtWeeklyLimit ) ;
5955 const tab = useAppSelector ( selectSidebarTab ) ;
6056 const isSidebarOpen = useAppSelector ( selectIsSidebarOpen ) ;
57+ const { decrementWeek, incrementWeek, goToToday } = util ;
58+ const { scrollToNow } = scrollUtil ;
6159
62- useHotkeys ( "shift+1" , ( ) => {
63- dispatch ( viewSlice . actions . updateSidebarTab ( "tasks" ) ) ;
64- } ) ;
65- useHotkeys ( "shift+2" , ( ) => {
66- dispatch ( viewSlice . actions . updateSidebarTab ( "monthWidget" ) ) ;
67- } ) ;
68-
69- useEffect ( ( ) => {
70- const _createSomedayDraft = async (
60+ const _createSomedayDraft = useCallback (
61+ async (
7162 category : Categories_Event . SOMEDAY_WEEK | Categories_Event . SOMEDAY_MONTH ,
7263 ) => {
7364 if ( category === Categories_Event . SOMEDAY_WEEK && isAtWeeklyLimit ) {
@@ -89,86 +80,73 @@ export const useWeekShortcuts = ({
8980 if ( tab !== "tasks" ) {
9081 dispatch ( viewSlice . actions . updateSidebarTab ( "tasks" ) ) ;
9182 }
92- } ;
83+ } ,
84+ [ context , isAtMonthlyLimit , isAtWeeklyLimit , isSidebarOpen , tab , dispatch ] ,
85+ ) ;
9386
94- const _discardDraft = ( ) => {
95- if ( isEventFormOpen ( ) ) {
96- dispatch ( draftSlice . actions . discard ( undefined ) ) ;
97- }
98- } ;
99-
100- const keyDownHandler = ( e : KeyboardEvent ) => {
101- // Prevent shortcuts from triggering unexpectedly
102- if ( isEventFormOpen ( ) ) return ;
103-
104- const isEditingHeader =
105- document . getElementById ( ID_REMINDER_INPUT ) !== null ;
106- if ( isEditingHeader ) return ;
107-
108- const isCmdPaletteOpen =
109- document . getElementById ( "headlessui-portal-root" ) !== null ;
110- if ( isCmdPaletteOpen ) return ;
111-
112- if ( e . metaKey ) return ;
113-
114- // map shortcuts to handler
115- const handlersByKey = {
116- [ Key . OpenBracket ] : ( ) => dispatch ( viewSlice . actions . toggleSidebar ( ) ) ,
117- [ Key . C ] : ( ) =>
118- createTimedDraft (
119- isCurrentWeek ,
120- startOfView ,
121- "createShortcut" ,
122- dispatch ,
123- ) ,
124- [ Key . A ] : ( ) => {
125- createAlldayDraft ( startOfView , endOfView , "createShortcut" , dispatch ) ;
126- } ,
127- [ Key . T ] : ( ) => {
128- scrollUtil . scrollToNow ( ) ;
129- _discardDraft ( ) ;
130- util . goToToday ( ) ;
131- } ,
132- [ Key . J ] : ( ) => {
133- _discardDraft ( ) ;
134- util . decrementWeek ( ) ;
135- } ,
136- [ Key . K ] : ( ) => {
137- _discardDraft ( ) ;
138- util . incrementWeek ( ) ;
139- } ,
140- [ Key . M ] : ( ) => _createSomedayDraft ( Categories_Event . SOMEDAY_MONTH ) ,
141- [ Key . W ] : ( ) => _createSomedayDraft ( Categories_Event . SOMEDAY_WEEK ) ,
142- [ Key . Z ] : ( ) => {
143- navigate ( ROOT_ROUTES . LOGOUT ) ;
144- } ,
145- } as { [ key : number ] : ( ) => void } ;
146-
147- const handler = handlersByKey [ e . which ] ;
148- if ( ! handler ) return ;
149-
150- setTimeout ( handler ) ;
151- } ;
152-
153- document . addEventListener ( "keydown" , keyDownHandler ) ;
154-
155- return ( ) => {
156- document . removeEventListener ( "keydown" , keyDownHandler ) ;
157- } ;
158- } , [
159- dateCalcs ,
160- dispatch ,
161- today ,
162- isAtMonthlyLimit ,
163- isAtWeeklyLimit ,
164- isCurrentWeek ,
165- navigate ,
166- startOfView ,
167- endOfView ,
168- scrollUtil ,
169- util ,
170- tab ,
171- context ,
172- isSidebarOpen ,
173- ] ) ;
87+ const _discardDraft = useCallback ( ( ) => {
88+ if ( isEventFormOpen ( ) ) {
89+ dispatch ( draftSlice . actions . discard ( undefined ) ) ;
90+ }
91+ } , [ dispatch ] ) ;
92+
93+ const openTasks = useCallback ( ( ) => {
94+ dispatch ( viewSlice . actions . updateSidebarTab ( "tasks" ) ) ;
95+ } , [ dispatch ] ) ;
96+
97+ const openMonthWidget = useCallback ( ( ) => {
98+ dispatch ( viewSlice . actions . updateSidebarTab ( "monthWidget" ) ) ;
99+ } , [ dispatch ] ) ;
100+
101+ const goToPreviousWeek = useCallback ( ( ) => {
102+ _discardDraft ( ) ;
103+ decrementWeek ( ) ;
104+ } , [ decrementWeek , _discardDraft ] ) ;
105+
106+ const toToday = useCallback ( ( ) => {
107+ scrollToNow ( ) ;
108+ _discardDraft ( ) ;
109+ goToToday ( ) ;
110+ } , [ scrollToNow , _discardDraft , goToToday ] ) ;
111+
112+ const goToNextWeek = useCallback ( ( ) => {
113+ _discardDraft ( ) ;
114+ incrementWeek ( ) ;
115+ } , [ incrementWeek , _discardDraft ] ) ;
116+
117+ const openSidebar = useCallback (
118+ ( ) => dispatch ( viewSlice . actions . toggleSidebar ( ) ) ,
119+ [ dispatch ] ,
120+ ) ;
121+
122+ const createAllDayDraftEvent = useCallback ( ( ) => {
123+ createAlldayDraft ( startOfView , endOfView , "createShortcut" , dispatch ) ;
124+ } , [ dispatch , startOfView , endOfView ] ) ;
125+
126+ const createTimedDraftEvent = useCallback (
127+ ( ) =>
128+ createTimedDraft ( isCurrentWeek , startOfView , "createShortcut" , dispatch ) ,
129+ [ isCurrentWeek , startOfView , dispatch ] ,
130+ ) ;
131+
132+ const createSomedayMonthDraft = useCallback ( ( ) => {
133+ _createSomedayDraft ( Categories_Event . SOMEDAY_MONTH ) ;
134+ } , [ _createSomedayDraft ] ) ;
135+
136+ const createSomedayWeekDraft = useCallback ( ( ) => {
137+ _createSomedayDraft ( Categories_Event . SOMEDAY_WEEK ) ;
138+ } , [ _createSomedayDraft ] ) ;
139+
140+ useKeyDownEvent ( { combination : [ "Shift" , "1" ] , handler : openTasks } ) ;
141+ useKeyDownEvent ( { combination : [ "Shift" , "2" ] , handler : openMonthWidget } ) ;
142+ useKeyDownEvent ( { combination : [ "Shift" , "!" ] , handler : openTasks } ) ;
143+ useKeyDownEvent ( { combination : [ "Shift" , "@" ] , handler : openMonthWidget } ) ;
144+ useKeyUpEvent ( { combination : [ "[" ] , handler : openSidebar } ) ;
145+ useKeyUpEvent ( { combination : [ "j" ] , handler : goToPreviousWeek } ) ;
146+ useKeyUpEvent ( { combination : [ "k" ] , handler : goToNextWeek } ) ;
147+ useKeyUpEvent ( { combination : [ "t" ] , handler : toToday } ) ;
148+ useKeyUpEvent ( { combination : [ "a" ] , handler : createAllDayDraftEvent } ) ;
149+ useKeyUpEvent ( { combination : [ "c" ] , handler : createTimedDraftEvent } ) ;
150+ useKeyUpEvent ( { combination : [ "m" ] , handler : createSomedayMonthDraft } ) ;
151+ useKeyUpEvent ( { combination : [ "w" ] , handler : createSomedayWeekDraft } ) ;
174152} ;
0 commit comments