Files
syops/frontend/src/features/chat/page/useChatPageDialogs.ts
T

124 lines
3.7 KiB
TypeScript

/**
* features/chat/page/useChatPageDialogs.ts
* 用途:对话页删除确认、反馈弹窗与轻量 toast 状态。
* 依赖:vue computed/ref、chat.types。
* 逻辑:由 useChatPage 组装;ChatViewOverlays 绑定 DeleteConfirmDialog 与 toast 条。
*/
import { computed, onBeforeUnmount, ref } from 'vue';
import type { ChatMessage } from '../types/chat.types';
import type { Ref } from 'vue';
const SHIPPING_TOAST_AUTO_CLOSE_MS = 3200;
export function useChatPageDialogs(messages: Ref<ChatMessage[]>) {
const showDeleteConfirmDialog = ref(false);
const deleteConfirmMessage = ref('');
let pendingDeleteAction: (() => Promise<void> | void) | null = null;
/**
* 打开删除确认弹窗,并在确认时执行待执行动作。
* @param message - 确认说明文案
* @param onConfirm - 用户确认后执行的回调
*/
const openDeleteConfirmDialog = (
message: string,
onConfirm: () => Promise<void> | void,
) => {
deleteConfirmMessage.value = message;
pendingDeleteAction = onConfirm;
showDeleteConfirmDialog.value = true;
};
/** 关闭删除确认弹窗并清空待执行动作。 */
const closeDeleteConfirmDialog = () => {
showDeleteConfirmDialog.value = false;
deleteConfirmMessage.value = '';
pendingDeleteAction = null;
};
/** 执行删除确认回调后关闭弹窗。 */
const handleDeleteConfirm = async () => {
if (!pendingDeleteAction) return;
await pendingDeleteAction();
closeDeleteConfirmDialog();
};
const shippingToastMessage = ref('');
let shippingToastTimer: ReturnType<typeof setTimeout> | null = null;
/** 清除 toast 自动关闭计时器。 */
const clearShippingToastTimer = () => {
if (!shippingToastTimer) return;
clearTimeout(shippingToastTimer);
shippingToastTimer = null;
};
/**
* 展示非阻塞顶部 toast。
* @param message - toast 正文;空字符串视为关闭
*/
const showShippingToast = (message: string) => {
clearShippingToastTimer();
const next = String(message || '').trim();
shippingToastMessage.value = next;
if (!next) return;
shippingToastTimer = setTimeout(() => {
shippingToastMessage.value = '';
shippingToastTimer = null;
}, SHIPPING_TOAST_AUTO_CLOSE_MS);
};
/** 手动关闭发货 toast。 */
const clearShippingToast = () => {
clearShippingToastTimer();
shippingToastMessage.value = '';
};
onBeforeUnmount(() => {
clearShippingToastTimer();
});
const showGeneralFeedbackDialog = ref(false);
const activeFeedbackMessageIndex = ref<number | null>(null);
/**
* 打开消息反馈弹窗并记录当前消息下标。
* @param index - messages 数组下标
*/
const handleOpenFeedback = (index: number) => {
activeFeedbackMessageIndex.value = index;
showGeneralFeedbackDialog.value = true;
};
const currentGeneralFeedbackChatLogId = computed(() => {
if (activeFeedbackMessageIndex.value !== null) {
const msgItem = messages.value[activeFeedbackMessageIndex.value];
const id = Number(msgItem?.chat_log_id);
if (Number.isInteger(id) && id > 0) return id;
}
for (let i = messages.value.length - 1; i >= 0; i--) {
const msgItem = messages.value[i];
if (msgItem?.role === 'assistant') {
const id = Number(msgItem?.chat_log_id);
if (Number.isInteger(id) && id > 0) return id;
}
}
return null;
});
return {
showDeleteConfirmDialog,
deleteConfirmMessage,
openDeleteConfirmDialog,
closeDeleteConfirmDialog,
handleDeleteConfirm,
shippingToastMessage,
showShippingToast,
clearShippingToast,
showGeneralFeedbackDialog,
activeFeedbackMessageIndex,
handleOpenFeedback,
currentGeneralFeedbackChatLogId,
};
}