mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-17 18:35:19 +02:00
- Add formatThreadTimestamp() to surfsense_web/lib/format-date.ts - Use shared helper in AllPrivateChatsSidebar and AllSharedChatsSidebar - Remove unused date-fns format import from both sidebar files - Centralises timestamp formatting policy for future i18n/relative-time changes
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { differenceInDays, differenceInMinutes, format, isToday, isYesterday } from "date-fns";
|
|
|
|
/**
|
|
* Format a date string as a human-readable relative time
|
|
* - < 1 min: "Just now"
|
|
* - < 60 min: "15m ago"
|
|
* - Today: "Today, 2:30 PM"
|
|
* - Yesterday: "Yesterday, 2:30 PM"
|
|
* - < 7 days: "3d ago"
|
|
* - Older: "Jan 15, 2026"
|
|
*/
|
|
export function formatRelativeDate(dateString: string): string {
|
|
const date = new Date(dateString);
|
|
const now = new Date();
|
|
const minutesAgo = differenceInMinutes(now, date);
|
|
const daysAgo = differenceInDays(now, date);
|
|
|
|
if (minutesAgo < 1) return "Just now";
|
|
if (minutesAgo < 60) return `${minutesAgo}m ago`;
|
|
if (isToday(date)) return `Today, ${format(date, "h:mm a")}`;
|
|
if (isYesterday(date)) return `Yesterday, ${format(date, "h:mm a")}`;
|
|
if (daysAgo < 7) return `${daysAgo}d ago`;
|
|
return format(date, "MMM d, yyyy");
|
|
}
|
|
|
|
/**
|
|
* Format a thread's last-updated timestamp for the chats sidebars.
|
|
* Example: "Mar 23, 2026 at 4:30 PM"
|
|
*/
|
|
export function formatThreadTimestamp(dateString: string): string {
|
|
return format(new Date(dateString), "MMM d, yyyy 'at' h:mm a");
|
|
}
|