import { createSignal } from "solid-js"; // Rounded down, never up: 1,950 likes is "1.9K", not a "2K" the post hasn't reached. const compact = new Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 1, roundingMode: "trunc", }); const monthDay = new Intl.DateTimeFormat("en", { month: "short", day: "numeric" }); const monthDayYear = new Intl.DateTimeFormat("en", { month: "short", day: "numeric", year: "numeric", }); // The month short, "Nov 14, 2026, 10:07 PM": short enough to share a phone's line with what // sits beside it under a post. const full = new Intl.DateTimeFormat("en", { dateStyle: "medium", timeStyle: "short" }); const exact = new Intl.NumberFormat("en"); /** * Counts in full, in words, for a tooltip: "1,234 reposts, 56 quotes". Only when the short form * hides something, a count rounded down or two counts added together; otherwise nothing. */ export const countsInFull = (...parts: [n: number | undefined, one: string, many: string][]) => { const shown = parts.filter(([n]) => n); const hidden = shown.length > 1 || shown.some(([n]) => n! >= 1000); return hidden ? shown.map(([n, one, many]) => `${exact.format(n!)} ${n === 1 ? one : many}`).join(", ") : undefined; }; /** 1234 -> "1.2K", 1999 -> "1.9K". Zero is left blank so quiet posts stay quiet. */ export const count = (n: number | undefined) => (n ? compact.format(n) : ""); /** * The time, for relative times to count from: moved on every 30 seconds while the window is in * view, and at once when it comes back, so "now" becomes "1m" without anything else changing. * Read inside a view, it redraws every time shown on screen as it moves. */ const [clock, setClock] = createSignal(Date.now()); setInterval(() => { if (document.visibilityState === "visible") setClock(Date.now()); }, 30_000); document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") setClock(Date.now()); }); /** "now", "4m", "3h", "2d", then a date. Kept current as time passes (see `clock`). */ export const relativeTime = (iso: string, now = clock()) => { const date = new Date(iso); const seconds = Math.max(0, (now - date.getTime()) / 1000); if (seconds < 45) return "now"; if (seconds < 3600) return `${Math.round(seconds / 60)}m`; if (seconds < 86400) return `${Math.floor(seconds / 3600)}h`; if (seconds < 86400 * 7) return `${Math.floor(seconds / 86400)}d`; return date.getFullYear() === new Date(now).getFullYear() ? monthDay.format(date) : monthDayYear.format(date); }; export const fullTime = (iso: string) => full.format(new Date(iso)); /** The record key at the end of an at:// URI. */ export const rkeyOf = (uri: string) => uri.slice(uri.lastIndexOf("/") + 1); /** * Who an address is about: their DID, which never changes, so a link (or a tab kept between * visits) still leads to them after a change of handle, and needs no lookup to load. Addresses * with a handle still open, typed in or from elsewhere. */ const actorParam = (actor: { did: string }) => actor.did; export const profilePath = (actor: { did: string; handle: string }) => `/${actorParam(actor)}`; export const postPath = (post: { uri: string; author: { did: string; handle: string } }) => `${profilePath(post.author)}/post/${rkeyOf(post.uri)}`; /** A custom feed's page, at the same path bsky.app uses (minus its /profile prefix). */ export const feedPath = (feed: { uri: string; creator: { did: string; handle: string } }) => `${profilePath(feed.creator)}/feed/${rkeyOf(feed.uri)}`; /** A list's page, at the same path bsky.app uses (minus its /profile prefix). */ export const listPath = (list: { uri: string; creator: { did: string; handle: string } }) => `${profilePath(list.creator)}/lists/${rkeyOf(list.uri)}`; /** * A starter pack's page, at the same path bsky.app uses: under /starter-pack, by its creator and * record key, so a shared link works here with its domain swapped. */ export const starterPackPath = (pack: { uri: string; creator: { did: string } }) => `/starter-pack/${actorParam(pack.creator)}/${rkeyOf(pack.uri)}`; export const hostOf = (url: string) => { try { return new URL(url).host.replace(/^www\./, ""); } catch { return url; } }; /** * A display name, if there's anything in it to see: not one made only of spaces and characters * that draw nothing (tag characters, zero-width ones, Hangul fillers, the blank Braille pattern), * as some people set theirs to look nameless. * * Without one, there's no name: wherever a name sits with the handle (a post, a card, a profile, * a list of people), its line is left out, no gap where it would be, and the handle, faint as * ever, says who it is, leading to them where the name would have. Where words have to stand on * their own (a page's title, a tab's, "… liked your post"), the handle stands in for the name. */ export const visibleName = (name: string | undefined) => name && /[^\s\p{Cf}\p{Mn}\p{Me}ᅟᅠㅤᅠ⠀]/u.test(name) ? isolated(name) : undefined; /** Characters that turn the direction of what follows: overrides, embeddings, isolates. */ const TURNS = /[\u202A-\u202E\u2066-\u2069]/; /** * A name that turns the direction of what follows it (a right-to-left override left open, as * some set for a joke), shut in on its own: between an isolate's marks, which end whatever it * opened, so it's drawn as it is but nothing after it is turned with it ("kris: …" written * backwards to the end of the line). Any other name as it is. Twice, the same. */ const isolated = (name: string) => !TURNS.test(name) || (name.startsWith("\u2068") && name.endsWith("\u2069")) ? name : `\u2068${name}\u2069`;