import 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 = { brew: "brews", tea: "teas", vendor: "vendors", vessel: "vessels", infuser: "infusers", }; const KIND_BY_SEGMENT: Record = { 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 | 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 = { 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, ): 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(); 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 {} }