import { blob } from "https://esm.town/v/std/blob";
const COMMENTS_KEY = "comments_storage";
const getHtml = (comments: string[]) => `
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<title>Comment Box</title>
</head>
<body class="bg-gray-100 text-gray-900 font-sans">
<div class="container mx-auto p-4">
<h1 class="text-3xl font-bold mb-4">Comment Box</h1>
<form method="POST" class="mb-4">
<textarea name="comment" rows="4" cols="50" class="w-full p-2 border border-gray-300 rounded mb-2"></textarea><br />
<button type="submit" class="bg-blue-500 text-white font-bold py-2 px-4 rounded">Submit</button>
</form>
<h2 class="text-2xl font-semibold mb-2">Previous Comments:</h2>
<ul class="list-disc pl-5">
${comments.map(comment => `<li class="mb-1">${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" }
});
}