import { Hono } from "https://esm.sh/hono";
import React, { useEffect, useState } from "https://esm.sh/react";
import { hydrateRoot } from "https://esm.sh/react-dom/client";
import { renderToReadableStream } from "https://esm.sh/react-dom/server";
import { blob } from "https://esm.town/v/std/blob?v=10";
function App() {
const [data, setData] = useState();
useEffect(() => {
async function fetchSchedule() {
await fetch("/schedule")
.then(response => {
return response.json();
})
.then(data => {
console.log("DATA: ", data);
setData(data);
});
}
fetchSchedule();
}, []);
return (
<html>
<head>
<title>Comments</title>
<link rel="stylesheet" href="https://unpkg.com/tailwindcss@2.2.19/dist/tailwind.min.css" />
</head>
<body>
<div className="max-w-md mx-auto mt-8 p-4 bg-gray-100 rounded-lg">
<h1 className="text-2xl font-bold mb-4">Comments</h1>
{data
? data.map((shift) => (
<div key={shift.id} className="bg-white p-2 rounded flex justify-between items-center">
{shift}
</div>
))
: "Loading..."}
</div>
</body>
</html>
);
}
if (typeof document !== "undefined") {
hydrateRoot(document, <App />);
}
const app = new Hono();
app.get("/", async (c) => {
const stream = await renderToReadableStream(<App />, { bootstrapModules: [import.meta.url] });
return new Response(stream, { headers: { "content-type": "text/html" } });
});
app.get("/schedule", async (c) => {
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
const data = await fetch(
`https://api.stage.teamsurfboard.com/api/v1/schedule?start=${today.toISOString()}&end=${tomorrow.toISOString()}`,
{
headers: {
"Authorization": `Bearer ${Deno.env.get("TEAMSURFBOARD_API_TOKEN")}`,
},
},
)
.then((res) => res.json())
.then((data) => data);
return c.json(data.data);
});
export default app.fetch;