2024-01-25 23:44:24 +03:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { useChatContext } from "@/lib/context";
|
2024-01-29 18:55:38 +03:00
|
|
|
import { MessageMetadata, Source } from "@/lib/types/MessageMetadata";
|
2024-01-25 23:44:24 +03:00
|
|
|
|
|
|
|
import styles from "./DataPanel.module.scss";
|
2024-01-29 18:55:38 +03:00
|
|
|
import Sources from "./components/Sources/Sources";
|
|
|
|
|
|
|
|
import { ChatMessage } from "../../types";
|
2024-01-25 23:44:24 +03:00
|
|
|
|
|
|
|
const DataPanel = (): JSX.Element => {
|
|
|
|
const { messages } = useChatContext();
|
2024-01-29 18:55:38 +03:00
|
|
|
const [lastMessageMetadata, setLastMessageMetadata] =
|
|
|
|
useState<MessageMetadata>();
|
2024-01-25 23:44:24 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (messages.length > 0) {
|
2024-01-29 18:55:38 +03:00
|
|
|
const lastMessage: ChatMessage = messages[messages.length - 1];
|
|
|
|
const newSources: Source[] = (lastMessage.metadata?.sources ?? []).map(
|
|
|
|
(source: Source) => ({
|
|
|
|
...source,
|
|
|
|
frequency: 0,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const updatedSources: Source[] = [];
|
|
|
|
|
|
|
|
newSources.forEach((newSource) => {
|
|
|
|
const existingSourceIndex = updatedSources.findIndex(
|
2024-02-01 03:02:14 +03:00
|
|
|
(source) => source.name.trim() === newSource.name.trim()
|
2024-01-29 18:55:38 +03:00
|
|
|
);
|
|
|
|
if (existingSourceIndex !== -1) {
|
|
|
|
updatedSources[existingSourceIndex] = {
|
|
|
|
...updatedSources[existingSourceIndex],
|
|
|
|
frequency: updatedSources[existingSourceIndex].frequency + 1,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
updatedSources.push(newSource);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
updatedSources.sort((a, b) => b.frequency - a.frequency);
|
|
|
|
|
|
|
|
setLastMessageMetadata({
|
|
|
|
closeBrains: lastMessage.metadata?.close_brains ?? [],
|
|
|
|
sources: updatedSources,
|
|
|
|
});
|
2024-01-25 23:44:24 +03:00
|
|
|
}
|
2024-01-29 18:55:38 +03:00
|
|
|
}, [messages]);
|
2024-01-25 23:44:24 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.data_panel_wrapper}>
|
2024-01-29 18:55:38 +03:00
|
|
|
<Sources sources={lastMessageMetadata?.sources} />
|
2024-01-25 23:44:24 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DataPanel;
|