diff --git a/src/components/chat/ChatMessage.tsx b/src/components/chat/ChatMessage.tsx index 7bb1e5a..0213f07 100644 --- a/src/components/chat/ChatMessage.tsx +++ b/src/components/chat/ChatMessage.tsx @@ -5,7 +5,12 @@ import { } from "./DyadMarkdownParser"; import { motion } from "framer-motion"; import { useStreamChat } from "@/hooks/useStreamChat"; -import { CheckCircle, XCircle } from "lucide-react"; +import { CheckCircle, XCircle, Clock, GitCommit } from "lucide-react"; +import { formatDistanceToNow, format } from "date-fns"; +import { useVersions } from "@/hooks/useVersions"; +import { useAtomValue } from "jotai"; +import { selectedAppIdAtom } from "@/atoms/appAtoms"; +import { useMemo } from "react"; interface ChatMessageProps { message: Message; @@ -14,13 +19,46 @@ interface ChatMessageProps { const ChatMessage = ({ message, isLastMessage }: ChatMessageProps) => { const { isStreaming } = useStreamChat(); + const appId = useAtomValue(selectedAppIdAtom); + const { versions: liveVersions } = useVersions(appId); + // Find the version that was active when this message was sent + const messageVersion = useMemo(() => { + if ( + message.role === "assistant" && + message.commitHash && + liveVersions.length + ) { + return ( + liveVersions.find( + (version) => + message.commitHash && + version.oid.slice(0, 7) === message.commitHash.slice(0, 7), + ) || null + ); + } + return null; + }, [message.commitHash, message.role, liveVersions]); + + // Format the message timestamp + const formatTimestamp = (timestamp: string | Date) => { + const now = new Date(); + const messageTime = new Date(timestamp); + const diffInHours = + (now.getTime() - messageTime.getTime()) / (1000 * 60 * 60); + if (diffInHours < 24) { + return formatDistanceToNow(messageTime, { addSuffix: true }); + } else { + return format(messageTime, "MMM d, yyyy 'at' h:mm a"); + } + }; + return (