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
// This approach creates a minimalist, full-screen website that tracks the total number of clicks made anywhere on the page.
// We'll use SQLite for persistence to store the click count and add a confetti effect for each click.
/** @jsxImportSource https://esm.sh/react */
import React, { useState, useEffect } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import confetti from 'https://esm.sh/canvas-confetti';
function App() {
const [clicks, setClicks] = useState(0);
useEffect(() => {
fetchClicks();
}, []);
const fetchClicks = async () => {
const response = await fetch('/clicks');
const data = await response.json();
setClicks(data.clicks);
};
const handleClick = async (event) => {
const { clientX, clientY } = event;
confetti({
particleCount: 100,
spread: 70,
origin: { x: clientX / window.innerWidth, y: clientY / window.innerHeight }
});
await fetch('/increment', { method: 'POST' });
fetchClicks();
};
return (
<div className="fullscreen" onClick={handleClick}>
<div className="content">
<h1>{clicks}</h1>
<p>Clicks</p>
</div>
<footer>
<span>Made by <a href="https://x.com/ankitkr0" target="_blank" rel="noopener noreferrer">ankit</a></span>
<span className="separator"></span>
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank" rel="noopener noreferrer">
View Source
</a>
</footer>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
const SCHEMA_VERSION = 2
const KEY = "click_counter";
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${KEY}_clicks_${SCHEMA_VERSION} (
id INTEGER PRIMARY KEY,
count INTEGER NOT NULL DEFAULT 0
)
`);
// Initialize the counter if it doesn't exist
await sqlite.execute(`
INSERT OR IGNORE INTO ${KEY}_clicks_${SCHEMA_VERSION} (id, count) VALUES (1, 0)
`);
const url = new URL(request.url);
if (url.pathname === '/clicks') {
const result = await sqlite.execute(`SELECT count FROM ${KEY}_clicks_${SCHEMA_VERSION} WHERE id = 1`);
return new Response(JSON.stringify({ clicks: result.rows[0].count }), {
headers: { 'Content-Type': 'application/json' }
});
}
if (url.pathname === '/increment' && request.method === 'POST') {
await sqlite.execute(`UPDATE ${KEY}_clicks_${SCHEMA_VERSION} SET count = count + 1 WHERE id = 1`);
return new Response(JSON.stringify({ success: true }), {
headers: { 'Content-Type': 'application/json' }
});
}
return new Response(`
<html>
<head>
<title>Click Counter</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>${css}</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="${import.meta.url}"></script>
</body>