import { blob } from "https://esm.town/v/std/blob";
const COMMENTS_KEY = "comments_storage";
const getHtml = (comments: string[]) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comment Box</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="flex items-center justify-center min-h-screen bg-gray-100">
<div class="bg-white p-8 rounded shadow-md w-full max-w-lg">
<h1 class="text-2xl font-bold mb-4">Comment Box</h1>
<form method="POST" class="mb-6">
<textarea name="comment" rows="4" class="w-full p-2 border rounded mb-4" placeholder="Write a comment..."></textarea>
<button type="submit" class="w-full bg-blue-500 text-white p-2 rounded hover:bg-blue-600">Submit</button>
</form>
<h2 class="text-xl font-semibold mb-2">Previous Comments:</h2>
<ul class="space-y-4">
${comments.map(comment => `<li class="p-4 border rounded shadow-sm">"${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" }
});
}