AiCampus/stores/state.ts

52 lines
1.2 KiB
TypeScript

import type { LocalMessage, VisitorRole } from "~/types";
export const useGState = defineStore("global_state", () => {
const currentRole = ref<VisitorRole>("stu");
const botName = computed(() => {
switch (currentRole.value) {
case "stu":
return "AI 辅导员";
case "tea":
return "AI 教研专家";
case "fans":
return "校园百事通";
default:
return "AI 智慧校园";
}
});
const setCurrentRole = (role: VisitorRole) => {
currentRole.value = role;
};
const messagesByRole = ref<Record<VisitorRole, LocalMessage[]>>({
stu: [],
tea: [],
fans: [],
});
const messages = computed({
get: () => messagesByRole.value[currentRole.value],
set: (value: LocalMessage[]) => {
messagesByRole.value[currentRole.value] = value;
},
});
const insertOrUpdateMessage = (message: LocalMessage) => {
const index = messages.value.findIndex((m) => m.id === message.id);
if (index >= 0) {
messages.value.splice(index, 1, message);
} else {
messages.value.push(message);
}
};
return {
currentRole,
botName,
setCurrentRole,
messagesByRole,
messages,
insertOrUpdateMessage,
};
});