11import { formatDistanceToNow , isValid } from "date-fns" ;
22import { useEffect , useMemo , useState } from "react" ;
3- import { authClient } from "~/client/lib/auth-client" ;
43import { Route as RootRoute } from "~/routes/__root" ;
54
65export type DateInput = Date | string | number | null | undefined ;
@@ -64,6 +63,7 @@ function getRequiredPart(parts: Intl.DateTimeFormatPart[], type: Intl.DateTimeFo
6463
6564function formatConfiguredDate ( date : Date , options : DateFormatOptions , includeYear : boolean ) {
6665 const dateFormat = options . dateFormat ?? DEFAULT_DATE_FORMAT ;
66+ const safeDateFormat = DATE_FORMATS . includes ( dateFormat ) ? dateFormat : DEFAULT_DATE_FORMAT ;
6767 const parts = getDateTimeFormat ( options . locale , options . timeZone , {
6868 month : "numeric" ,
6969 day : "numeric" ,
@@ -74,7 +74,7 @@ function formatConfiguredDate(date: Date, options: DateFormatOptions, includeYea
7474 day : getRequiredPart ( parts , "day" ) ,
7575 year : getRequiredPart ( parts , "year" ) ,
7676 } ;
77- const order = includeYear ? DATE_PART_ORDERS [ dateFormat ] : SHORT_DATE_PART_ORDERS [ dateFormat ] ;
77+ const order = includeYear ? DATE_PART_ORDERS [ safeDateFormat ] : SHORT_DATE_PART_ORDERS [ safeDateFormat ] ;
7878
7979 return order . map ( ( part ) => values [ part ] ) . join ( "/" ) ;
8080}
@@ -163,10 +163,7 @@ export function formatTimeAgo(date: DateInput, now = Date.now()): string {
163163
164164export function useTimeFormat ( ) {
165165 const { locale, timeZone, dateFormat, timeFormat, now } = RootRoute . useLoaderData ( ) ;
166- const session = authClient . useSession ( ) ;
167166 const [ currentNow , setCurrentNow ] = useState ( now ) ;
168- const currentDateFormat = ( session . data ?. user . dateFormat ?? dateFormat ) as DateFormatPreference ;
169- const currentTimeFormat = ( session . data ?. user . timeFormat ?? timeFormat ) as TimeFormatPreference ;
170167
171168 useEffect ( ( ) => {
172169 const nextNow = Date . now ( ) ;
@@ -175,20 +172,14 @@ export function useTimeFormat() {
175172
176173 return useMemo (
177174 ( ) => ( {
178- formatDateTime : ( date : DateInput ) =>
179- formatDateTime ( date , { locale, timeZone, dateFormat : currentDateFormat , timeFormat : currentTimeFormat } ) ,
180- formatDateWithMonth : ( date : DateInput ) =>
181- formatDateWithMonth ( date , { locale, timeZone, dateFormat : currentDateFormat , timeFormat : currentTimeFormat } ) ,
182- formatDate : ( date : DateInput ) =>
183- formatDate ( date , { locale, timeZone, dateFormat : currentDateFormat , timeFormat : currentTimeFormat } ) ,
184- formatShortDate : ( date : DateInput ) =>
185- formatShortDate ( date , { locale, timeZone, dateFormat : currentDateFormat , timeFormat : currentTimeFormat } ) ,
186- formatShortDateTime : ( date : DateInput ) =>
187- formatShortDateTime ( date , { locale, timeZone, dateFormat : currentDateFormat , timeFormat : currentTimeFormat } ) ,
188- formatTime : ( date : DateInput ) =>
189- formatTime ( date , { locale, timeZone, dateFormat : currentDateFormat , timeFormat : currentTimeFormat } ) ,
175+ formatDateTime : ( date : DateInput ) => formatDateTime ( date , { locale, timeZone, dateFormat, timeFormat } ) ,
176+ formatDateWithMonth : ( date : DateInput ) => formatDateWithMonth ( date , { locale, timeZone, dateFormat, timeFormat } ) ,
177+ formatDate : ( date : DateInput ) => formatDate ( date , { locale, timeZone, dateFormat, timeFormat } ) ,
178+ formatShortDate : ( date : DateInput ) => formatShortDate ( date , { locale, timeZone, dateFormat, timeFormat } ) ,
179+ formatShortDateTime : ( date : DateInput ) => formatShortDateTime ( date , { locale, timeZone, dateFormat, timeFormat } ) ,
180+ formatTime : ( date : DateInput ) => formatTime ( date , { locale, timeZone, dateFormat, timeFormat } ) ,
190181 formatTimeAgo : ( date : DateInput ) => formatTimeAgo ( date , currentNow ) ,
191182 } ) ,
192- [ locale , timeZone , currentDateFormat , currentTimeFormat , currentNow ] ,
183+ [ locale , timeZone , currentNow , dateFormat , timeFormat ] ,
193184 ) ;
194185}
0 commit comments