@@ -35,30 +35,12 @@ export const DateTime = ({
35
35
} , [ ] ) ;
36
36
37
37
const tooltipContent = (
38
- < div className = "flex flex-col gap-1" >
39
- < div className = "flex flex-col gap-2.5 pb-1" >
40
- { timeZone && timeZone !== "UTC" && (
41
- < DateTimeTooltipContent
42
- title = { timeZone }
43
- dateTime = { formatDateTime ( realDate , timeZone , locales , true , true ) }
44
- isoDateTime = { formatDateTimeISO ( realDate , timeZone ) }
45
- icon = { < GlobeAmericasIcon className = "size-4 text-purple-500" /> }
46
- />
47
- ) }
48
- < DateTimeTooltipContent
49
- title = "UTC"
50
- dateTime = { formatDateTime ( realDate , "UTC" , locales , true , true ) }
51
- isoDateTime = { formatDateTimeISO ( realDate , "UTC" ) }
52
- icon = { < GlobeAltIcon className = "size-4 text-blue-500" /> }
53
- />
54
- < DateTimeTooltipContent
55
- title = "Local"
56
- dateTime = { formatDateTime ( realDate , localTimeZone , locales , true , true ) }
57
- isoDateTime = { formatDateTimeISO ( realDate , localTimeZone ) }
58
- icon = { < Laptop className = "size-4 text-green-500" /> }
59
- />
60
- </ div >
61
- </ div >
38
+ < TooltipContent
39
+ realDate = { realDate }
40
+ timeZone = { timeZone }
41
+ localTimeZone = { localTimeZone }
42
+ locales = { locales }
43
+ />
62
44
) ;
63
45
64
46
const formattedDateTime = (
@@ -215,42 +197,48 @@ export const DateTimeAccurate = ({
215
197
date,
216
198
timeZone = "UTC" ,
217
199
previousDate = null ,
200
+ showTooltip = true ,
218
201
} : DateTimeProps ) => {
219
202
const locales = useLocales ( ) ;
203
+ const [ localTimeZone , setLocalTimeZone ] = useState < string > ( "UTC" ) ;
220
204
const realDate = typeof date === "string" ? new Date ( date ) : date ;
221
205
const realPrevDate = previousDate
222
206
? typeof previousDate === "string"
223
207
? new Date ( previousDate )
224
208
: previousDate
225
209
: null ;
226
210
227
- // Use the new Smart formatting if previousDate is provided
228
- const initialFormattedDateTime = realPrevDate
229
- ? isSameDay ( realDate , realPrevDate )
230
- ? formatTimeOnly ( realDate , timeZone , locales )
231
- : formatDateTimeAccurate ( realDate , timeZone , locales )
232
- : formatDateTimeAccurate ( realDate , timeZone , locales ) ;
233
-
234
- const [ formattedDateTime , setFormattedDateTime ] = useState < string > ( initialFormattedDateTime ) ;
235
-
236
211
useEffect ( ( ) => {
237
212
const resolvedOptions = Intl . DateTimeFormat ( ) . resolvedOptions ( ) ;
238
- const userTimeZone = resolvedOptions . timeZone ;
213
+ setLocalTimeZone ( resolvedOptions . timeZone ) ;
214
+ } , [ ] ) ;
239
215
240
- if ( realPrevDate ) {
241
- // Smart formatting based on whether date changed
242
- setFormattedDateTime (
243
- isSameDay ( realDate , realPrevDate )
244
- ? formatTimeOnly ( realDate , userTimeZone , locales )
245
- : formatDateTimeAccurate ( realDate , userTimeZone , locales )
246
- ) ;
247
- } else {
248
- // Default behavior when no previous date
249
- setFormattedDateTime ( formatDateTimeAccurate ( realDate , userTimeZone , locales ) ) ;
250
- }
251
- } , [ locales , realDate , realPrevDate ] ) ;
216
+ // Smart formatting based on whether date changed
217
+ const formattedDateTime = realPrevDate
218
+ ? isSameDay ( realDate , realPrevDate )
219
+ ? formatTimeOnly ( realDate , localTimeZone , locales )
220
+ : formatDateTimeAccurate ( realDate , localTimeZone , locales )
221
+ : formatDateTimeAccurate ( realDate , localTimeZone , locales ) ;
252
222
253
- return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
223
+ if ( ! showTooltip )
224
+ return < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > ;
225
+
226
+ const tooltipContent = (
227
+ < TooltipContent
228
+ realDate = { realDate }
229
+ timeZone = { timeZone }
230
+ localTimeZone = { localTimeZone }
231
+ locales = { locales }
232
+ />
233
+ ) ;
234
+
235
+ return (
236
+ < SimpleTooltip
237
+ button = { < Fragment > { formattedDateTime . replace ( / \s / g, String . fromCharCode ( 32 ) ) } </ Fragment > }
238
+ content = { tooltipContent }
239
+ side = "right"
240
+ />
241
+ ) ;
254
242
} ;
255
243
256
244
function formatDateTimeAccurate ( date : Date , timeZone : string , locales : string [ ] ) : string {
@@ -333,3 +321,42 @@ function DateTimeTooltipContent({
333
321
</ div >
334
322
) ;
335
323
}
324
+
325
+ function TooltipContent ( {
326
+ realDate,
327
+ timeZone,
328
+ localTimeZone,
329
+ locales,
330
+ } : {
331
+ realDate : Date ;
332
+ timeZone ?: string ;
333
+ localTimeZone : string ;
334
+ locales : string [ ] ;
335
+ } ) {
336
+ return (
337
+ < div className = "flex flex-col gap-1" >
338
+ < div className = "flex flex-col gap-2.5 pb-1" >
339
+ { timeZone && timeZone !== "UTC" && (
340
+ < DateTimeTooltipContent
341
+ title = { timeZone }
342
+ dateTime = { formatDateTime ( realDate , timeZone , locales , true , true ) }
343
+ isoDateTime = { formatDateTimeISO ( realDate , timeZone ) }
344
+ icon = { < GlobeAmericasIcon className = "size-4 text-purple-500" /> }
345
+ />
346
+ ) }
347
+ < DateTimeTooltipContent
348
+ title = "UTC"
349
+ dateTime = { formatDateTime ( realDate , "UTC" , locales , true , true ) }
350
+ isoDateTime = { formatDateTimeISO ( realDate , "UTC" ) }
351
+ icon = { < GlobeAltIcon className = "size-4 text-blue-500" /> }
352
+ />
353
+ < DateTimeTooltipContent
354
+ title = "Local"
355
+ dateTime = { formatDateTime ( realDate , localTimeZone , locales , true , true ) }
356
+ isoDateTime = { formatDateTimeISO ( realDate , localTimeZone ) }
357
+ icon = { < Laptop className = "size-4 text-green-500" /> }
358
+ />
359
+ </ div >
360
+ </ div >
361
+ ) ;
362
+ }
0 commit comments