import { create } from 'zustand'; /** * CharacterCard UI 状态 Store * 管理角色卡列表的 UI 交互状态(非持久化) */ const useCharacterCardUIStore = create((set) => ({ // ==================== 状态 ==================== // 筛选标签数组 - 支持多标签交集筛选 // 格式: ['include:tag1', 'exclude:tag2', 'include:tag3'] filterTags: [], // 是否处于编辑模式 isEditing: false, // 编辑表单数据 editForm: null, // 当前页码 currentPage: 1, // 每页显示数量 pageSize: 12, // ==================== Actions ==================== /** * 设置筛选标签(三次切换:无筛选 -> 包含 -> 排除 -> 无筛选) * @param {string} tag - 标签名 */ setFilterTag: (tag) => { set((state) => { const currentFilter = state.filterTag; // 如果点击的是同一个标签,循环切换状态 if (currentFilter && (currentFilter === tag || currentFilter === `include:${tag}` || currentFilter === `exclude:${tag}`)) { if (currentFilter === tag || currentFilter === `include:${tag}`) { // 从包含切换到排除 return { filterTag: `exclude:${tag}`, currentPage: 1 }; } else { // 从排除切换到无筛选 return { filterTag: '', currentPage: 1 }; } } else { // 点击新标签,设置为包含模式 return { filterTag: `include:${tag}`, currentPage: 1 }; } }); }, /** * 清空筛选 */ clearFilter: () => { set({ filterTag: '', currentPage: 1 }); }, /** * 进入编辑模式 * @param {Object} character - 角色数据 */ startEditing: (character) => { set({ isEditing: true, editForm: { name: character.name, description: character.description || '', personality: character.personality || '', scenario: character.scenario || '', first_mes: character.first_mes || '', mes_example: character.mes_example || '', categories: character.categories || [], tags: character.tags || [], worldInfoId: character.worldInfoId || null } }); }, /** * 退出编辑模式 */ cancelEditing: () => { set({ isEditing: false, editForm: null }); }, /** * 更新编辑表单字段 * @param {string} field - 字段名 * @param {*} value - 字段值 */ updateEditForm: (field, value) => { set((state) => ({ editForm: { ...state.editForm, [field]: value } })); }, /** * 设置页码 * @param {number} page - 页码 */ setCurrentPage: (page) => { set({ currentPage: page }); }, /** * 设置每页显示数量 * @param {number} size - 每页数量 */ setPageSize: (size) => { set({ pageSize: size, currentPage: 1 }); // 重置页码 }, /** * 下一页 */ nextPage: () => { set((state) => ({ currentPage: state.currentPage + 1 })); }, /** * 上一页 */ prevPage: () => { set((state) => ({ currentPage: Math.max(1, state.currentPage - 1) })); }, /** * 重置所有 UI 状态 */ reset: () => { set({ filterTag: '', isEditing: false, editForm: null, currentPage: 1, pageSize: 12 }); } })); export default useCharacterCardUIStore;