- sitemap.xml, robots.txt, og:url, og:image, twitter:image, metadataBase all resolve from NEXT_PUBLIC_SITE_URL with a https://ritgram.ereboslab.com default instead of the dead gitgram.app (NXDOMAIN). - Page titles, OG copy, PWA manifest, offline.html, service-worker push notification title: Gitgram -> Ritgram. Left internal SW cache-name/tag strings (gitgram-static-, gitgram-sync, etc.) alone as low-risk code identifiers. - /[owner]/page.tsx (top-level profile route) converted to an async Server Component that calls fetchProfile() + notFound() before any HTML streams, so a GET on any nonexistent username now returns a real 404 instead of a 200 loading-shell (the same pattern [owner]/[name]/page.tsx already used one level down). - apps/ritgram/backend: matching Gitgram->Ritgram string rename sweep across controllers/services/config comments and defaults (EMAIL_FROM, AITM_API_URL description, etc.) — no behavior change. - apps/ritgram/mobile: matching user-visible string rename sweep. tsc --noEmit clean on both web and backend. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
194 lines
5.8 KiB
TypeScript
194 lines
5.8 KiB
TypeScript
import { type ClassValue, clsx } from 'clsx'
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tailwind class merging (existing helper — keep as-is)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function cn(...inputs: ClassValue[]): string {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Date / time formatting
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Returns a human-readable relative time string.
|
|
*
|
|
* Examples: "just now", "5m ago", "2h ago", "3d ago", "Jan 5", "Jan 5, 2023"
|
|
*/
|
|
export function formatRelativeTime(date: string | Date): string {
|
|
const now = Date.now()
|
|
const then = typeof date === 'string' ? new Date(date).getTime() : date.getTime()
|
|
const diffMs = now - then
|
|
|
|
const MINUTE = 60_000
|
|
const HOUR = 60 * MINUTE
|
|
const DAY = 24 * HOUR
|
|
const WEEK = 7 * DAY
|
|
|
|
if (diffMs < MINUTE) return 'just now'
|
|
if (diffMs < HOUR) return `${Math.floor(diffMs / MINUTE)}m ago`
|
|
if (diffMs < DAY) return `${Math.floor(diffMs / HOUR)}h ago`
|
|
if (diffMs < WEEK) return `${Math.floor(diffMs / DAY)}d ago`
|
|
|
|
const d = typeof date === 'string' ? new Date(date) : date
|
|
const options: Intl.DateTimeFormatOptions =
|
|
d.getFullYear() === new Date().getFullYear()
|
|
? { month: 'short', day: 'numeric' }
|
|
: { month: 'short', day: 'numeric', year: 'numeric' }
|
|
|
|
return d.toLocaleDateString('en-US', options)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Number formatting
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Formats a number with abbreviated suffixes.
|
|
*
|
|
* Examples: 1200 → "1.2k", 45000 → "45k", 1_200_000 → "1.2M"
|
|
*/
|
|
export function formatNumber(n: number): string {
|
|
if (n >= 1_000_000) {
|
|
const value = n / 1_000_000
|
|
return `${value % 1 === 0 ? value : value.toFixed(1)}M`
|
|
}
|
|
if (n >= 1_000) {
|
|
const value = n / 1_000
|
|
return `${value % 1 === 0 ? value : value.toFixed(1)}k`
|
|
}
|
|
return String(n)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Programming language colors (GitHub-style)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const LANGUAGE_COLORS: Record<string, string> = {
|
|
TypeScript: '#3178c6',
|
|
JavaScript: '#f1e05a',
|
|
Python: '#3572A5',
|
|
Rust: '#dea584',
|
|
Go: '#00ADD8',
|
|
Java: '#b07219',
|
|
'C++': '#f34b7d',
|
|
C: '#555555',
|
|
'C#': '#178600',
|
|
Ruby: '#701516',
|
|
PHP: '#4F5D95',
|
|
Swift: '#F05138',
|
|
Kotlin: '#A97BFF',
|
|
Dart: '#00B4AB',
|
|
Scala: '#c22d40',
|
|
Shell: '#89e051',
|
|
HTML: '#e34c26',
|
|
CSS: '#563d7c',
|
|
Vue: '#41b883',
|
|
Svelte: '#ff3e00',
|
|
Elixir: '#6e4a7e',
|
|
Haskell: '#5e5086',
|
|
Clojure: '#db5855',
|
|
Lua: '#000080',
|
|
R: '#198CE7',
|
|
MATLAB: '#e16737',
|
|
}
|
|
|
|
/**
|
|
* Returns the GitHub-style hex color for a programming language.
|
|
* Falls back to a neutral grey when the language is unknown.
|
|
*/
|
|
export function getLanguageColor(language: string): string {
|
|
return LANGUAGE_COLORS[language] ?? '#8b949e'
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// String utilities
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Truncates a string to `length` characters, appending "…" when truncated.
|
|
*/
|
|
export function truncate(str: string, length: number): string {
|
|
if (str.length <= length) return str
|
|
return `${str.slice(0, length)}…`
|
|
}
|
|
|
|
/**
|
|
* Extracts initials from a full name (up to two characters).
|
|
*
|
|
* Examples: "John Doe" → "JD", "Alice" → "A", " " → "?"
|
|
*/
|
|
export function getInitials(name: string): string {
|
|
const parts = name.trim().split(/\s+/).filter(Boolean)
|
|
if (parts.length === 0) return '?'
|
|
if (parts.length === 1) return parts[0].charAt(0).toUpperCase()
|
|
return (parts[0].charAt(0) + parts[parts.length - 1].charAt(0)).toUpperCase()
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// URL helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Builds the Ritgram in-app URL for a repository.
|
|
*
|
|
* Example: buildRepoUrl("torvalds", "linux") → "/torvalds/linux"
|
|
*/
|
|
export function buildRepoUrl(owner: string, name: string): string {
|
|
return `/${owner}/${name}`
|
|
}
|
|
|
|
/**
|
|
* Returns true when the string looks like a valid GitHub repository or profile URL.
|
|
*
|
|
* Accepts both `https://github.com/owner` and `https://github.com/owner/repo`.
|
|
*/
|
|
export function isValidGitHubUrl(url: string): boolean {
|
|
try {
|
|
const parsed = new URL(url)
|
|
if (parsed.hostname !== 'github.com') return false
|
|
// Path must have at least one non-empty segment after the leading slash
|
|
const segments = parsed.pathname.split('/').filter(Boolean)
|
|
return segments.length >= 1
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Debounce
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Returns a debounced version of `fn` that delays invocation until `delay` ms
|
|
* have elapsed since the last call.
|
|
*
|
|
* The returned function also exposes a `.cancel()` method to clear the pending timer.
|
|
*/
|
|
export function debounce<T extends (...args: unknown[]) => unknown>(
|
|
fn: T,
|
|
delay: number
|
|
): ((...args: Parameters<T>) => void) & { cancel: () => void } {
|
|
let timer: ReturnType<typeof setTimeout> | null = null
|
|
|
|
const debounced = (...args: Parameters<T>): void => {
|
|
if (timer !== null) clearTimeout(timer)
|
|
timer = setTimeout(() => {
|
|
timer = null
|
|
fn(...args)
|
|
}, delay)
|
|
}
|
|
|
|
debounced.cancel = (): void => {
|
|
if (timer !== null) {
|
|
clearTimeout(timer)
|
|
timer = null
|
|
}
|
|
}
|
|
|
|
return debounced
|
|
}
|