From a6079469f61b674e4e5d65dfcdd37f1ed7e50da7 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Mon, 17 Mar 2025 20:10:53 +0100 Subject: [PATCH] Update ChatDetail.tsx --- components/dashboard/ChatDetail.tsx | 109 ++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 21 deletions(-) diff --git a/components/dashboard/ChatDetail.tsx b/components/dashboard/ChatDetail.tsx index 4dd6ef9..47651da 100644 --- a/components/dashboard/ChatDetail.tsx +++ b/components/dashboard/ChatDetail.tsx @@ -12,6 +12,7 @@ import axios from "axios"; import { toast } from "sonner"; import { ArrowLeft, Send, RefreshCw, File, FileText, Image as ImageIcon, Download } from "lucide-react"; import { getCookie } from "@/lib/client-utils"; +import { ImageViewerModal } from "@/components/modals/image-viewer-modal"; interface Message { _id: string; @@ -88,6 +89,9 @@ export default function ChatDetail({ chatId }: { chatId: string }) { const [previousMessageCount, setPreviousMessageCount] = useState(0); const audioRef = useRef(null); const markReadTimeoutRef = useRef(null); + const [selectedImage, setSelectedImage] = useState(null); + const [selectedMessageIndex, setSelectedMessageIndex] = useState(null); + const [selectedAttachmentIndex, setSelectedAttachmentIndex] = useState(null); // Initialize audio element useEffect(() => { @@ -290,6 +294,58 @@ export default function ChatDetail({ chatId }: { chatId: string }) { router.push("/dashboard/chats"); }; + // Add function to handle image navigation + const handleImageNavigation = (direction: 'prev' | 'next') => { + if (!chat || selectedMessageIndex === null || selectedAttachmentIndex === null) return; + + const currentMessage = chat.messages[selectedMessageIndex]; + const currentAttachments = currentMessage.attachments.filter(att => + /\.(jpg|jpeg|png|gif|webp|svg|bmp|tiff)($|\?)/i.test(att) || + att.includes('/photos/') || + att.includes('/photo/') + ); + + let newAttachmentIndex = selectedAttachmentIndex; + let newMessageIndex = selectedMessageIndex; + + if (direction === 'next') { + newAttachmentIndex++; + if (newAttachmentIndex >= currentAttachments.length) { + newAttachmentIndex = 0; + newMessageIndex++; + if (newMessageIndex >= chat.messages.length) { + newMessageIndex = 0; + } + } + } else { + newAttachmentIndex--; + if (newAttachmentIndex < 0) { + newMessageIndex--; + if (newMessageIndex < 0) { + newMessageIndex = chat.messages.length - 1; + } + const prevMessage = chat.messages[newMessageIndex]; + const prevAttachments = prevMessage.attachments.filter(att => + /\.(jpg|jpeg|png|gif|webp|svg|bmp|tiff)($|\?)/i.test(att) || + att.includes('/photos/') || + att.includes('/photo/') + ); + newAttachmentIndex = prevAttachments.length - 1; + } + } + + setSelectedMessageIndex(newMessageIndex); + setSelectedAttachmentIndex(newAttachmentIndex); + setSelectedImage(currentAttachments[newAttachmentIndex]); + }; + + // Update the image click handler + const handleImageClick = (imageUrl: string, messageIndex: number, attachmentIndex: number) => { + setSelectedImage(imageUrl); + setSelectedMessageIndex(messageIndex); + setSelectedAttachmentIndex(attachmentIndex); + }; + if (loading) { return (
@@ -340,9 +396,9 @@ export default function ChatDetail({ chatId }: { chatId: string }) {

No messages yet. Send one to start the conversation.

) : ( - chat.messages.map((msg, index) => ( + chat.messages.map((msg, messageIndex) => (
0 && (
- {msg.attachments.map((attachment, idx) => { - // Check if attachment is an image by looking at the URL extension or paths - const isImage = /\.(jpg|jpeg|png|gif|webp)($|\?)/i.test(attachment) || - attachment.includes('/photos/') || - attachment.includes('/photo/'); + {msg.attachments.map((attachment, attachmentIndex) => { + const isImage = /\.(jpg|jpeg|png|gif|webp|svg|bmp|tiff)($|\?)/i.test(attachment) || + attachment.includes('/photos/') || + attachment.includes('/photo/'); const fileName = getFileNameFromUrl(attachment); return isImage ? ( - // Render image attachment -
+
handleImageClick(attachment, messageIndex, attachmentIndex)} + >
@@ -399,21 +457,18 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
- - {fileName} { - // Fallback for broken images - (e.target as HTMLImageElement).src = "/placeholder-image.svg"; - }} - /> - + {fileName} { + (e.target as HTMLImageElement).src = "/placeholder-image.svg"; + }} + />
) : ( // Render file attachment -
+
{getFileIcon(attachment)}
@@ -457,6 +512,18 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
+ + {/* Update the image viewer modal */} + { + setSelectedImage(null); + setSelectedMessageIndex(null); + setSelectedAttachmentIndex(null); + }} + imageUrl={selectedImage || ""} + onNavigate={handleImageNavigation} + />
); } \ No newline at end of file