File size: 1,917 Bytes
8f9c4ef | 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 | 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`;
}
|