From 51e05474ec015ad04148788dc3e2896d7e648bc5 Mon Sep 17 00:00:00 2001
From: Xevion
Date: Sun, 22 Dec 2024 09:53:49 -0600
Subject: [PATCH] pluralize, improve SessionData typing
---
frontend/src/components/StatefulDemo.tsx | 19 ++++++++++++-------
frontend/src/util.ts | 4 ++++
2 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/frontend/src/components/StatefulDemo.tsx b/frontend/src/components/StatefulDemo.tsx
index 426c741..3d32087 100644
--- a/frontend/src/components/StatefulDemo.tsx
+++ b/frontend/src/components/StatefulDemo.tsx
@@ -1,4 +1,5 @@
import Badge from "@/components/Badge";
+import { plural } from "@/util";
import { useState } from "preact/hooks";
type StatefulDemoProps = {
@@ -11,9 +12,9 @@ type SessionData = {
};
const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
- const [session, setSession] = useState({
- id: "0x59AF5",
- downloads: ["0xABF4"],
+ const [session, setSession] = useState({
+ id: "0x59AF5BC09",
+ downloads: ["0xABF4", "0x1F3A", "0x2F3A"],
});
return (
@@ -23,10 +24,14 @@ const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
browser. Each download gets a unique identifier bound to the user
session.
- Your session is{" "}
- {session?.id ?? "loading"}. You
- have {session?.downloads.length}{" "}
- known downloads.
+ {session != null ? (
+ <>
+ Your session is {session.id}
+ . You have{" "}
+ {session.downloads.length}{" "}
+ known {plural("download", session.downloads.length)}.
+ >
+ ) : null}
{session?.downloads.map((download) => (
diff --git a/frontend/src/util.ts b/frontend/src/util.ts
index 0230a35..c9c0ee3 100644
--- a/frontend/src/util.ts
+++ b/frontend/src/util.ts
@@ -5,3 +5,7 @@ export { type ClassValue };
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
+
+export function plural(text: string, count: number) {
+ return `${text}${count === 1 ? "" : "s"}`;
+}