File size: 6,242 Bytes
c6302b1 492326f 879c715 492326f c6302b1 a756421 c6302b1 a756421 c6302b1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | import type { PooledTweet } from "@xtap-pool/shared";
export type TextSegment =
{ kind: "text"; text: string } | { kind: "link"; text: string; href: string };
const TOKEN_PATTERN = /(https?:\/\/[^\s]+)|(@[A-Za-z0-9_]{1,15})|(#[\p{L}0-9_]+)/gu;
/** Split tweet text into plain and linkified segments (URLs, @mentions, #hashtags). */
export function tokenizeTweetText(text: string): TextSegment[] {
const segments: TextSegment[] = [];
let last = 0;
for (const match of text.matchAll(TOKEN_PATTERN)) {
const index = match.index;
if (index > last) segments.push({ kind: "text", text: text.slice(last, index) });
const token = match[0];
segments.push({ kind: "link", text: token, href: hrefForToken(token) });
last = index + token.length;
}
if (last < text.length) segments.push({ kind: "text", text: text.slice(last) });
return segments;
}
function hrefForToken(token: string): string {
if (token.startsWith("@")) return `https://x.com/${token.slice(1)}`;
if (token.startsWith("#")) return `https://x.com/hashtag/${token.slice(1)}`;
return token;
}
const HTML_ESCAPES: Record<string, string> = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
/** Escape text for safe interpolation into HTML markup. */
export function escapeHtml(text: string): string {
return text.replace(/[&<>"']/g, (char) => HTML_ESCAPES[char] ?? char);
}
/**
* Tweet text as escaped HTML with URLs, @mentions and #hashtags linkified.
* Same output as the old React-element rendering, represented as a string
* for safe direct rendering in a tweet card.
*/
export function tweetTextHtml(text: string): string {
return tokenizeTweetText(text)
.map((segment) =>
segment.kind === "link"
? `<a href="${escapeHtml(segment.href)}" target="_blank" rel="noopener noreferrer">` +
`${escapeHtml(segment.text)}</a>`
: escapeHtml(segment.text),
)
.join("");
}
/** X-style compact counts: 999, 1.2K, 3.4M. */
export function formatCount(count: number): string {
if (count < 1000) return String(count);
if (count < 1_000_000) return `${trimZero((count / 1000).toFixed(1))}K`;
return `${trimZero((count / 1_000_000).toFixed(1))}M`;
}
function trimZero(value: string): string {
return value.endsWith(".0") ? value.slice(0, -2) : value;
}
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const REFRESH_FUZZ_MS = 100;
const MIN_REFRESH_DELAY_MS = 1000;
function relativeLabel(ageMs: number): string | undefined {
if (ageMs < 0) return undefined;
if (ageMs < 60_000) return "now";
if (ageMs < 3_600_000) return `${String(Math.floor(ageMs / 60_000))}m`;
if (ageMs < 86_400_000) return `${String(Math.floor(ageMs / 3_600_000))}h`;
return undefined;
}
/** X-style timestamps: "now", "5m", "3h", then "May 21" / "May 21, 2024". */
export function formatTweetDate(iso: string, now: Date): string {
const date = new Date(iso);
const relative = relativeLabel(now.getTime() - date.getTime());
if (relative !== undefined) return relative;
const month = MONTHS[date.getUTCMonth()] ?? "";
const label = `${month} ${String(date.getUTCDate())}`;
return date.getUTCFullYear() === now.getUTCFullYear()
? label
: `${label}, ${String(date.getUTCFullYear())}`;
}
function refreshDelay(boundaryMs: number): number {
return Math.max(MIN_REFRESH_DELAY_MS, Math.ceil(boundaryMs) + REFRESH_FUZZ_MS);
}
/** Milliseconds until formatTweetDate can change, or undefined for stable absolute dates. */
export function nextTweetDateRefreshDelay(iso: string, now: Date): number | undefined {
const date = new Date(iso);
const ageMs = now.getTime() - date.getTime();
if (!Number.isFinite(ageMs)) return undefined;
if (ageMs < 0) return refreshDelay(-ageMs);
if (ageMs < 60_000) return refreshDelay(60_000 - ageMs);
if (ageMs < 3_600_000) return refreshDelay(60_000 - (ageMs % 60_000));
if (ageMs < 86_400_000) return refreshDelay(3_600_000 - (ageMs % 3_600_000));
return undefined;
}
export type PhotoMedia = { url: string; alt: string };
function toPhoto(entry: unknown): PhotoMedia | undefined {
if (typeof entry !== "object" || entry === null) return undefined;
const item = entry as Record<string, unknown>;
if (item["type"] !== "photo" || typeof item["url"] !== "string") return undefined;
return {
url: item["url"],
alt: typeof item["alt_text"] === "string" ? item["alt_text"] : "Tweet image",
};
}
/** Up to four photo entries from a tweet's media list. */
export function photoMedia(tweet: PooledTweet): PhotoMedia[] {
const media = tweet["media"];
if (!Array.isArray(media)) return [];
return media
.map(toPhoto)
.filter((photo): photo is PhotoMedia => photo !== undefined)
.slice(0, 4);
}
export type TweetMetrics = { replies: number; retweets: number; likes: number; views: number };
/** Numeric engagement metrics with zero defaults. */
export function tweetMetrics(tweet: PooledTweet): TweetMetrics {
const metrics = tweet["metrics"];
const source =
typeof metrics === "object" && metrics !== null ? (metrics as Record<string, unknown>) : {};
const num = (key: string): number => (typeof source[key] === "number" ? source[key] : 0);
return {
replies: num("replies"),
retweets: num("retweets"),
likes: num("likes"),
views: num("views"),
};
}
/** Deterministic avatar background color per username. */
export function avatarColor(username: string): string {
let hash = 0;
for (const char of username) hash = (hash * 31 + (char.codePointAt(0) ?? 0)) % 360;
return `hsl(${String(hash)} 55% 45%)`;
}
export function displayName(tweet: PooledTweet): string {
const name = tweet.author.display_name;
return name === undefined || name === "" ? tweet.author.username : name;
}
export function isArticleTweet(tweet: PooledTweet): boolean {
return tweet["is_article"] === true;
}
export function isRetweet(tweet: PooledTweet): boolean {
return tweet["is_retweet"] === true;
}
export function quotedTweetUrl(tweet: PooledTweet): string | undefined {
const id = tweet["quoted_tweet_id"];
return typeof id === "string" && id.length > 0 ? `https://x.com/i/status/${id}` : undefined;
}
|