Skip to content

Commit ef5463a

Browse files
committed
feat: localize community event times and fix serialization bug (#2283)
- Implemented LocalizedTime component using Intl API\n- Handled Next.js hydration for client-side timezone detection\n- Fixed serialization error in getStaticProps when calendar fetch fails
1 parent 22ea307 commit ef5463a

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

components/LocalizedTime.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@ const LocalizedTime: React.FC<LocalizedTimeProps> = ({ utcTime, utcDate }) => {
1010

1111
useEffect(() => {
1212
try {
13-
// Convert "YYYY-MM-DD HH:mm:ss" to ISO "YYYY-MM-DDTHH:mm:ssZ"
1413
const dateStr = utcDate.replace(' ', 'T') + 'Z';
1514
const date = new Date(dateStr);
16-
1715
if (isNaN(date.getTime())) return;
1816

1917
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
20-
21-
// Don't show if user is already in UTC to avoid redundancy
2218
if (userTimeZone === 'UTC' || userTimeZone === 'Etc/UTC') return;
2319

24-
const formatted = new Intl.DateTimeFormat(undefined, {
20+
const options: Intl.DateTimeFormatOptions = {
2521
hour: '2-digit',
2622
minute: '2-digit',
2723
timeZoneName: 'short',
28-
}).format(date);
24+
};
25+
26+
// Check if the date changed locally (e.g., event is 11 PM UTC, but 4 AM next day local)
27+
if (new Date(dateStr).getUTCDate() !== date.getDate()) {
28+
options.month = 'short';
29+
options.day = 'numeric';
30+
}
2931

32+
const formatted = new Intl.DateTimeFormat(undefined, options).format(
33+
date,
34+
);
3035
setLocalized(formatted);
3136
} catch (err) {
3237
console.error('Error localizing time:', err);
@@ -36,7 +41,11 @@ const LocalizedTime: React.FC<LocalizedTimeProps> = ({ utcTime, utcDate }) => {
3641
return (
3742
<>
3843
{utcTime} UTC
39-
{localized && <span> ({localized})</span>}
44+
{localized && (
45+
<span className='text-blue-600 dark:text-blue-400 font-semibold ml-1'>
46+
({localized})
47+
</span>
48+
)}
4049
</>
4150
);
4251
};

lib/calendarUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function printEventsForNextWeeks(icalData: { [x: string]: any }) {
1717
const arrayDates = [];
1818
if (!icalData) {
1919
console.error('iCal data is empty or invalid.');
20-
return;
20+
return [];
2121
}
2222

2323
// Calculate the range of dates for the next 12 weeks from today

pages/community/index.page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const getStaticProps: GetStaticProps = async () => {
4747
return {
4848
props: {
4949
blogPosts,
50-
datesInfo,
50+
datesInfo: datesInfo ?? [],
5151
fallback: false,
5252
},
5353
};

0 commit comments

Comments
 (0)