@@ -10,10 +10,24 @@ import {
1010import { BsRssFill , BsApple , BsGoogle , BsMicrosoft } from "react-icons/bs" ;
1111import { CiCalendarDate } from "react-icons/ci" ;
1212
13+ function getHTTPOfURL ( url : string ) {
14+ return url . replace ( / ^ h t t p s ? : \/ \/ / , "http://" ) ;
15+ }
16+
17+ function getURLWithoutProtocol ( url : string ) {
18+ return url . replace ( / ^ h t t p s ? : \/ \/ / , "" ) ;
19+ }
20+
21+ const calendarURL = `${ process . env . URL || process . env . NEXT_PUBLIC_BASE_URL } /api/calendar` ;
22+ const urls = {
23+ google : `https://calendar.google.com/calendar/render?cid=${ encodeURIComponent ( getHTTPOfURL ( calendarURL ) ) } ` ,
24+ outlook : `https://outlook.live.com/calendar/0/addcalendar?url=${ encodeURIComponent ( "webcal://" + getURLWithoutProtocol ( calendarURL ) ) } ` ,
25+ apple : `webcal://${ encodeURIComponent ( getURLWithoutProtocol ( calendarURL ) ) } ` ,
26+ download : calendarURL ,
27+ } ;
28+
1329const AddToCalendar = ( ) => {
1430 const [ isOpen , setIsOpen ] = useState ( false ) ;
15- const calendarURL = `${ process . env . NEXT_PUBLIC_BASE_URL } /api/calendar` ;
16- console . log ( "🚀 ~ AddToCalendar ~ calendarURL:" , calendarURL ) ;
1731
1832 return (
1933 < >
@@ -51,7 +65,7 @@ const AddToCalendar = () => {
5165
5266 < div className = "mt-4" >
5367 < a
54- href = { `https://calendar .google.com/calendar/render?cid= ${ encodeURIComponent ( getHTTPOfURL ( calendarURL ) ) } ` }
68+ href = { urls . google }
5569 target = "_blank"
5670 rel = "noopener noreferrer"
5771 className = "flex items-center px-4 py-3 mt-2 text-sm font-bold text-white bg-blue-500 rounded-md hover:bg-blue-700"
@@ -60,26 +74,27 @@ const AddToCalendar = () => {
6074 Google Calendar
6175 </ a >
6276 < a
63- href = { `https://outlook.live.com/calendar/0/addcalendar?url=${ encodeURIComponent ( "webcal://" + calendarURL ) } ` }
77+ href = { urls . outlook }
78+ target = "_blank"
79+ rel = "noopener noreferrer"
6480 className = "flex items-center px-4 py-3 mt-2 text-sm font-bold text-white bg-blue-600 rounded-md hover:bg-blue-800"
6581 >
6682 < BsMicrosoft className = "w-4 h-4 mr-2" />
6783 Outlook Calendar
6884 </ a >
6985 < a
70- href = { `webcal:// ${ encodeURIComponent ( calendarURL ) } ` }
86+ href = { urls . apple }
7187 className = "flex items-center px-4 py-3 mt-2 text-sm font-bold text-white bg-gray-900 rounded-md hover:bg-gray-700"
7288 >
7389 < BsApple className = "w-4 h-4 mr-2" />
7490 Apple Calendar
7591 </ a >
7692 < a
77- href = { calendarURL }
93+ href = { urls . download }
7894 download = "calendar.ics"
7995 className = "text-center block px-4 py-2 mt-2 text-sm font-medium text-gray-500 hover:text-gray-300"
8096 >
81- If the links above didn't work, click here to download .ics
82- file and add it manually to your calendar app.
97+ Click here to download .ics file instead
8398 </ a >
8499 </ div >
85100 </ div >
@@ -89,8 +104,4 @@ const AddToCalendar = () => {
89104 ) ;
90105} ;
91106
92- function getHTTPOfURL ( url : string ) {
93- return url . replace ( / ^ h t t p s ? : \/ \/ / , "http://" ) ;
94- }
95-
96107export default AddToCalendar ;
0 commit comments