Files
shakespeare/src/lib/utils.ts
T
Alex Gleason 5a456c9957 refactor: move paste image handling to Textarea component
- Add onPasteImage prop to Textarea component
- Remove createPasteHandler utility function from utils.ts
- Update Index.tsx and ChatInput.tsx to use onPasteImage prop
- onPaste takes precedence over onPasteImage when both are provided
2025-12-20 14:13:15 -06:00

73 lines
2.3 KiB
TypeScript

import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
/**
* Shuffles an array using the Fisher-Yates algorithm
*/
export function shuffleArray<T>(array: T[]): T[] {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
/**
* Formats a timestamp as a relative time string (e.g., "2 hours ago", "just now")
* @param timestamp - Unix timestamp in seconds or milliseconds, or a Date object
* @returns Human-readable relative time string
*/
export function formatRelativeTime(timestamp: number | Date): string {
const now = Date.now();
const time = timestamp instanceof Date ? timestamp.getTime() : timestamp;
// Handle both seconds and milliseconds timestamps
const timestampMs = time < 10000000000 ? time * 1000 : time;
const diffMs = now - timestampMs;
const diffSeconds = Math.floor(diffMs / 1000);
const diffMinutes = Math.floor(diffMs / (1000 * 60));
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
const diffWeeks = Math.floor(diffDays / 7);
const diffMonths = Math.floor(diffDays / 30);
const diffYears = Math.floor(diffDays / 365);
if (diffSeconds < 25) {
return 'just now';
} else if (diffSeconds < 35) {
return 'half a minute ago'
} else if (diffSeconds < 55) {
return 'less than a minute ago'
} else if (diffMinutes < 2) {
return 'a minute ago';
} else if (diffMinutes < 60) {
return `${diffMinutes} minutes ago`;
} else if (diffHours === 1) {
return 'an hour ago';
} else if (diffHours < 24) {
return `${diffHours} hours ago`;
} else if (diffDays === 1) {
return '1 day ago';
} else if (diffDays < 7) {
return `${diffDays} days ago`;
} else if (diffWeeks === 1) {
return '1 week ago';
} else if (diffWeeks < 4) {
return `${diffWeeks} weeks ago`;
} else if (diffMonths === 1) {
return '1 month ago';
} else if (diffMonths < 12) {
return `${diffMonths} months ago`;
} else if (diffYears === 1) {
return '1 year ago';
} else {
return `${diffYears} years ago`;
}
}