import React, { useState, useEffect } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import { Calendar, momentLocalizer } from "https://esm.sh/react-big-calendar";
import moment from "https://esm.sh/moment";
const localizer = momentLocalizer(moment);
function App() {
const [events, setEvents] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const handleFileUpload = async (e) => {
const file = e.target.files[0];
if (file) {
setIsLoading(true);
const formData = new FormData();
formData.append('pdf', file);
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (Array.isArray(data)) {
setEvents(data.map(event => ({
...event,
start: new Date(event.start),
end: new Date(event.end),
})));
} else {
throw new Error('Unexpected data format received from server');
}
} catch (error) {
console.error('Error uploading file:', error);
setEvents([]);
alert('There was an error processing your file. Please try again or use a different PDF.');
} finally {
setIsLoading(false);
}
}
};
return (
<div style={{ height: '100vh', display: 'flex', flexDirection: 'column' }}>
<h1>Calendar Event Extractor</h1>
<input type="file" accept=".pdf" onChange={handleFileUpload} />
{isLoading && <p>Processing PDF and extracting events...</p>}
<div style={{ flex: 1 }}>
<Calendar
localizer={localizer}
events={events}
startAccessor="start"
endAccessor="end"
style={{ height: '100%' }}
/>
</div>
<p>
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank" rel="noopener noreferrer">
View Source
</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const { OpenAI } = await import("https://esm.town/v/std/openai");
const pdfExtractText = await import("https://esm.town/v/pdebieamzn/pdfExtractText");
if (request.method === 'POST' && new URL(request.url).pathname === '/upload') {
const formData = await request.formData();
const pdfFile = formData.get('pdf') as File;
if (!pdfFile) {
return new Response('No file uploaded', { status: 400 });
}
const arrayBuffer = await pdfFile.arrayBuffer();
const fullText = await pdfExtractText.default(arrayBuffer);
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [