import { blob } from "https://esm.town/v/std/blob";
const COMMENTS_KEY = "comments_storage";
const getHtml = (comments: string[]) => `
<html>
<head>
<title>Comment Box</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-6">
<div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden md:max-w-2xl">
<div class="md:flex">
<div class="p-8">
<h1 class="block mt-1 text-lg leading-tight font-medium text-black">Comment Box</h1>
<form method="POST" class="mt-4">
<textarea name="comment" rows="4" cols="50" class="w-full p-2 border rounded-lg focus:outline-none focus:shadow-outline" placeholder="Write your comment here..."></textarea><br />
<button class="mt-2 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" type="submit">Submit</button>
</form>
<h2 class="mt-8 text-xl leading-tight font-medium text-black">Previous Comments:</h2>
<ul class="mt-4 space-y-2">
${comments.map(comment => `<li class="p-4 bg-gray-50 rounded-lg shadow-md">${comment}</li>`).join('')}
</ul>
</div>
</div>
</div>
</body>
</html>
`;
export default async function main(req: Request): Promise<Response> {
let comments: string[] = await blob.getJSON(COMMENTS_KEY) || [];
if (req.method === "POST") {
const formData = await req.formData();
const newComment = formData.get("comment")?.toString().trim();
if (newComment) {
comments.push(newComment);
await blob.setJSON(COMMENTS_KEY, comments);
return Response.redirect(new URL(req.url).origin);
}
}
return new Response(getHtml(comments), {
headers: { "Content-Type": "text/html" }
});
}