@@ -16,7 +16,8 @@ import {
1616 subWeeks ,
1717} from 'date-fns' ;
1818import { ChevronLeftIcon , ChevronRightIcon } from 'lucide-react' ;
19- import { useActionState , useState } from 'react' ;
19+ import { useSearchParams } from 'next/navigation' ;
20+ import { useActionState , useMemo , useState } from 'react' ;
2021import { useForm } from 'react-hook-form' ;
2122import { toast } from 'sonner' ;
2223import { z } from 'zod/v4' ;
@@ -54,6 +55,7 @@ const today = getNext30Minutes();
5455
5556export default function Events ( { events, userOrgs } : EventsProps ) {
5657 const isAuthenticated = useAuth ( ) ;
58+ const searchParams = useSearchParams ( ) ;
5759
5860 const [ createEventDate , setCreateEventDate ] = useState < Date | null > ( null ) ;
5961 const [ selectedEvent , setSelectedEvent ] = useState < EventView | null > ( null ) ;
@@ -62,7 +64,21 @@ export default function Events({ events, userOrgs }: EventsProps) {
6264
6365 const [ viewMode , setViewMode ] = useState < 'MONTH' | 'WEEK' > ( 'MONTH' ) ;
6466
65- const [ currentDate , setCurrentDate ] = useState < Date > ( new Date ( ) ) ;
67+ const currentDate = useMemo ( ( ) => {
68+ let date = new Date ( ) ;
69+ try {
70+ let searchParamsDate : number | undefined = undefined ;
71+ const searchParamsMonth = searchParams . get ( 'month' ) ;
72+ if ( searchParamsMonth ) searchParamsDate = Date . parse ( searchParamsMonth ) ;
73+
74+ const searchParamsWeek = searchParams . get ( 'date' ) ;
75+ if ( searchParamsWeek ) searchParamsDate = Date . parse ( searchParamsWeek ) ;
76+
77+ if ( searchParamsDate !== undefined && ! isNaN ( searchParamsDate ) )
78+ date = new Date ( searchParamsDate ) ;
79+ } catch { }
80+ return date ;
81+ } , [ searchParams ] ) ;
6682
6783 const [ isDailyViewOpen , setIsDailyViewOpen ] = useState ( false ) ;
6884
@@ -198,15 +214,41 @@ export default function Events({ events, userOrgs }: EventsProps) {
198214 form . setValue ( 'endTime' , addHours ( date , 2 ) ) ;
199215 } ;
200216
201- const handlePrevious = ( ) =>
202- setCurrentDate ( ( prev ) =>
203- viewMode === 'MONTH' ? subMonths ( prev , 1 ) : subWeeks ( prev , 1 ) ,
217+ const handlePrevious = ( ) => {
218+ const newDate =
219+ viewMode === 'MONTH'
220+ ? subMonths ( currentDate , 1 )
221+ : subWeeks ( currentDate , 1 ) ;
222+ const params = new URLSearchParams (
223+ viewMode === 'MONTH'
224+ ? {
225+ [ viewMode . toLowerCase ( ) ] : newDate . toLocaleString ( 'en-sg' , {
226+ month : 'long' ,
227+ year : 'numeric' ,
228+ } ) ,
229+ }
230+ : { date : newDate . toDateString ( ) } ,
204231 ) ;
232+ window . history . pushState ( null , '' , `?${ params . toString ( ) } ` ) ;
233+ } ;
205234
206- const handleNext = ( ) =>
207- setCurrentDate ( ( prev ) =>
208- viewMode === 'MONTH' ? addMonths ( prev , 1 ) : addWeeks ( prev , 1 ) ,
235+ const handleNext = ( ) => {
236+ const newDate =
237+ viewMode === 'MONTH'
238+ ? addMonths ( currentDate , 1 )
239+ : addWeeks ( currentDate , 1 ) ;
240+ const params = new URLSearchParams (
241+ viewMode === 'MONTH'
242+ ? {
243+ [ viewMode . toLowerCase ( ) ] : newDate . toLocaleString ( 'en-sg' , {
244+ month : 'long' ,
245+ year : 'numeric' ,
246+ } ) ,
247+ }
248+ : { date : newDate . toDateString ( ) } ,
209249 ) ;
250+ window . history . pushState ( null , '' , `?${ params . toString ( ) } ` ) ;
251+ } ;
210252
211253 const calendarStart = startOfWeek ( startOfMonth ( currentDate ) , {
212254 weekStartsOn : 1 ,
@@ -321,7 +363,19 @@ export default function Events({ events, userOrgs }: EventsProps) {
321363 < div className = 'relative grid grid-cols-7 gap-1' >
322364 < CalendarGrid
323365 currentDate = { currentDate }
324- setCurrentDate = { setCurrentDate }
366+ // TODO: Update this
367+ setCurrentDate = { ( newDate ) => {
368+ if ( viewMode === 'WEEK' ) {
369+ const params = new URLSearchParams ( {
370+ date : newDate . toDateString ( ) ,
371+ } ) ;
372+ window . history . pushState (
373+ null ,
374+ '' ,
375+ `?${ params . toString ( ) } ` ,
376+ ) ;
377+ }
378+ } }
325379 viewMode = { viewMode }
326380 // TODO: This is problematic
327381 calendarDays = { calendarDays }
@@ -382,15 +436,15 @@ export default function Events({ events, userOrgs }: EventsProps) {
382436 < Button
383437 variant = 'ghost'
384438 size = 'icon'
385- className = 'h-[38px] w-[38px] rounded-full bg-[#FF7D4E] text-white hover:bg-[#FF7D4E]/90'
439+ className = 'h-9.5 w-9.5 rounded-full bg-[#FF7D4E] text-white hover:bg-[#FF7D4E]/90'
386440 onClick = { handlePrevious }
387441 >
388442 < ChevronLeftIcon className = 'h-4 w-4' />
389443 </ Button >
390444 < Button
391445 variant = 'ghost'
392446 size = 'icon'
393- className = 'h-[38px] w-[38px] rounded-full bg-[#FF7D4E] text-white hover:bg-[#FF7D4E]/90'
447+ className = 'h-9.5 w-9.5 rounded-full bg-[#FF7D4E] text-white hover:bg-[#FF7D4E]/90'
394448 onClick = { handleNext }
395449 >
396450 < ChevronRightIcon className = 'h-4 w-4' />
0 commit comments