import type { Signal } from "@preact/signals"; import { Button } from "../components/Button.tsx"; import { useState, useEffect } from "preact/hooks"; interface SharedProps { hasClicked: Signal; globalCount: string; } export default function Counter(props: SharedProps) { const count = useState(0); const onClick = () => { // set a timer to update the global count, resetting // whenever a user activity is detected let timer: number; timer = setTimeout(async () => { // TODO: add the upload code here }, 5000); window.onclick = () => { clearTimeout(timer); timer = setTimeout(async () => { // Upload code goes here }, 5000); } } useEffect(() => { let es = new EventSource(window.location.href); es.addEventListener("open", () => { console.log(`[${new Date()}] Connected to statistics stream`); }) es.addEventListener("message", (e) => { }); // TODO: Reconnect backoff logic could be improved es.addEventListener("error", () => { console.warn(`[${new Date()}] Disconnected from statistics stream, attempting to reconnect...`); const backoff = 1000 + Math.random() * 5000; new Promise((resolve) => setTimeout(resolve, backoff)); es = new EventSource(window.location.href); }) }, []) return (

{count}

Times clicked

Everyone has clicked the button {parseInt(props.globalCount)} times!

); }