Something went wrong. Try again.
Tea journaling on ATProto (alpha) forked from arabica.social/arabica
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397import type { Author, Brew, EntityKind, Infuser, OolongRecord, Tea, Vendor, Vessel,} from "./types";import { entityLabels } from "./format";
export interface EntityComment { rkey: string; subject_uri: string; text: string; cid: string; parent_uri: string; parent_rkey: string; depth: number; created_at: string; author: Author; like_count: number; is_liked: boolean;}
export interface EntitySocial { is_liked: boolean; like_count: number; comment_count: number; comments: EntityComment[]; is_moderator: boolean; can_hide_record: boolean; can_block_user: boolean; is_record_hidden: boolean;}
export interface BacklinkEntry { did: string; handle: string; display_name: string; avatar_url: string; record_uri: string; collection: string; rkey: string; created_at: string; title: string; rating: number; has_rating: boolean; chain_depth: number;}
export interface BacklinkUsageGroup { key: string; label: string; entries: BacklinkEntry[]; count: number; rating_average: number; rating_count: number; page: number; per_page: number; has_prev: boolean; has_next: boolean;}
export interface BacklinkResult { library_entries: BacklinkEntry[]; library_count: number; usage: BacklinkUsageGroup[]; usage_count: number; rating_average: number; rating_count: number;}
export interface EntityViewResponse { record: OolongRecord; subject_uri: string; subject_cid: string; author: Author; social: EntitySocial; backlinks: BacklinkResult | null; backlinks_detail_url: string; is_own_profile: boolean; is_authenticated: boolean; share_url: string; entity_type: string; entity_count: number;}
export interface BacklinksResponse { entity_name: string; entity_type: string; back_url: string; detail_url: string; result: BacklinkResult | null;}
export interface LikeToggleResponse { is_liked: boolean; like_count: number; subject_uri: string;}
export interface CommentListResponse { comments: EntityComment[]; subject_uri: string; subject_cid: string; is_authenticated: boolean; viewer_did: string;}
export interface CommentCreateResponse { comment: EntityComment; comments: EntityComment[];}
export interface CommentDeleteResponse { deleted: boolean; rkey: string;}
export interface ReportResponse { report_id: string; submitted: boolean;}
export const COLLECTION_BY_KIND: Record<EntityKind, string> = { brew: "brews", tea: "teas", vendor: "vendors", vessel: "vessels", infuser: "infusers",};
const KIND_BY_SEGMENT: Record<string, EntityKind> = { brews: "brew", brew: "brew", teas: "tea", tea: "tea", vendors: "vendor", vendor: "vendor", vessels: "vessel", vessel: "vessel", infusers: "infuser", infuser: "infuser",};
export function kindFromPathname(pathname: string): EntityKind | null { const segment = (pathname ?? "").split("/").filter(Boolean)[0] ?? ""; return KIND_BY_SEGMENT[segment.toLowerCase()] ?? null;}
export function collectionFor(kind: EntityKind): string { return COLLECTION_BY_KIND[kind];}
export function entityNoun(kind: EntityKind): string { return entityLabels[kind].singular;}
function recordPath(kind: EntityKind, actor: string, rkey: string): string { return ( "/" + collectionFor(kind) + "/" + encodeURIComponent(actor) + "/" + encodeURIComponent(rkey) );}
export function recordHref( kind: EntityKind, actor: string, rkey: string,): string { if (!actor || !rkey) return ""; return recordPath(kind, actor, rkey);}
export function backlinksHref( kind: EntityKind, actor: string, rkey: string,): string { const base = recordHref(kind, actor, rkey); return base ? base + "/backlinks" : "";}
export function recordApiPath( kind: EntityKind, actor: string, rkey: string,): string { if (!actor || !rkey) return ""; return "/api" + recordPath(kind, actor, rkey);}
export function backlinksApiPath( kind: EntityKind, actor: string, rkey: string,): string { const base = recordApiPath(kind, actor, rkey); return base ? base + "/backlinks" : "";}
export function editHref(kind: EntityKind, rkey: string): string { if (!rkey) return ""; return "/" + collectionFor(kind) + "/" + encodeURIComponent(rkey) + "/edit";}
export function deleteApiPath(kind: EntityKind, rkey: string): string { if (!rkey) return ""; return "/api/" + collectionFor(kind) + "/" + encodeURIComponent(rkey);}
export function actorOf(author: Author | null | undefined): string { return author?.handle || author?.did || "";}
export function profileHrefFor( author: Pick<Author, "handle" | "did"> | null | undefined,): string { const actor = author?.handle || author?.did || ""; return actor ? "/profile/" + encodeURIComponent(actor) : "";}
export function entityTitle(kind: EntityKind, record: OolongRecord): string { switch (kind) { case "brew": { const brew = record as Brew; return (brew.tea?.name ?? "").trim() || "A steep"; } case "tea": return (record as Tea).name?.trim() || "A tea"; case "vendor": return (record as Vendor).name?.trim() || "A vendor"; case "vessel": return (record as Vessel).name?.trim() || "A vessel"; case "infuser": return (record as Infuser).name?.trim() || "An infuser"; }}
const CATEGORY_LABELS: Record<string, string> = { green: "Green", yellow: "Yellow", white: "White", oolong: "Oolong", red: "Red", black: "Black", dark: "Dark / Fermented", herbal: "Herbal", rooibos: "Rooibos", flavored: "Flavored", blend: "Blend", other: "Other",};
export function teaCategoryLabel(category: string | undefined | null): string { const raw = (category ?? "").trim(); if (!raw) return ""; return CATEGORY_LABELS[raw] ?? raw;}
export function brewRatio( brew: Pick<Brew, "leaf_grams" | "water_amount">,): string { const leaf = brew.leaf_grams ?? 0; const water = brew.water_amount ?? 0; if (leaf <= 0 || water <= 0) return ""; return "1:" + Math.round(water / leaf);}
export function ratingScore(rating: number | null | undefined): string { return rating && rating > 0 ? String(Math.round(rating)) : "";}
export function formatTimestamp(value: string | undefined | null): string { if (!value) return ""; const date = new Date(value); if (Number.isNaN(date.getTime())) return ""; return new Intl.DateTimeFormat(undefined, { dateStyle: "long", timeStyle: "short", }).format(date);}
export interface BacklinkAvatar { did: string; handle: string; display_name: string; avatar_url: string; title: string; initial: string; profileHref: string;}
export function backlinkAvatars( entries: BacklinkEntry[] | undefined, max = 5,): BacklinkAvatar[] { const seen = new Set<string>(); const avatars: BacklinkAvatar[] = []; for (const entry of entries ?? []) { const did = entry.did || entry.handle || entry.record_uri; if (seen.has(did)) continue; seen.add(did); const title = entry.display_name || entry.handle || entry.did; avatars.push({ did, handle: entry.handle, display_name: entry.display_name, avatar_url: entry.avatar_url, title, initial: (title || "?").slice(0, 1).toUpperCase(), profileHref: entry.handle ? "/profile/" + encodeURIComponent(entry.handle) : entry.did ? "/profile/" + encodeURIComponent(entry.did) : "", }); if (avatars.length >= max) break; } return avatars;}
export function backlinksEmpty(result: BacklinkResult | null): boolean { if (!result) return true; return (result.library_count ?? 0) === 0 && (result.usage_count ?? 0) === 0;}
export function collectionTail(collection: string): string { const parts = (collection ?? "").split("."); return parts[parts.length - 1] ?? "";}
export function backlinkEntryHref(entry: BacklinkEntry): string { const kind = KIND_BY_SEGMENT[collectionTail(entry.collection).toLowerCase()]; if (!kind || !entry.rkey) return ""; const actor = entry.handle || entry.did; return recordHref(kind, actor, entry.rkey);}
export function appendBacklinkPage( previous: BacklinkUsageGroup[], incoming: BacklinkUsageGroup[], pagedKey: string,): BacklinkUsageGroup[] { return previous.map((group) => { if (group.key !== pagedKey) return group; const page = incoming.find((candidate) => candidate.key === pagedKey); if (!page) return group; const seen = new Set(group.entries.map((entry) => entry.record_uri)); const entries = [ ...group.entries, ...page.entries.filter((entry) => !seen.has(entry.record_uri)), ]; return { ...page, entries, }; });}
const DRAFT_PREFIX = "oolong:solid:comment-draft:";
export function commentDraftKey(subjectUri: string): string { return DRAFT_PREFIX + subjectUri;}
export function replyDraftKey(subjectUri: string, parentUri: string): string { return DRAFT_PREFIX + subjectUri + ":reply:" + parentUri;}
export function readDraft(key: string): string { try { return sessionStorage.getItem(key) ?? ""; } catch { return ""; }}
export function writeDraft(key: string, text: string): void { try { if (text) sessionStorage.setItem(key, text); else sessionStorage.removeItem(key); } catch {}}
export function clearDraft(key: string): void { try { sessionStorage.removeItem(key); } catch {}}