mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
983 B
TypeScript
32 lines
983 B
TypeScript
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 };
|
|
}
|