refactor(ChatMessagesPane): extract message key generation logic to a utility function

This commit is contained in:
Haileyesus
2026-02-12 22:06:57 +03:00
parent 450ce93ee4
commit c193137aa5
2 changed files with 39 additions and 38 deletions

View File

@@ -0,0 +1,38 @@
import type { ChatMessage } from '../types/types';
const toMessageKeyPart = (value: unknown): string | null => {
if (typeof value !== 'string' && typeof value !== 'number') {
return null;
}
const normalized = String(value).trim();
return normalized.length > 0 ? normalized : null;
};
export const getIntrinsicMessageKey = (message: ChatMessage): string | null => {
const candidates = [
message.id,
message.messageId,
message.toolId,
message.toolCallId,
message.blobId,
message.rowid,
message.sequence,
];
for (const candidate of candidates) {
const keyPart = toMessageKeyPart(candidate);
if (keyPart) {
return `message-${message.type}-${keyPart}`;
}
}
const timestamp = new Date(message.timestamp).getTime();
if (!Number.isFinite(timestamp)) {
return null;
}
const contentPreview = typeof message.content === 'string' ? message.content.slice(0, 48) : '';
const toolName = typeof message.toolName === 'string' ? message.toolName : '';
return `message-${message.type}-${timestamp}-${toolName}-${contentPreview}`;
};

View File

@@ -7,6 +7,7 @@ import ProviderSelectionEmptyState from './ProviderSelectionEmptyState';
import type { ChatMessage, Provider } from '../../types/types';
import type { Project, ProjectSession } from '../../../../types/app';
import AssistantThinkingIndicator from './AssistantThinkingIndicator';
import { getIntrinsicMessageKey } from '../../utils/messageKeys';
interface ChatMessagesPaneProps {
scrollContainerRef: RefObject<HTMLDivElement>;
@@ -47,44 +48,6 @@ interface ChatMessagesPaneProps {
isLoading: boolean;
}
const toMessageKeyPart = (value: unknown): string | null => {
if (typeof value !== 'string' && typeof value !== 'number') {
return null;
}
const normalized = String(value).trim();
return normalized.length > 0 ? normalized : null;
};
const getIntrinsicMessageKey = (message: ChatMessage): string | null => {
const candidates = [
message.id,
message.messageId,
message.toolId,
message.toolCallId,
message.blobId,
message.rowid,
message.sequence,
];
for (const candidate of candidates) {
const keyPart = toMessageKeyPart(candidate);
if (keyPart) {
return `message-${message.type}-${keyPart}`;
}
}
const timestamp = new Date(message.timestamp).getTime();
if (!Number.isFinite(timestamp)) {
return null;
}
const contentPreview = typeof message.content === 'string' ? message.content.slice(0, 48) : '';
const toolName = typeof message.toolName === 'string' ? message.toolName : '';
return `message-${message.type}-${timestamp}-${toolName}-${contentPreview}`;
};
export default function ChatMessagesPane({
scrollContainerRef,
onWheel,