| import { clsx, type ClassValue } from 'clsx'; |
| import { twMerge } from 'tailwind-merge'; |
|
|
| export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } |
|
|
| export function debounce<T extends (...args: any[]) => any>(fn: T, delay: number) { |
| let timeoutId: NodeJS.Timeout; |
| return (...args: Parameters<T>) => { clearTimeout(timeoutId); timeoutId = setTimeout(() => fn(...args), delay); }; |
| } |
|
|
| export function throttle<T extends (...args: any[]) => any>(fn: T, limit: number) { |
| let inThrottle: boolean; |
| return (...args: Parameters<T>) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => { inThrottle = false; }, limit); } }; |
| } |
|
|
| export function formatRelativeTime(dateStr: string): string { |
| const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000); |
| if (seconds < 60) return 'just now'; |
| if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`; |
| if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`; |
| return `${Math.floor(seconds / 86400)}d ago`; |
| } |
|
|
| export function getFileIcon(filename: string): string { |
| const ext = filename.split('.').pop()?.toLowerCase(); |
| const icons: Record<string, string> = { js: 'π', ts: 'π', py: 'π', cpp: 'βοΈ', java: 'β', html: 'π', css: 'π¨', json: 'π', md: 'π' }; |
| return icons[ext || ''] || 'π'; |
| } |
|
|
| export async function copyToClipboard(text: string): Promise<boolean> { |
| try { await navigator.clipboard.writeText(text); return true; } catch { return false; } |
| } |
|
|
| export function generateInviteLink(inviteCode: string): string { |
| const baseUrl = typeof window !== 'undefined' ? window.location.origin : ''; |
| return `${baseUrl}/join/${inviteCode}`; |
| } |
|
|
| export function formatDuration(ms: number): string { |
| if (ms < 1000) return `${ms}ms`; |
| if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`; |
| return `${Math.floor(ms / 60000)}m ${Math.floor((ms % 60000) / 1000)}s`; |
| } |
|
|