Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
/** @jsxImportSource npm:hono@3/jsx */
import { basicAuth } from "https://esm.town/v/pomdtr/basicAuth?v=62";
import { Browserbase, type Session } from "npm:@browserbasehq/sdk";
import { Hono } from "npm:hono";
const app = new Hono();
const browserbase = new Browserbase();
function duration(session) {
const startedAt = new Date(session.startedAt).getTime();
if (session.status === "RUNNING") {
return (Date.now() - startedAt) / 1000;
}
const endedAt = new Date(session.endedAt).getTime();
return (endedAt - startedAt) / 1000;
}
// Route to list all sessions
app.get("/", async (c) => {
const sessions = await browserbase.listSessions();
const currentPage = Number(c.req.query("page") || 1);
return c.html(
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/tailwindcss@2.2.19/dist/tailwind.min.css" />
</head>
<div class="container mx-auto p-4">
<h1 class="text-2xl font-bold mb-4">All sessions</h1>
<a href="/sessions/new" class="text-blue-600 hover:underline">Start a new session</a>
<SessionsTable sessions={sessions} currentPage={currentPage} />
</div>
</html>,
);
});
const formatDuration = (seconds) => {
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
if (hours > 0) {
return `${hours}:${String(minutes % 60).padStart(2, "0")}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`;
}
return `${minutes}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`;
};
function SessionsTable({ sessions, currentPage }: { sessions: Session[]; currentPage: number }) {
const sessionsPerPage = 10;
const totalPages = Math.ceil(sessions.length / sessionsPerPage);
const paginatedSessions = sessions.slice(
(currentPage - 1) * sessionsPerPage,
currentPage * sessionsPerPage,
);
return (
<div class="container mx-auto p-4">
<div class="bg-white shadow-md rounded-lg overflow-hidden">
<table class="min-w-full divide-y divide-gray-200">
<div class="bg-gray-50">
<div class="flex justify-between items-center">
<div class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</div>
<div class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Started</div>
<div class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Duration</div>
<div class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Session</div>
</div>
</div>
<div class="bg-white divide-y divide-gray-200">
{paginatedSessions.map((session) => (
<a
key={session.id}
href={`/sessions/${session.id}`}
class="flex items-center justify-between hover:bg-gray-50"
>
<div class="px-6 py-4 whitespace-nowrap">
<span class="flex items-center">
<span class="h-2 w-2 bg-green-400 rounded-full mr-2"></span>
<span class="text-sm text-gray-900">
{session.status[0] + session.status.toLowerCase().slice(1)}
</span>
</span>
</div>
<div class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{new Date(session.startedAt).toLocaleString()}
</div>
<div class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{formatDuration(duration(session))}
</div>
<div class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<span class="flex items-center">
<span class="text-indigo-600">{session.id.substring(0, 6)}</span>
</span>
</div>
</a>
))}
</div>
</table>
</div>
<div class="flex justify-center mt-4 space-x-1">
{[...Array(totalPages).keys()].map((page) => (
<a
key={page + 1}
stevekrouse-browserbase.web.val.run
June 30, 2024