'use client';
import classNames from 'classnames';
import dayjs from 'dayjs';
import type { FC } from 'react';
import { useContext } from 'react';
import type { HistoryItem } from '@/context/ChatContext';
import { ChatContext } from '@/context/ChatContext';
import { SettingsContext } from '@/context/SettingsContext';
import { FULL_SPACE } from '@/utils/constants';
import { exportJSON } from '@/utils/export';
import { last } from '@/utils/last';
import { getContent } from '@/utils/message';
import { DeleteHistoryButton } from './buttons/DeleteHistoryButton';
/**
* 聊天记录
*/
export const History = () => {
const { messages, history, historyIndex } = useContext(ChatContext)!;
if (messages.length === 0 && (history === undefined || history.length === 0)) {
return
暂无聊天记录
;
}
return (
<>
{historyIndex === 'current' && }
{history?.map((_, index) => (
))}
>
);
};
/**
* 单条聊天记录
*/
export const HistoryItemComp: FC<{ historyIndex: 'current' | number; isActive: boolean }> = ({
historyIndex,
isActive,
}) => {
const { messages, history, loadHistory } = useContext(ChatContext)!;
const { settings } = useContext(SettingsContext)!;
let historyItem: HistoryItem;
if (historyIndex === 'current') {
historyItem = { model: settings.model, messages };
} else if (history === undefined) {
return null;
} else {
historyItem = history[historyIndex];
}
return (
historyIndex !== 'current' && loadHistory(historyIndex)}
>
{getContent(historyItem.messages[0])}
{historyItem.messages.length > 1 ? getContent(last(historyItem.messages)) : FULL_SPACE}
{isActive && }
);
};
/**
* 导出聊天记录
*/
export const ExportHistory = () => {
const { messages, history } = useContext(ChatContext)!;
const { settings } = useContext(SettingsContext)!;
let historyWithCurrentMessages = history ?? [];
// 如果当前有消息,则将当前消息放入聊天记录中
if (messages.length > 0) {
historyWithCurrentMessages = [{ model: settings.model, messages }, ...historyWithCurrentMessages];
}
return (
);
};