From 7e24b0c3471b6d64cf7969ab28ba9b25b078a810 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 20 Oct 2025 21:44:17 +0700 Subject: [PATCH] use same date formatting funcs on messages as chat list --- src/components/dm/DMChatArea.tsx | 28 +++++++----- src/components/dm/DMConversationList.tsx | 47 +------------------ src/lib/dmUtils.ts | 57 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/src/components/dm/DMChatArea.tsx b/src/components/dm/DMChatArea.tsx index ee89f1c..b7b9e33 100644 --- a/src/components/dm/DMChatArea.tsx +++ b/src/components/dm/DMChatArea.tsx @@ -4,6 +4,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAuthor } from '@/hooks/useAuthor'; import { genUserName } from '@/lib/genUserName'; import { MESSAGE_PROTOCOL, PROTOCOL_MODE, type MessageProtocol } from '@/lib/dmConstants'; +import { formatConversationTime, formatFullDateTime } from '@/lib/dmUtils'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; @@ -22,12 +23,6 @@ interface DMChatAreaProps { className?: string; } -// Helper function outside component to avoid recreation -const formatMessageTime = (timestamp: number) => { - const date = new Date(timestamp * 1000); - return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); -}; - const MessageBubble = memo(({ message, isFromCurrentUser @@ -87,12 +82,21 @@ const MessageBubble = memo(({

)}
- - {formatMessageTime(message.created_at)} - + + + + + {formatConversationTime(message.created_at)} + + + +

{formatFullDateTime(message.created_at)}

+
+
+
{isNIP4Message && ( diff --git a/src/components/dm/DMConversationList.tsx b/src/components/dm/DMConversationList.tsx index ba5e10e..292c25d 100644 --- a/src/components/dm/DMConversationList.tsx +++ b/src/components/dm/DMConversationList.tsx @@ -3,6 +3,7 @@ import { AlertTriangle, Info, Loader2 } from 'lucide-react'; import { useDMContext } from '@/contexts/DMContext'; import { useAuthor } from '@/hooks/useAuthor'; import { genUserName } from '@/lib/genUserName'; +import { formatConversationTime, formatFullDateTime } from '@/lib/dmUtils'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; @@ -19,52 +20,6 @@ interface DMConversationListProps { onStatusClick?: () => void; } -// Helper functions for conversation timestamps (matches Signal/WhatsApp/Telegram pattern) -const formatConversationTime = (timestamp: number): string => { - const date = new Date(timestamp * 1000); - const now = new Date(); - - // Start of today (midnight) - const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()); - - // Start of yesterday - const yesterdayStart = new Date(todayStart); - yesterdayStart.setDate(yesterdayStart.getDate() - 1); - - // Start of this week (assuming week starts on Sunday, adjust if needed) - const weekStart = new Date(todayStart); - weekStart.setDate(weekStart.getDate() - weekStart.getDay()); - - if (date >= todayStart) { - // Today: Show time (e.g., "2:45 PM") - return date.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' }); - } else if (date >= yesterdayStart) { - // Yesterday - return 'Yesterday'; - } else if (date >= weekStart) { - // This week: Show day name (e.g., "Monday") - return date.toLocaleDateString(undefined, { weekday: 'short' }); - } else if (date.getFullYear() === now.getFullYear()) { - // This year: Show month and day (e.g., "Jan 15") - return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }); - } else { - // Older: Show full date (e.g., "Jan 15, 2024") - return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' }); - } -}; - -const formatFullDateTime = (timestamp: number): string => { - const date = new Date(timestamp * 1000); - return date.toLocaleString(undefined, { - weekday: 'short', - year: 'numeric', - month: 'short', - day: 'numeric', - hour: 'numeric', - minute: '2-digit' - }); -}; - const ConversationItem = memo(({ pubkey, isSelected, diff --git a/src/lib/dmUtils.ts b/src/lib/dmUtils.ts index 30c8571..8b5ba92 100644 --- a/src/lib/dmUtils.ts +++ b/src/lib/dmUtils.ts @@ -39,3 +39,60 @@ export function getConversationPartner(event: NostrEvent, userPubkey: string): s return event.pubkey; } } + +/** + * Format timestamp for display (matches Signal/WhatsApp/Telegram pattern) + * Today: Show time (e.g., "2:45 PM") + * Yesterday: "Yesterday" + * This week: Day name (e.g., "Mon") + * This year: Month and day (e.g., "Jan 15") + * Older: Full date (e.g., "Jan 15, 2024") + */ +export function formatConversationTime(timestamp: number): string { + const date = new Date(timestamp * 1000); + const now = new Date(); + + // Start of today (midnight) + const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + + // Start of yesterday + const yesterdayStart = new Date(todayStart); + yesterdayStart.setDate(yesterdayStart.getDate() - 1); + + // Start of this week (assuming week starts on Sunday, adjust if needed) + const weekStart = new Date(todayStart); + weekStart.setDate(weekStart.getDate() - weekStart.getDay()); + + if (date >= todayStart) { + // Today: Show time (e.g., "2:45 PM") + return date.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' }); + } else if (date >= yesterdayStart) { + // Yesterday + return 'Yesterday'; + } else if (date >= weekStart) { + // This week: Show day name (e.g., "Monday") + return date.toLocaleDateString(undefined, { weekday: 'short' }); + } else if (date.getFullYear() === now.getFullYear()) { + // This year: Show month and day (e.g., "Jan 15") + return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }); + } else { + // Older: Show full date (e.g., "Jan 15, 2024") + return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' }); + } +} + +/** + * Format timestamp as full date and time for tooltips + * e.g., "Mon, Jan 15, 2024, 2:45 PM" + */ +export function formatFullDateTime(timestamp: number): string { + const date = new Date(timestamp * 1000); + return date.toLocaleString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', + hour: 'numeric', + minute: '2-digit' + }); +}