@@ -22,11 +22,10 @@ import { extend } from 'dayjs';
2222import useCookie from 'react-use-cookie' ;
2323extend ( isoWeek ) ;
2424extend ( weekOfYear ) ;
25+
2526export const CalendarContext = createContext ( {
26- currentDay : dayjs ( ) . day ( ) as 0 | 1 | 2 | 3 | 4 | 5 | 6 ,
27- currentWeek : dayjs ( ) . week ( ) ,
28- currentYear : dayjs ( ) . year ( ) ,
29- currentMonth : dayjs ( ) . month ( ) ,
27+ startDate : dayjs ( ) . startOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ,
28+ endDate : dayjs ( ) . endOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ,
3029 customer : null as string | null ,
3130 sets : [ ] as { name : string ; id : string ; content : string [ ] } [ ] ,
3231 signature : undefined as any ,
@@ -51,10 +50,8 @@ export const CalendarContext = createContext({
5150 } ,
5251 display : 'week' ,
5352 setFilters : ( filters : {
54- currentWeek : number ;
55- currentYear : number ;
56- currentDay : 0 | 1 | 2 | 3 | 4 | 5 | 6 ;
57- currentMonth : number ;
53+ startDate : string ;
54+ endDate : string ;
5855 display : 'week' | 'month' | 'day' ;
5956 customer : string | null ;
6057 } ) => {
@@ -64,6 +61,7 @@ export const CalendarContext = createContext({
6461 /** empty **/
6562 } ,
6663} ) ;
64+
6765export interface Integrations {
6866 name : string ;
6967 id : string ;
@@ -85,23 +83,35 @@ export interface Integrations {
8583 id ?: string ;
8684 } ;
8785}
88- function getWeekNumber ( date : Date ) {
89- // Copy date so don't modify original
90- const targetDate = new Date (
91- Date . UTC ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) )
92- ) ;
93- // Set to nearest Thursday: current date + 4 - current day number
94- // Make Sunday's day number 7
95- targetDate . setUTCDate (
96- targetDate . getUTCDate ( ) + 4 - ( targetDate . getUTCDay ( ) || 7 )
97- ) ;
98- // Get first day of year
99- const yearStart = new Date ( Date . UTC ( targetDate . getUTCFullYear ( ) , 0 , 1 ) ) ;
100- // Calculate full weeks to nearest Thursday
101- return Math . ceil (
102- ( ( targetDate . getTime ( ) - yearStart . getTime ( ) ) / 86400000 + 1 ) / 7
103- ) ;
86+
87+ // Helper function to get start and end dates based on display type
88+ function getDateRange ( display : string , referenceDate ?: string ) {
89+ const date = referenceDate ? dayjs ( referenceDate ) : dayjs ( ) ;
90+
91+ switch ( display ) {
92+ case 'day' :
93+ return {
94+ startDate : date . format ( 'YYYY-MM-DD' ) ,
95+ endDate : date . format ( 'YYYY-MM-DD' ) ,
96+ } ;
97+ case 'week' :
98+ return {
99+ startDate : date . startOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ,
100+ endDate : date . endOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ,
101+ } ;
102+ case 'month' :
103+ return {
104+ startDate : date . startOf ( 'month' ) . format ( 'YYYY-MM-DD' ) ,
105+ endDate : date . endOf ( 'month' ) . format ( 'YYYY-MM-DD' ) ,
106+ } ;
107+ default :
108+ return {
109+ startDate : date . startOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ,
110+ endDate : date . endOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ,
111+ } ;
112+ }
104113}
114+
105115export const CalendarWeekProvider : FC < {
106116 children : ReactNode ;
107117 integrations : Integrations [ ] ;
@@ -112,35 +122,45 @@ export const CalendarWeekProvider: FC<{
112122 const searchParams = useSearchParams ( ) ;
113123 const [ displaySaved , setDisplaySaved ] = useCookie ( 'calendar-display' , 'week' ) ;
114124 const display = searchParams . get ( 'display' ) || displaySaved ;
125+
126+ // Initialize with current date range based on URL params or defaults
127+ const initStartDate = searchParams . get ( 'startDate' ) ;
128+ const initEndDate = searchParams . get ( 'endDate' ) ;
129+ const initCustomer = searchParams . get ( 'customer' ) ;
130+
131+ const initialRange =
132+ initStartDate && initEndDate
133+ ? { startDate : initStartDate , endDate : initEndDate }
134+ : getDateRange ( display ) ;
135+
115136 const [ filters , setFilters ] = useState ( {
116- currentDay : + ( searchParams . get ( 'day' ) || dayjs ( ) . day ( ) ) as
117- | 0
118- | 1
119- | 2
120- | 3
121- | 4
122- | 5
123- | 6 ,
124- currentWeek : + ( searchParams . get ( 'week' ) || getWeekNumber ( new Date ( ) ) ) ,
125- currentMonth : + ( searchParams . get ( 'month' ) || dayjs ( ) . month ( ) ) ,
126- currentYear : + ( searchParams . get ( 'year' ) || dayjs ( ) . year ( ) ) ,
127- customer : ( searchParams . get ( 'customer' ) as string ) || null ,
137+ startDate : initialRange . startDate ,
138+ endDate : initialRange . endDate ,
139+ customer : initCustomer || null ,
128140 display,
129141 } ) ;
142+
130143 const params = useMemo ( ( ) => {
131144 return new URLSearchParams ( {
132145 display : filters . display ,
133- day : filters . currentDay . toString ( ) ,
134- week : filters . currentWeek . toString ( ) ,
135- month : ( filters . currentMonth + 1 ) . toString ( ) ,
136- year : filters . currentYear . toString ( ) ,
146+ startDate : filters . startDate ,
147+ endDate : filters . endDate ,
137148 customer : filters ?. customer ?. toString ( ) || '' ,
138149 } ) . toString ( ) ;
139- } , [ filters , display ] ) ;
150+ } , [ filters ] ) ;
151+
140152 const loadData = useCallback ( async ( ) => {
141- const data = ( await fetch ( `/posts?${ params } ` ) ) . json ( ) ;
153+ const modifiedParams = new URLSearchParams ( {
154+ display : filters . display ,
155+ customer : filters ?. customer ?. toString ( ) || '' ,
156+ startDate : dayjs ( filters . startDate ) . startOf ( 'day' ) . utc ( ) . format ( ) ,
157+ endDate : dayjs ( filters . endDate ) . endOf ( 'day' ) . utc ( ) . format ( ) ,
158+ } ) . toString ( ) ;
159+
160+ const data = ( await fetch ( `/posts?${ modifiedParams } ` ) ) . json ( ) ;
142161 return data ;
143162 } , [ filters , params ] ) ;
163+
144164 const swr = useSWR ( `/posts-${ params } ` , loadData , {
145165 refreshInterval : 3600000 ,
146166 refreshWhenOffline : false ,
@@ -157,37 +177,35 @@ export const CalendarWeekProvider: FC<{
157177 } , [ ] ) ;
158178
159179 const { data : sets , mutate } = useSWR ( 'sets' , setList ) ;
160- const { data : sign } = useSWR ( 'default-sign' , defaultSign ) ;
180+ const { data : sign } = useSWR ( 'default-sign' , defaultSign ) ;
161181
162182 const setFiltersWrapper = useCallback (
163183 ( filters : {
164- currentDay : 0 | 1 | 2 | 3 | 4 | 5 | 6 ;
165- currentWeek : number ;
166- currentYear : number ;
167- currentMonth : number ;
184+ startDate : string ;
185+ endDate : string ;
168186 display : 'week' | 'month' | 'day' ;
169187 customer : string | null ;
170188 } ) => {
171189 setDisplaySaved ( filters . display ) ;
172190 setFilters ( filters ) ;
173191 setInternalData ( [ ] ) ;
174192 const path = [
175- `day=${ filters . currentDay } ` ,
176- `week=${ filters . currentWeek } ` ,
177- `month=${ filters . currentMonth } ` ,
178- `year=${ filters . currentYear } ` ,
193+ `startDate=${ filters . startDate } ` ,
194+ `endDate=${ filters . endDate } ` ,
179195 `display=${ filters . display } ` ,
180196 filters . customer ? `customer=${ filters . customer } ` : `` ,
181197 ] . filter ( ( f ) => f ) ;
182198 window . history . replaceState ( null , '' , `/launches?${ path . join ( '&' ) } ` ) ;
183199 } ,
184200 [ filters , swr . mutate ]
185201 ) ;
202+
186203 const { isLoading } = swr ;
187204 const { posts, comments } = swr ?. data || {
188205 posts : [ ] ,
189206 comments : [ ] ,
190207 } ;
208+
191209 const changeDate = useCallback (
192210 ( id : string , date : dayjs . Dayjs ) => {
193211 setInternalData ( ( d ) =>
@@ -204,11 +222,13 @@ export const CalendarWeekProvider: FC<{
204222 } ,
205223 [ posts , internalData ]
206224 ) ;
225+
207226 useEffect ( ( ) => {
208227 if ( posts ) {
209228 setInternalData ( posts ) ;
210229 }
211230 } , [ posts ] ) ;
231+
212232 return (
213233 < CalendarContext . Provider
214234 value = { {
@@ -228,4 +248,5 @@ export const CalendarWeekProvider: FC<{
228248 </ CalendarContext . Provider >
229249 ) ;
230250} ;
251+
231252export const useCalendar = ( ) => useContext ( CalendarContext ) ;
0 commit comments