Skip to content

Commit 4d045e5

Browse files
authored
♻️ Refactor: unify common components Part 1 #1029
2 parents 89fd7be + 7933c6a commit 4d045e5

File tree

9 files changed

+1291
-977
lines changed

9 files changed

+1291
-977
lines changed

frontend/app/[locale]/chat/internal/chatAttachment.tsx

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { chatConfig } from "@/const/chatConfig";
22
import { useState } from "react";
33
import { useTranslation } from "react-i18next";
44
import { ExternalLink } from "lucide-react";
5+
import { storageService } from "@/services/storageService";
6+
import { ChatMessageType } from "@/types/chat";
7+
import { ROLE_ASSISTANT } from "@/const/agentConfig";
8+
import log from "@/lib/logger";
59
import {
610
AiFillFileImage,
711
AiFillFilePdf,
@@ -316,3 +320,84 @@ export function ChatAttachment({
316320
</div>
317321
);
318322
}
323+
324+
/**
325+
* Asynchronously load attachment URLs for messages
326+
* @param messages Array of chat messages
327+
* @param targetConversationId Target conversation ID
328+
* @param messageManagement Message management hook instance
329+
* @param t Translation function
330+
* @returns Promise that resolves when all URLs are loaded
331+
*/
332+
export const loadAttachmentUrls = async (
333+
messages: ChatMessageType[],
334+
targetConversationId: number,
335+
messageManagement: any,
336+
t: any
337+
): Promise<void> => {
338+
// Create a copy to avoid directly modifying parameters
339+
const updatedMessages = [...messages];
340+
let hasUpdates = false;
341+
342+
// Process attachments for each message
343+
for (const message of updatedMessages) {
344+
if (message.attachments && message.attachments.length > 0) {
345+
// Get URL for each attachment
346+
for (const attachment of message.attachments) {
347+
if (attachment.object_name && !attachment.url) {
348+
try {
349+
// Get file URL
350+
const url = await storageService.getFileUrl(
351+
attachment.object_name
352+
);
353+
// Update attachment info
354+
attachment.url = url;
355+
hasUpdates = true;
356+
} catch (error) {
357+
log.error(
358+
t("chatInterface.errorFetchingAttachmentUrl", {
359+
object_name: attachment.object_name,
360+
}),
361+
error
362+
);
363+
}
364+
}
365+
}
366+
}
367+
}
368+
369+
// If there are updates, set new message array
370+
if (hasUpdates) {
371+
messageManagement.setMessages(targetConversationId, updatedMessages);
372+
}
373+
};
374+
375+
/**
376+
* Handle image loading errors
377+
* @param imageUrl Failed image URL
378+
* @param messageManagement Message management hook
379+
* @param conversationId Current conversation ID
380+
* @param t Translation function
381+
*/
382+
export const handleImageError = (
383+
imageUrl: string,
384+
messageManagement: any,
385+
conversationId: number,
386+
t: any
387+
) => {
388+
log.error(t("chatInterface.imageLoadFailed"), imageUrl);
389+
390+
// Remove failed images from messages
391+
const messages = messageManagement.getMessages(conversationId);
392+
if (messages.length === 0) return;
393+
394+
const lastMsg = messages[messages.length - 1];
395+
if (lastMsg.role === ROLE_ASSISTANT && lastMsg.images) {
396+
// Filter out failed images
397+
const updatedImages = lastMsg.images.filter((url: string) => url !== imageUrl);
398+
messageManagement.updateMessage(conversationId, lastMsg.id, {
399+
...lastMsg,
400+
images: updatedImages,
401+
});
402+
}
403+
};

frontend/app/[locale]/chat/internal/chatHelpers.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ export const deduplicateImages = (
3333

3434
return uniqueImages;
3535
};
36+
37+
// Get internationalization key based on message type
38+
export const getI18nKeyByType = (type: string): string => {
39+
const typeToKeyMap: Record<string, string> = {
40+
"progress": "chatInterface.parsingFileWithProgress",
41+
"truncation": "chatInterface.fileTruncated",
42+
};
43+
return typeToKeyMap[type] || "";
44+
};

0 commit comments

Comments
 (0)