diff --git a/components/chat/ChatMessages.tsx b/components/chat/ChatMessages.tsx index 825a8bc..24de231 100644 --- a/components/chat/ChatMessages.tsx +++ b/components/chat/ChatMessages.tsx @@ -1,6 +1,6 @@ /** * @file components/chat/ChatMessages.tsx - * @description Message history container component displaying initial channel greeting and rendering a list of individual chat messages. + * @description Message history container component displaying initial channel greeting, date separators, and a list of individual chat messages. */ "use client"; @@ -22,7 +22,40 @@ interface ChatMessagesProps { } /** - * Renders the scrollable message list along with a welcoming channel header. + * Formats a given date string or object into a human-readable label ("Today", "Yesterday", or a localized date string). + * + * @function formatDateLabel + * @param {string | Date} dateString - The date value to format. + * @returns {string} The formatted date label. + */ +function formatDateLabel(dateString: string | Date): string { + const date = new Date(dateString); + const now = new Date(); + + const isToday = + date.getDate() === now.getDate() && + date.getMonth() === now.getMonth() && + date.getFullYear() === now.getFullYear(); + + const yesterday = new Date(now); + yesterday.setDate(now.getDate() - 1); + const isYesterday = + date.getDate() === yesterday.getDate() && + date.getMonth() === yesterday.getMonth() && + date.getFullYear() === yesterday.getFullYear(); + + if (isToday) return "Today"; + if (isYesterday) return "Yesterday"; + + return date.toLocaleDateString("de-DE", { + day: "numeric", + month: "long", + year: "numeric", + }); +} + +/** + * Renders the scrollable message list along with a welcoming channel header and dynamic date separators. * * @param {ChatMessagesProps} props - The component props. * @param {string} props.channelName - The name of the active chat channel to display in the header greeting. @@ -36,8 +69,8 @@ export function ChatMessages({ currentUserId, }: ChatMessagesProps) { return ( -