import { computed, ref, type Ref } from 'vue';
|
import { RoleEnum } from '../../../model/types';
|
import { uniqBy } from 'lodash-es';
|
|
type usePickHistoryOption = {
|
msgList: Ref<any[]>;
|
inputValue: Ref<string>;
|
};
|
export const usePickHistory = (option: usePickHistoryOption) => {
|
const { msgList, inputValue } = option;
|
let historyPickIndex = null;
|
/** @description 是否循环选择 */
|
const loopPick = false;
|
const userMsgList = computed(() => {
|
const userMsgs = msgList.value.filter((item) => item.role === RoleEnum.user && !!item?.content?.values);
|
return userMsgs;
|
});
|
|
const getLastIndexMsg = (lastIndex: number) => {
|
const absIndex = Math.abs(lastIndex);
|
const index = absIndex % userMsgList.value.length;
|
lastIndex = -(index === 0 ? userMsgList.value.length : index);
|
return userMsgList.value[userMsgList.value.length + lastIndex];
|
};
|
|
//显示上一条消息
|
const setUpQuestion = () => {
|
let msg = null;
|
// debugger;
|
if (historyPickIndex === null) {
|
historyPickIndex = -1;
|
msg = getLastIndexMsg(historyPickIndex);
|
if (!msg) {
|
historyPickIndex = null;
|
return;
|
}
|
} else {
|
if (!loopPick && Math.abs(historyPickIndex) === userMsgList.value.length) return;
|
|
historyPickIndex--;
|
msg = getLastIndexMsg(historyPickIndex);
|
if (!msg) {
|
historyPickIndex++;
|
return;
|
}
|
}
|
inputValue.value = msg?.content?.values ?? '';
|
};
|
//显示下一条消息
|
const setDownQuestion = () => {
|
if (historyPickIndex === null) return;
|
if (!loopPick && Math.abs(historyPickIndex) === 1) return;
|
|
historyPickIndex++;
|
const msg = getLastIndexMsg(historyPickIndex);
|
if (!msg) {
|
historyPickIndex--;
|
return;
|
}
|
inputValue.value = msg?.content?.values ?? '';
|
};
|
|
return {
|
setUpQuestion,
|
setDownQuestion,
|
};
|
};
|