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>
<style>
body { font-family: Arial, sans-serif; background-color: #f0f4f8; padding: 30px; }
</style>
</head>
<body>
<div class="max-w-lg mx-auto p-8 bg-white shadow rounded-lg">
<h1 class="text-2xl font-bold mb-6">Comment Box</h1>
<form method="POST" class="mb-4">
<textarea name="comment" rows="4" class="w-full p-2 border rounded mb-4" placeholder="Write your comment here..."></textarea>
<button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-700">Submit</button>
</form>
<h2 class="text-xl font-semibold mb-4">Previous Comments</h2>
<ul class="space-y-2">
${comments.map(comment => `<li class="p-4 bg-gray-100 rounded">${comment}</li>`).join('')}
</ul>
</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" }
});
}