janpaul123-valle_tmp_493882739904999215894176448564479.web.val.run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { blob } from "https://esm.town/v/std/blob";
import { Hono } from "npm:hono";
import { html } from "https://esm.town/v/stevekrouse/html";
// Initialize sample stories and store them in blob storage
const SAMPLE_STORIES_KEY = "hn_sample_stories";
async function initializeSampleStories() {
const existingStories = await blob.getJSON(SAMPLE_STORIES_KEY);
if (!existingStories) {
const sampleStories = Array.from({ length: 30 }).map((_, idx) => ({
id: idx + 1,
title: `Sample Story ${idx + 1}`,
url: `https://example.com/story${idx + 1}`,
votes: Math.floor(Math.random() * 100),
}));
await blob.setJSON(SAMPLE_STORIES_KEY, sampleStories);
}
}
await initializeSampleStories();
// Hono app setup
const app = new Hono();
// Serve main page with stories
app.get("/", async (c) => {
const stories = await blob.getJSON(SAMPLE_STORIES_KEY) || [];
const storiesHtml = stories
.sort((a, b) => b.votes - a.votes)
.map(
(story) => `
<tr class="athing">
<td align="right" valign="top" class="title">${story.votes}</td>
<td></td>
<td class="title">
<a href="${story.url}" class="storylink">${story.title}</a>
<span class="sitebit comhead"> (<a href="${story.url}" target="_blank" class="siteurl">example.com</a>)</span>
</td>
<td valign="top">
<form method="POST" action="/vote" style="display:inline;">
<input type="hidden" name="id" value="${story.id}" />
<button type="submit" class="upvote">▲</button>
</form>
</td>
</tr>`
)
.join("");
return html(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hacker News Clone</title>
<style>
body { font-family: Verdana, Geneva, sans-serif; margin: 0; padding: 0; background: #f6f6ef; }
.hn-container { width: 85%; margin: auto; background: white; border: 1px solid #e6e6df; padding: 20px; }
.hn-header { background: #ff6600; padding: 10px; display: flex; align-items: center; }
.hn-header a { color: white; text-decoration: none; font-size: 18px; }
.hn-header .title { font-weight: bold; margin-left: 10px; }
table { width: 100%; }
.athing { border-bottom: 1px solid #e6e6df; padding: 6px 0; }
.athing .title { font-size: 14px; }
.sitebit { font-size: 11px; color: #828282; }
.upvote { background: none; border: none; padding: 0; color: #ff6600; cursor: pointer; }
.upvote:hover { color: #ff8547; }
.submit-container { margin-top: 20px; }
.submit-container h2 { margin: 0 0 10px; font-size: 16px; }
.submit-container form { display: flex; flex-direction: column; }
.submit-container input[type="text"], .submit-container input[type="url"] { margin-bottom: 10px; padding: 8px; border: 1px solid #e6e6df; border-radius: 4px; }
.submit-container button { padding: 8px 12px; background: #ff6600; color: white; border: none; border-radius: 4px; cursor: pointer; }
.submit-container button:hover { background: #f25800; }
</style>
</head>
<body>
<div class="hn-header">
<a href="/"><img src="https://news.ycombinator.com/y18.gif" alt="Hacker News" border="0" height="18" width="18"></a>
<a href="/"><span class="title">Hacker News Clone</span></a>
</div>
<div class="hn-container">
<table>
<tbody>
${storiesHtml}
</tbody>
</table>
<div class="submit-container">
<h2>Submit a New Story</h2>
<form method="POST" action="/submit">
<input type="text" name="title" placeholder="Title" required />
<input type="url" name="url" placeholder="URL" required />
<button type="submit">Submit</button>
</form>
</div>
</div>
</body>
</html>
`);
});
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 17, 2024