Skip to content

Commit 04b9973

Browse files
authored
Shows the tooltip date time on DateTImeAccurate component (#2021)
1 parent 8da3043 commit 04b9973

File tree

1 file changed

+74
-47
lines changed

1 file changed

+74
-47
lines changed

apps/webapp/app/components/primitives/DateTime.tsx

+74-47
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,12 @@ export const DateTime = ({
3535
}, []);
3636

3737
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+
/>
6244
);
6345

6446
const formattedDateTime = (
@@ -215,42 +197,48 @@ export const DateTimeAccurate = ({
215197
date,
216198
timeZone = "UTC",
217199
previousDate = null,
200+
showTooltip = true,
218201
}: DateTimeProps) => {
219202
const locales = useLocales();
203+
const [localTimeZone, setLocalTimeZone] = useState<string>("UTC");
220204
const realDate = typeof date === "string" ? new Date(date) : date;
221205
const realPrevDate = previousDate
222206
? typeof previousDate === "string"
223207
? new Date(previousDate)
224208
: previousDate
225209
: null;
226210

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-
236211
useEffect(() => {
237212
const resolvedOptions = Intl.DateTimeFormat().resolvedOptions();
238-
const userTimeZone = resolvedOptions.timeZone;
213+
setLocalTimeZone(resolvedOptions.timeZone);
214+
}, []);
239215

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);
252222

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+
);
254242
};
255243

256244
function formatDateTimeAccurate(date: Date, timeZone: string, locales: string[]): string {
@@ -333,3 +321,42 @@ function DateTimeTooltipContent({
333321
</div>
334322
);
335323
}
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

Comments
 (0)