chat update
This commit is contained in:
@@ -298,45 +298,42 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
|
||||
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/')
|
||||
// Get all images from all messages
|
||||
const allImages: { messageIndex: number; attachmentIndex: number; url: string }[] = [];
|
||||
|
||||
chat.messages.forEach((msg, msgIndex) => {
|
||||
msg.attachments.forEach((att, attIndex) => {
|
||||
if (/\.(jpg|jpeg|png|gif|webp|svg|bmp|tiff)($|\?)/i.test(att) ||
|
||||
att.includes('/photos/') ||
|
||||
att.includes('/photo/')) {
|
||||
allImages.push({ messageIndex: msgIndex, attachmentIndex: attIndex, url: att });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (allImages.length === 0) return;
|
||||
|
||||
// Find current image index
|
||||
const currentIndex = allImages.findIndex(img =>
|
||||
img.messageIndex === selectedMessageIndex &&
|
||||
img.attachmentIndex === selectedAttachmentIndex
|
||||
);
|
||||
|
||||
let newAttachmentIndex = selectedAttachmentIndex;
|
||||
let newMessageIndex = selectedMessageIndex;
|
||||
if (currentIndex === -1) return;
|
||||
|
||||
// Calculate new index
|
||||
let newIndex;
|
||||
if (direction === 'next') {
|
||||
newAttachmentIndex++;
|
||||
if (newAttachmentIndex >= currentAttachments.length) {
|
||||
newAttachmentIndex = 0;
|
||||
newMessageIndex++;
|
||||
if (newMessageIndex >= chat.messages.length) {
|
||||
newMessageIndex = 0;
|
||||
}
|
||||
}
|
||||
newIndex = (currentIndex + 1) % allImages.length;
|
||||
} 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;
|
||||
}
|
||||
newIndex = currentIndex === 0 ? allImages.length - 1 : currentIndex - 1;
|
||||
}
|
||||
|
||||
setSelectedMessageIndex(newMessageIndex);
|
||||
setSelectedAttachmentIndex(newAttachmentIndex);
|
||||
setSelectedImage(currentAttachments[newAttachmentIndex]);
|
||||
// Update state with new image
|
||||
const newImage = allImages[newIndex];
|
||||
setSelectedMessageIndex(newImage.messageIndex);
|
||||
setSelectedAttachmentIndex(newImage.attachmentIndex);
|
||||
setSelectedImage(newImage.url);
|
||||
};
|
||||
|
||||
// Update the image click handler
|
||||
|
||||
Reference in New Issue
Block a user