2023-09-10 12:22:22 +00:00
|
|
|
import type { Signal } from "@preact/signals";
|
|
|
|
import { Button } from "../components/Button.tsx";
|
2023-09-11 04:05:53 +00:00
|
|
|
import { useEffect, useState } from "preact/hooks";
|
|
|
|
import axios from "axios-web";
|
2023-09-10 12:22:22 +00:00
|
|
|
|
|
|
|
interface SharedProps {
|
|
|
|
hasClicked: Signal<boolean>;
|
2023-09-12 06:28:01 +00:00
|
|
|
globalCount: number;
|
2023-09-10 12:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function Counter(props: SharedProps) {
|
2023-09-11 03:55:06 +00:00
|
|
|
const [count, setCount] = useState(0);
|
2023-09-12 06:28:01 +00:00
|
|
|
const [globalCount, setGlobalCount] = useState(props.globalCount);
|
2023-09-11 04:05:53 +00:00
|
|
|
const [internalCount, setInternalCount] = useState(0);
|
2023-09-11 04:00:53 +00:00
|
|
|
|
2023-09-10 12:22:22 +00:00
|
|
|
const onClick = () => {
|
2023-09-11 04:05:53 +00:00
|
|
|
setInternalCount(internalCount + 1);
|
|
|
|
setCount(count + 1);
|
|
|
|
};
|
2023-09-10 12:22:22 +00:00
|
|
|
|
2023-09-11 04:05:53 +00:00
|
|
|
useEffect(() => {
|
2023-09-10 12:28:22 +00:00
|
|
|
// set a timer to update the global count, resetting
|
|
|
|
// whenever a user activity is detected
|
2023-09-11 04:05:53 +00:00
|
|
|
let timer: number;
|
2023-09-10 12:28:22 +00:00
|
|
|
|
2023-09-12 06:28:01 +00:00
|
|
|
timer = setTimeout(() => {
|
|
|
|
axios.post(window.location.href, JSON.stringify({ data: internalCount }));
|
|
|
|
setInternalCount(0);
|
|
|
|
}, 5000)
|
|
|
|
|
2023-09-10 12:28:22 +00:00
|
|
|
window.onclick = () => {
|
|
|
|
clearTimeout(timer);
|
2023-09-11 03:55:06 +00:00
|
|
|
timer = setTimeout(() => {
|
2023-09-11 04:05:53 +00:00
|
|
|
console.info(
|
|
|
|
`[${new Date()}] Updating global count: ${internalCount + 1}`,
|
|
|
|
);
|
|
|
|
axios.post(
|
|
|
|
window.location.href,
|
|
|
|
JSON.stringify({ data: internalCount + 1 }),
|
|
|
|
);
|
2023-09-11 04:00:53 +00:00
|
|
|
|
|
|
|
setInternalCount(0);
|
2023-09-10 12:31:19 +00:00
|
|
|
}, 5000);
|
2023-09-11 04:05:53 +00:00
|
|
|
};
|
|
|
|
});
|
2023-09-10 13:05:07 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let es = new EventSource(window.location.href);
|
|
|
|
|
|
|
|
es.addEventListener("open", () => {
|
2023-09-11 04:57:06 +00:00
|
|
|
console.log(`Connected to statistics stream`);
|
2023-09-11 04:05:53 +00:00
|
|
|
});
|
2023-09-10 13:05:07 +00:00
|
|
|
|
|
|
|
es.addEventListener("message", (e) => {
|
2023-09-11 04:57:06 +00:00
|
|
|
console.log(` Received global count: ${e.data}`);
|
2023-09-12 06:28:01 +00:00
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
setGlobalCount(parseInt(data.globalCount));
|
2023-09-10 13:05:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Reconnect backoff logic could be improved
|
|
|
|
es.addEventListener("error", () => {
|
2023-09-11 04:05:53 +00:00
|
|
|
console.warn(
|
2023-09-11 04:57:06 +00:00
|
|
|
`Disconnected from statistics stream, attempting to reconnect...`,
|
2023-09-11 04:05:53 +00:00
|
|
|
);
|
2023-09-10 13:05:07 +00:00
|
|
|
const backoff = 1000 + Math.random() * 5000;
|
|
|
|
new Promise((resolve) => setTimeout(resolve, backoff));
|
|
|
|
es = new EventSource(window.location.href);
|
2023-09-11 04:05:53 +00:00
|
|
|
});
|
|
|
|
}, []);
|
2023-09-10 13:05:07 +00:00
|
|
|
|
2023-09-10 12:22:22 +00:00
|
|
|
return (
|
|
|
|
<div class="max-w-sm text-center rounded overflow-hidden">
|
|
|
|
<div class="px-6 py-4">
|
2023-09-10 13:05:07 +00:00
|
|
|
<p class="text-3xl">{count}</p>
|
2023-09-10 12:22:22 +00:00
|
|
|
<p class="text-gray-700 text-base">Times clicked</p>
|
|
|
|
</div>
|
|
|
|
<div class="px-6 pt-4 pb-2">
|
|
|
|
<Button onClick={onClick}>Squish that button</Button>
|
|
|
|
</div>
|
|
|
|
<div class="px-6 pt-4 pb-2">
|
2023-09-11 04:05:53 +00:00
|
|
|
<p>
|
2023-09-12 06:28:01 +00:00
|
|
|
Everyone has clicked the button {globalCount} times!
|
2023-09-11 04:05:53 +00:00
|
|
|
</p>
|
2023-09-10 12:22:22 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|