diff --git a/src/components/Chat.tsx b/src/components/Chat.tsx index 903f948..6f4f733 100644 --- a/src/components/Chat.tsx +++ b/src/components/Chat.tsx @@ -5,6 +5,7 @@ import { TypingIndicator } from './TypingIndicator'; import type { ChatMessage, ConnectionStatus } from '../types'; import { Bot, ArrowDown } from 'lucide-react'; import { useT } from '../hooks/useLocale'; +import { getLocale, type TranslationKey } from '../lib/i18n'; interface Props { messages: ChatMessage[]; @@ -41,6 +42,24 @@ function hasStreamedText(messages: ChatMessage[]): boolean { return last.blocks.some(b => b.type === 'text' && b.text.trim().length > 0) || (last.content?.trim().length > 0); } +function formatDateSeparator(ts: number, t: (k: TranslationKey) => string): string { + const date = new Date(ts); + const now = new Date(); + const locale = getLocale(); + const bcp47 = locale === 'fr' ? 'fr-FR' : 'en-US'; + + if (date.toDateString() === now.toDateString()) return t('time.today'); + const yesterday = new Date(now); + yesterday.setDate(yesterday.getDate() - 1); + if (date.toDateString() === yesterday.toDateString()) return t('time.yesterday'); + return date.toLocaleDateString(bcp47, { weekday: 'long', day: 'numeric', month: 'long', year: date.getFullYear() !== now.getFullYear() ? 'numeric' : undefined }); +} + +function getDateKey(ts: number): string { + const d = new Date(ts); + return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`; +} + /** Threshold in pixels — if the user is within this distance of the bottom, auto-scroll */ const SCROLL_THRESHOLD = 150; @@ -111,9 +130,26 @@ export function Chat({ messages, isGenerating, status, onSend, onAbort }: Props)
{t('chat.welcomeSub')}
)} - {messages.filter(hasVisibleContent).map(msg => ( - - ))} + {(() => { + let lastDateKey = ''; + return messages.filter(hasVisibleContent).map(msg => { + const dk = getDateKey(msg.timestamp); + const showSep = dk !== lastDateKey; + lastDateKey = dk; + return ( +
+ {showSep && ( +
+
+ {formatDateSeparator(msg.timestamp, t)} +
+
+ )} + +
+ ); + }); + })()} {showTyping && }
diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts index 2d3fc70..8652589 100644 --- a/src/lib/i18n.ts +++ b/src/lib/i18n.ts @@ -63,6 +63,7 @@ const en = { // Timestamps 'time.yesterday': 'Yesterday', + 'time.today': 'Today', // Keyboard shortcuts 'shortcuts.title': 'Keyboard Shortcuts', @@ -131,6 +132,7 @@ const fr: Record = { 'message.retry': 'Renvoyer le message', 'time.yesterday': 'Hier', + 'time.today': "Aujourd'hui", 'shortcuts.title': 'Raccourcis clavier', 'shortcuts.send': 'Envoyer le message',