-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.jsx
243 lines (222 loc) · 7.62 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import dayGridPlugin from '@fullcalendar/daygrid';
import iCalendarPlugin from '@fullcalendar/icalendar';
import interactionPlugin from '@fullcalendar/interaction';
import FullCalendar from '@fullcalendar/react';
import rrulePlugin from '@fullcalendar/rrule';
import timeGridPlugin from '@fullcalendar/timegrid';
import { mdiCalendarRange, mdiClose, mdiMapMarkerOutline } from '@mdi/js';
import Icon from '@mdi/react';
import parse from 'html-react-parser';
import { createRef, useCallback, useMemo, useState } from 'react';
import useEscKey from './hooks/useEscKey';
import './App.css';
const htmlRegex = /<\/*html-blob>/;
function App() {
const calendarRef = createRef();
const [loading, setLoading] = useState(true);
const [showEventDetails, setShowEventDetails] = useState(false);
const [eventDetails, setEventDetails] = useState(false);
const [aspectRatio, setAspectRatio] = useState(
window.outerWidth > window.innerHeight
? 1.35
: window.innerWidth / window.innerHeight
);
const [initialView, setInitialView] = useState(
window.outerWidth > 600 ? 'dayGridMonth' : 'timeGridWeek'
);
useEscKey(() => setShowEventDetails(false));
const [popupPosition, setPopupPosition] = useState({});
const windowResize = () => {
setAspectRatio(
window.outerWidth > window.innerHeight
? 1.35
: window.innerWidth / window.innerHeight
);
setInitialView(window.outerWidth > 600 ? 'dayGridMonth' : 'timeGridWeek');
setShowEventDetails(false);
window.outerWidth < 600 && setPopupPosition({ left: 0, top: 0 });
};
const createPopupPosition = (event) => {
const popup = { width: 330, height: 400 };
let position = { top: event.pageY + 20, left: event.pageX + 50 };
if (
event.pageX + popup.width + 140 > window.outerWidth ||
event.pageY + popup.height + 20 > document.body.scrollHeight
) {
if (event.pageX + popup.width + 140 > window.outerWidth) {
position.left = event.pageX - popup.width - 50;
if (position.left < 0) position.left = position.left * -1;
}
if (event.pageY + popup.height + 20 > document.body.scrollHeight) {
position.top = event.pageY - popup.height - 70;
if (position.top < 0) position.top = position.top * -1;
}
}
setPopupPosition({ left: position.left + 'px', top: position.top + 'px' });
};
const handleEventClick = useCallback((clickInfo) => {
window.outerWidth > 600 && createPopupPosition(clickInfo.jsEvent);
setEventDetails(clickInfo.event);
setShowEventDetails(true);
}, []);
function downloadICSFile() {
// console.log("print ics");
// console.log(eventDetails.extendedProps.ics);
const file = new Blob([eventDetails.extendedProps.ics], {
type: 'text/calendar',
});
const element = document.createElement('a');
element.href = URL.createObjectURL(file);
element.download = 'finos-event.ics';
document.body.appendChild(element);
element.click();
}
const dateOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
};
const timeOptions = {
hour: '2-digit',
minute: '2-digit',
};
function printDate(date) {
if (date) {
return date.toLocaleDateString(undefined, dateOptions);
} else return 'NONE';
}
function printTime(date) {
if (date) {
const str = date.toLocaleDateString(undefined, timeOptions);
return str.split(',')[1].trim();
} else return 'NONE';
}
const renderEventDetails = () => {
let description = eventDetails.extendedProps.description
? eventDetails.extendedProps.description.replace(htmlRegex, '')
: '<i>No description</i>';
const eventLocation = eventDetails.extendedProps.location;
const fromDate = printDate(eventDetails.start);
const toDate = printDate(eventDetails.end);
const fromTime = printTime(eventDetails.start);
const toTime = printTime(eventDetails.end);
let eventTime = '';
if (fromDate == toDate) {
eventTime = fromDate + ' ' + fromTime + ' - ' + toTime;
} else {
eventTime =
<strong>From:</strong> +
fromDate +
' - ' +
toDate +
<br /> +
<strong>To:</strong> +
fromTime +
' ' +
toTime;
}
// let seriesICS = '';
// if (eventDetails.extendedProps.rootIcsLink != null) {
// seriesICS = (
// <a href={eventDetails.extendedProps.rootIcsLink}>Series ICS</a>
// );
// }
const extractUrls = (text) => {
const urlPattern = /(?<!href\s*=\s*["'])\bhttps?:\/\/\S+\b/g;
return text.match(urlPattern) || [];
};
const extractAnchors = (text) => {
const urlPattern = /<a\s+(?:[^>]*?\s+)?href="([^"]*)"[^>]*>.*?<\/a>/g;
return text.match(urlPattern) || [];
};
function replaceUrlsWithAnchorTags(inputText) {
const urls = extractUrls(inputText);
const outputText = urls.reduce((text, url) => {
const anchorTag = `<a href="${url}">${url}</a>`;
const isAlreadyAnchorTagged = new RegExp(
`<a\\s+[^>]*href\\s*=\\s*['"]?${url}['"]?[^>]*>.*?<\\/a>`
).test(text);
return isAlreadyAnchorTagged ? text : text.replace(url, anchorTag);
}, inputText);
return outputText;
}
let formattedDescription = description;
if (description) {
if (extractUrls(description).length > extractAnchors(description).length)
formattedDescription = replaceUrlsWithAnchorTags(description);
}
return (
<div className="finos-calendar-event-details" style={popupPosition}>
<button
onClick={() => setShowEventDetails(false)}
className="fc-button finos-calendar-event-details-close"
>
<Icon path={mdiClose} size={1} />
</button>
<button onClick={() => downloadICSFile()} className="fc-button">
Event ICS
</button>
{/* <div>{seriesICS}</div> */}
<h2 className="event-title">{eventDetails.title}</h2>
<div className="event-time">
<div className="icon">
<Icon path={mdiCalendarRange} size={0.75} />
</div>
<div>{eventTime}</div>
</div>
{eventLocation && (
<div className="event-location">
<div className="icon">
<Icon path={mdiMapMarkerOutline} size={0.75} />
</div>
<div>{eventLocation}</div>
</div>
)}
<br />
{parse(formattedDescription)}
</div>
);
};
const renderFullCalendar = useMemo(
() => (
<FullCalendar
ref={calendarRef}
plugins={[
dayGridPlugin,
iCalendarPlugin,
interactionPlugin,
timeGridPlugin,
rrulePlugin,
]}
initialView={initialView}
aspectRatio={aspectRatio}
handleWindowResize={true}
windowResize={windowResize}
events="events.json"
headerToolbar={{
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay',
}}
dayMaxEventRows={999}
initialDate={new Date().toISOString().slice(0, 10)}
navLinks
editable
dayMaxEvents
eventClick={handleEventClick}
loading={(isLoading) => setLoading(isLoading)}
/>
),
[aspectRatio, initialView, calendarRef, handleEventClick]
);
return (
<div className="App main">
<div className="finos-calendar">{renderFullCalendar}</div>
{showEventDetails && renderEventDetails()}
{loading && <div className="finos-calendar-overlay" />}
{loading && <div className="finos-calendar-loading">Loading...</div>}
</div>
);
}
export default App;