Versions

  • v0

    7/17/2024
    Open: Version
    +178
    -0

    import { blob } from "https://esm.town/v/std/blob";
    import { Hono } from "npm:hono";
    import { html } from "https://esm.town/v/stevekrouse/html";
    import { faker } from "npm:@faker-js/faker";

    const SAMPLE_STORIES_KEY = "hn_realistic_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: faker.company.catchPhrase(),
    url: faker.internet.url(),
    votes: Math.floor(Math.random() * 100),
    }));
    await blob.setJSON(SAMPLE_STORIES_KEY, sampleStories);
    }
    }

    await initializeSampleStories();

    const app = new Hono();

    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 valign="top" class="votelinks">
    <form method="POST" action="/vote" style="display:inline;">
    <input type="hidden" name="id" value="${story.id}" />
    <button type="submit" class="votearrow" title="upvote">▲</button>
    </form>
    </td>
1
Next