Files
pargar/frontend/src/lib/hearts.ts
T
ahp 22d92c133d feat: release v0.2.0 profiles, chat, freeze, and bots
Ship the product pass on the v0.1.3 schema with additive migrations, a replica migration lock, and bot accounts kept out of people lists.
2026-10-10 16:42:35 +03:30

33 lines
980 B
TypeScript

"use client";
import type { Role, User } from "./types";
/** Absolute student cap. A heart returns on the server every few hours. */
export const MAX_HEARTS = 5;
/** Infinity glyph for staff heart display. */
export const INFINITE_HEARTS_LABEL = "∞";
export function hasInfiniteHearts(role: Role | string | null | undefined): boolean {
return role === "admin" || role === "mentor";
}
export function clampHearts(hearts: number | null | undefined): number {
if (hearts == null || Number.isNaN(hearts)) {
return MAX_HEARTS;
}
return Math.max(0, Math.min(MAX_HEARTS, hearts));
}
/** Effective hearts from the server user — never invents a timed refill. */
export function heartsOf(user: User | null | undefined): number {
return clampHearts(user?.hearts);
}
export function formatHeartsDisplay(user: User | null | undefined): string {
if (hasInfiniteHearts(user?.role)) {
return INFINITE_HEARTS_LABEL;
}
return String(clampHearts(user?.hearts));
}