Something went wrong. Try again.
Tea journaling on ATProto (alpha) forked from arabica.social/arabica
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219import type { Brew, EntityKind, FeedItem } from "./types";
export function displayHandle(handle: string | undefined | null): string { const cleaned = (handle ?? "").replace(/^@+/, "").trim(); return cleaned ? "@" + cleaned : "";}
export function displayName( name: string | undefined | null, handle?: string | null,): string { const trimmed = (name ?? "").trim(); if (trimmed) return trimmed; const cleaned = (handle ?? "").replace(/^@+/, "").trim(); return cleaned || "A tea drinker";}
export function initials( name: string | undefined | null, handle?: string | null,): string { const source = displayName(name, handle).replace(/^@/, ""); const parts = source.split(/[\s._-]+/).filter(Boolean); const first = parts[0]?.[0] ?? "T"; const second = parts.length > 1 ? parts[parts.length - 1][0] : ""; return (first + second).toUpperCase();}
export function safeExternalUrl(value: string | undefined | null): string { const raw = (value ?? "").trim(); if (!raw) return ""; try { const url = new URL(raw); return ["http:", "https:", "mailto:"].includes(url.protocol) ? url.toString() : ""; } catch { return ""; }}
function relativeTime(timestamp: string | undefined): string { if (!timestamp) return ""; const then = new Date(timestamp).getTime(); if (!Number.isFinite(then)) return ""; const seconds = Math.round((then - Date.now()) / 1000); const formatter = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }); const steps: Array<[Intl.RelativeTimeFormatUnit, number]> = [ ["year", 60 * 60 * 24 * 365], ["month", 60 * 60 * 24 * 30], ["week", 60 * 60 * 24 * 7], ["day", 60 * 60 * 24], ["hour", 60 * 60], ["minute", 60], ]; for (const [unit, span] of steps) { if (Math.abs(seconds) >= span) return formatter.format(Math.round(seconds / span), unit); } return "just now";}
export function timeAgo( item: Pick<FeedItem, "time_ago" | "timestamp">,): string { return (item.time_ago ?? "").trim() || relativeTime(item.timestamp);}
export function formatEntityDate(value: string | undefined | null): string { if (!value) return ""; const date = new Date(value); if (Number.isNaN(date.getTime())) return ""; return new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric", year: "numeric", }).format(date);}
export function formatRating(rating: number | undefined | null): string { return rating && rating > 0 ? rating + "/10" : "";}
export function formatCount(count: number | undefined | null): string { const value = count ?? 0; return new Intl.NumberFormat().format(value);}
export type TemperatureUnit = "recorded" | "celsius" | "fahrenheit";
type MeasuredUnit = "celsius" | "fahrenheit";
export function normalizeTemperatureUnit( value: string | undefined | null,): TemperatureUnit { return value === "celsius" || value === "fahrenheit" ? value : "recorded";}
// Mirrors internal/web/bff: brews record the number as entered, and the unit is// inferred from magnitude, above 100 being Fahrenheit.export function detectTemperatureUnit(value: number): MeasuredUnit { return value > 100 ? "fahrenheit" : "celsius";}
function convertTemperature( value: number, from: MeasuredUnit, to: MeasuredUnit,): number { if (from === to) return value; return from === "fahrenheit" ? ((value - 32) * 5) / 9 : (value * 9) / 5 + 32;}
function roundTenth(value: number): number { return Math.round(value * 10) / 10;}
export function formatTemperature( value: number | undefined | null, preferredUnit?: string | null,): string { if (!value || value <= 0) return ""; const recorded = detectTemperatureUnit(value); const preferred = normalizeTemperatureUnit(preferredUnit); const unit = preferred === "recorded" ? recorded : preferred; const shown = Math.round(convertTemperature(value, recorded, unit)); return shown + "°" + (unit === "fahrenheit" ? "F" : "C");}
export function temperatureInputValue( value: number | undefined | null, unit: string | undefined | null,): number { const stored = value ?? 0; if (!Number.isFinite(stored) || stored <= 0) return 0; const preferred = normalizeTemperatureUnit(unit); if (preferred === "recorded") return stored; return roundTenth( convertTemperature(stored, detectTemperatureUnit(stored), preferred), );}
// A typed value is stored as entered whenever its magnitude already reads as the// entered unit; otherwise the physical temperature is kept by storing the other// unit, so editing a 200°F brew with Celsius shown still means the same heat.export function temperatureStorageValue( value: number | undefined | null, unit: string | undefined | null,): number { const entered = value ?? 0; if (!Number.isFinite(entered) || entered <= 0) return 0; const preferred = normalizeTemperatureUnit(unit); if (preferred === "recorded") return entered; const target: MeasuredUnit = detectTemperatureUnit(entered) === preferred ? preferred : oppositeUnit(preferred); return roundTenth(convertTemperature(entered, preferred, target));}
function oppositeUnit(unit: MeasuredUnit): MeasuredUnit { return unit === "celsius" ? "fahrenheit" : "celsius";}
export function formatSeconds(value: number | undefined | null): string { if (!value || value <= 0) return ""; if (value < 60) return value + "s"; const minutes = Math.floor(value / 60); const seconds = value % 60; return seconds ? minutes + "m " + seconds + "s" : minutes + "m";}
export function plural( count: number, singular: string, pluralForm = singular + "s",): string { return count === 1 ? singular : pluralForm;}
export function styleLabel(style: string | undefined | null): string { switch (style) { case "longSteep": return "Long Steep"; case "coldBrew": return "Cold Brew"; default: return style ?? ""; }}
export function infusionMethodLabel(method: string | undefined | null): string { switch (method) { case "tea-bag": return "Tea Bag"; case "loose-leaf": return "Loose Leaf"; case "infuser": return "Infuser"; default: return method ?? ""; }}
export function brewTitle(brew: Brew): string { return brew.tea?.name || "A steep";}
export const entityLabels: Record< EntityKind, { singular: string; plural: string; filter: string }> = { brew: { singular: "steep", plural: "steeps", filter: "Steeps" }, tea: { singular: "tea", plural: "teas", filter: "Teas" }, vendor: { singular: "vendor", plural: "vendors", filter: "Vendors" }, vessel: { singular: "vessel", plural: "vessels", filter: "Vessels" }, infuser: { singular: "infuser", plural: "infusers", filter: "Infusers" },};