mirror of https://github.com/msgbyte/tailchat
refactor: 会话消息获取
parent
06fc8083ff
commit
646e62f63f
@ -0,0 +1,36 @@
|
||||
import { request } from '../api/request';
|
||||
|
||||
export interface ChatMessage {
|
||||
_id: string;
|
||||
|
||||
content: string;
|
||||
|
||||
author?: string;
|
||||
|
||||
groupId?: string;
|
||||
|
||||
converseId: string;
|
||||
|
||||
createdAt?: string;
|
||||
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话消息
|
||||
* @param converseId 会话ID
|
||||
* @param startId 开始ID
|
||||
*/
|
||||
export async function fetchConverseMessage(
|
||||
converseId: string,
|
||||
startId?: string
|
||||
): Promise<ChatMessage[]> {
|
||||
const { data } = await request.get('/api/chat/message/fetchConverseMessage', {
|
||||
params: {
|
||||
converseId,
|
||||
startId,
|
||||
},
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
import { getCachedConverseInfo } from '../../cache/cache';
|
||||
import { useAsync } from '../../hooks/useAsync';
|
||||
import { fetchConverseMessage } from '../../model/message';
|
||||
import { chatActions } from '../slices';
|
||||
import { useAppDispatch, useAppSelector } from './useAppSelector';
|
||||
|
||||
/**
|
||||
* 会话消息管理
|
||||
*/
|
||||
export function useConverseMessage(converseId: string) {
|
||||
const converse = useAppSelector((state) => state.chat.converses[converseId]);
|
||||
const dispatch = useAppDispatch();
|
||||
const messages = converse?.messages ?? [];
|
||||
|
||||
const { loading, error } = useAsync(async () => {
|
||||
if (!converse) {
|
||||
const converse = await getCachedConverseInfo(converseId);
|
||||
dispatch(chatActions.setConverseInfo(converse));
|
||||
|
||||
const messages = await fetchConverseMessage(converseId);
|
||||
dispatch(
|
||||
chatActions.appendConverseMessage({
|
||||
converseId,
|
||||
messages,
|
||||
})
|
||||
);
|
||||
}
|
||||
}, [converse, converseId]);
|
||||
|
||||
return { messages, loading, error };
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import React from 'react';
|
||||
import { Alert } from 'antd';
|
||||
|
||||
/**
|
||||
* 用于接口错误显示的组件
|
||||
*/
|
||||
export const AlertErrorView: React.FC<{
|
||||
error: Error;
|
||||
}> = React.memo((props) => {
|
||||
return (
|
||||
<Alert
|
||||
type="error"
|
||||
message={String(props.error.name)}
|
||||
description={String(props.error.message)}
|
||||
/>
|
||||
);
|
||||
});
|
||||
AlertErrorView.displayName = 'AlertErrorView';
|
Loading…
Reference in New Issue