Spaces:
Running
Running
gpt-engineer-app[bot]
commited on
Commit
·
49c9b4d
1
Parent(s):
b37da4d
Fix invalid time value error
Browse filesFixes a RangeError: Invalid time value in ConferenceDialog.tsx when creating calendar events.
src/components/ConferenceDialog.tsx
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
| 7 |
} from "@/components/ui/dialog";
|
| 8 |
import { CalendarDays, Globe, Tag, Clock, AlarmClock, CalendarPlus } from "lucide-react";
|
| 9 |
import { Conference } from "@/types/conference";
|
| 10 |
-
import { formatDistanceToNow, parseISO, isValid, format } from "date-fns";
|
| 11 |
import { Button } from "@/components/ui/button";
|
| 12 |
import {
|
| 13 |
DropdownMenu,
|
|
@@ -34,37 +34,67 @@ const ConferenceDialog = ({ conference, open, onOpenChange }: ConferenceDialogPr
|
|
| 34 |
return "text-green-600";
|
| 35 |
};
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
const createCalendarEvent = (type: 'google' | 'apple') => {
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
VERSION:2.0
|
| 69 |
BEGIN:VEVENT
|
| 70 |
URL:${conference.link || ''}
|
|
@@ -75,13 +105,16 @@ DESCRIPTION:${description}
|
|
| 75 |
LOCATION:${location}
|
| 76 |
END:VEVENT
|
| 77 |
END:VCALENDAR`;
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
};
|
| 87 |
|
|
|
|
| 7 |
} from "@/components/ui/dialog";
|
| 8 |
import { CalendarDays, Globe, Tag, Clock, AlarmClock, CalendarPlus } from "lucide-react";
|
| 9 |
import { Conference } from "@/types/conference";
|
| 10 |
+
import { formatDistanceToNow, parseISO, isValid, format, parse } from "date-fns";
|
| 11 |
import { Button } from "@/components/ui/button";
|
| 12 |
import {
|
| 13 |
DropdownMenu,
|
|
|
|
| 34 |
return "text-green-600";
|
| 35 |
};
|
| 36 |
|
| 37 |
+
const parseDateFromString = (dateStr: string) => {
|
| 38 |
+
try {
|
| 39 |
+
// Handle formats like "October 19-25, 2025" or "Sept 9-12, 2025"
|
| 40 |
+
const [monthDay, year] = dateStr.split(", ");
|
| 41 |
+
const [month, dayRange] = monthDay.split(" ");
|
| 42 |
+
const [startDay] = dayRange.split("-");
|
| 43 |
+
|
| 44 |
+
// Construct a date string in a format that can be parsed
|
| 45 |
+
const dateString = `${month} ${startDay} ${year}`;
|
| 46 |
+
const date = parse(dateString, 'MMMM d yyyy', new Date());
|
| 47 |
+
|
| 48 |
+
if (!isValid(date)) {
|
| 49 |
+
// Try alternative format for abbreviated months
|
| 50 |
+
return parse(dateString, 'MMM d yyyy', new Date());
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
return date;
|
| 54 |
+
} catch (error) {
|
| 55 |
+
console.error("Error parsing date:", error);
|
| 56 |
+
return new Date();
|
| 57 |
+
}
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
const createCalendarEvent = (type: 'google' | 'apple') => {
|
| 61 |
+
try {
|
| 62 |
+
let startDate = new Date();
|
| 63 |
+
let endDate = new Date();
|
| 64 |
+
|
| 65 |
+
if (conference.start && conference.end) {
|
| 66 |
+
startDate = parseISO(conference.start);
|
| 67 |
+
endDate = parseISO(conference.end);
|
| 68 |
+
} else {
|
| 69 |
+
startDate = parseDateFromString(conference.date);
|
| 70 |
+
// Set end date to the same day for single-day conferences or when end date is not clear
|
| 71 |
+
endDate = new Date(startDate);
|
| 72 |
+
endDate.setDate(endDate.getDate() + 1); // Add one day for end date
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const formatDateForGoogle = (date: Date) => format(date, "yyyyMMdd");
|
| 76 |
+
const formatDateForApple = (date: Date) => format(date, "yyyyMMdd'T'HHmmss");
|
| 77 |
+
|
| 78 |
+
const title = encodeURIComponent(conference.title);
|
| 79 |
+
const location = encodeURIComponent(conference.place);
|
| 80 |
+
const description = encodeURIComponent(
|
| 81 |
+
`Conference: ${conference.full_name || conference.title}\n` +
|
| 82 |
+
`Location: ${conference.place}\n` +
|
| 83 |
+
`Deadline: ${conference.deadline}\n` +
|
| 84 |
+
(conference.abstract_deadline ? `Abstract Deadline: ${conference.abstract_deadline}\n` : '') +
|
| 85 |
+
(conference.link ? `Website: ${conference.link}` : '')
|
| 86 |
+
);
|
| 87 |
+
|
| 88 |
+
if (type === 'google') {
|
| 89 |
+
const url = `https://calendar.google.com/calendar/render?action=TEMPLATE` +
|
| 90 |
+
`&text=${title}` +
|
| 91 |
+
`&dates=${formatDateForGoogle(startDate)}/${formatDateForGoogle(endDate)}` +
|
| 92 |
+
`&details=${description}` +
|
| 93 |
+
`&location=${location}` +
|
| 94 |
+
`&sprop=website:${encodeURIComponent(conference.link || '')}`;
|
| 95 |
+
window.open(url, '_blank');
|
| 96 |
+
} else {
|
| 97 |
+
const url = `data:text/calendar;charset=utf8,BEGIN:VCALENDAR
|
| 98 |
VERSION:2.0
|
| 99 |
BEGIN:VEVENT
|
| 100 |
URL:${conference.link || ''}
|
|
|
|
| 105 |
LOCATION:${location}
|
| 106 |
END:VEVENT
|
| 107 |
END:VCALENDAR`;
|
| 108 |
+
|
| 109 |
+
const link = document.createElement('a');
|
| 110 |
+
link.href = url;
|
| 111 |
+
link.download = `${conference.title.toLowerCase().replace(/\s+/g, '-')}.ics`;
|
| 112 |
+
document.body.appendChild(link);
|
| 113 |
+
link.click();
|
| 114 |
+
document.body.removeChild(link);
|
| 115 |
+
}
|
| 116 |
+
} catch (error) {
|
| 117 |
+
console.error("Error creating calendar event:", error);
|
| 118 |
}
|
| 119 |
};
|
| 120 |
|