revite/src/lib/PaintCounter.tsx

24 lines
615 B
TypeScript
Raw Normal View History

2021-08-05 14:47:00 +01:00
/* eslint-disable react-hooks/rules-of-hooks */
2021-06-19 12:34:53 +01:00
import { useState } from "preact/hooks";
const counts: { [key: string]: number } = {};
2021-07-05 11:23:23 +01:00
export default function PaintCounter({
2021-07-05 11:25:20 +01:00
small,
always,
2021-07-05 11:23:23 +01:00
}: {
2021-07-05 11:25:20 +01:00
small?: boolean;
always?: boolean;
2021-07-05 11:23:23 +01:00
}) {
2021-07-05 11:25:20 +01:00
if (import.meta.env.PROD && !always) return null;
2021-06-19 15:29:04 +01:00
const [uniqueId] = useState(`${Math.random()}`);
2021-07-05 11:25:20 +01:00
const count = counts[uniqueId] ?? 0;
counts[uniqueId] = count + 1;
return (
<div style={{ textAlign: "center", fontSize: "0.8em" }}>
{small ? <>P: {count + 1}</> : <>Painted {count + 1} time(s).</>}
</div>
);
2021-06-19 12:34:53 +01:00
}