pluralize, improve SessionData typing

This commit is contained in:
2024-12-22 09:53:49 -06:00
parent 9f67603bbe
commit 51e05474ec
2 changed files with 16 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
import Badge from "@/components/Badge"; import Badge from "@/components/Badge";
import { plural } from "@/util";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
type StatefulDemoProps = { type StatefulDemoProps = {
@@ -11,9 +12,9 @@ type SessionData = {
}; };
const StatefulDemo = ({ class: className }: StatefulDemoProps) => { const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
const [session, setSession] = useState<SessionData>({ const [session, setSession] = useState<SessionData | null>({
id: "0x59AF5", id: "0x59AF5BC09",
downloads: ["0xABF4"], downloads: ["0xABF4", "0x1F3A", "0x2F3A"],
}); });
return ( return (
@@ -23,10 +24,14 @@ const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
browser. Each download gets a unique identifier bound to the user browser. Each download gets a unique identifier bound to the user
session. session.
<br /> <br />
Your session is{" "} {session != null ? (
<b class="text-teal-400 font-inter">{session?.id ?? "loading"}</b>. You <>
have <b class="text-teal-400 font-inter">{session?.downloads.length}</b>{" "} Your session is <b class="text-teal-400 font-inter">{session.id}</b>
known downloads. . You have{" "}
<b class="text-teal-400 font-inter">{session.downloads.length}</b>{" "}
known {plural("download", session.downloads.length)}.
</>
) : null}
</p> </p>
<div> <div>
{session?.downloads.map((download) => ( {session?.downloads.map((download) => (

View File

@@ -5,3 +5,7 @@ export { type ClassValue };
export function cn(...inputs: ClassValue[]) { export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)); return twMerge(clsx(inputs));
} }
export function plural(text: string, count: number) {
return `${text}${count === 1 ? "" : "s"}`;
}