@@ -8,7 +8,8 @@ import { isDurationString, parseDurationString } from '@perses-dev/core';
88const parseTimeExpr = ( expr : string , now : Date ) : Date | undefined => {
99 const match = expr . match ( / ^ N O W (?: - ( .+ ) ) ? $ / ) ;
1010 if ( ! match ) {
11- return new Date ( expr ) ;
11+ const parsed = new Date ( expr ) ;
12+ return Number . isNaN ( parsed . getTime ( ) ) ? undefined : parsed ;
1213 }
1314 if ( ! match [ 1 ] ) {
1415 return now ;
@@ -22,17 +23,18 @@ const parseTimeExpr = (expr: string, now: Date): Date | undefined => {
2223export const useTimeRange = ( start ?: string , end ?: string , duration ?: string ) : TimeRangeValue => {
2324 return useMemo ( ( ) => {
2425 const now = new Date ( ) ;
26+ const safeDuration = duration && isDurationString ( duration ) ? duration : '1h' ;
2527 const startDate = start ? parseTimeExpr ( start , now ) : undefined ;
2628 const endDate = end ? parseTimeExpr ( end , now ) : undefined ;
2729
2830 // If end is exactly "NOW" with no offset and no explicit start, use relative time range
2931 if ( end === 'NOW' && ! start ) {
30- return { pastDuration : duration || '1h' } as RelativeTimeRange ;
32+ return { pastDuration : safeDuration } as RelativeTimeRange ;
3133 }
3234
3335 if ( startDate && endDate ) {
3436 return { start : startDate , end : endDate } as AbsoluteTimeRange ;
3537 }
36- return { pastDuration : duration || '1h' } as RelativeTimeRange ;
38+ return { pastDuration : safeDuration } as RelativeTimeRange ;
3739 } , [ duration , end , start ] ) ;
3840} ;
0 commit comments