import { create } from 'zustand' import { subscribeWithSelector } from 'zustand/middleware' import type { ReviewSessionStore } from '@/archive/lib/types/review' export const useReviewSessionStore = create()( subscribeWithSelector((set, get) => ({ // 初始狀態 mounted: false, isLoading: false, error: null, currentCard: null, currentCardIndex: 0, dueCards: [], totalCards: 0, mode: 'flip-memory', isAutoSelecting: false, showNoDueCards: false, showComplete: false, completedCards: 0, correctAnswers: 0, sessionStartTime: undefined, // 基本操作 setMounted: (mounted) => set({ mounted }), setLoading: (loading) => set({ isLoading: loading }), setError: (error) => set({ error }), // 卡片操作 setCurrentCard: (card) => set({ currentCard: card }), setCurrentCardIndex: (index) => set({ currentCardIndex: index }), setDueCards: (cards) => set({ dueCards: cards, totalCards: cards.length }), // 模式操作 setMode: (mode) => set({ mode }), setAutoSelecting: (auto) => set({ isAutoSelecting: auto }), // UI 操作 setShowNoDueCards: (show) => set({ showNoDueCards: show }), setShowComplete: (show) => set({ showComplete: show }), // 會話操作 startSession: () => set({ sessionStartTime: new Date(), completedCards: 0, correctAnswers: 0, currentCardIndex: 0, showComplete: false, showNoDueCards: false }), resetSession: () => set({ currentCard: null, currentCardIndex: 0, error: null, mounted: false, isLoading: false, dueCards: [], totalCards: 0, completedCards: 0, correctAnswers: 0, sessionStartTime: undefined, showComplete: false, showNoDueCards: false }), nextCard: () => { const state = get() const nextIndex = state.currentCardIndex + 1 if (nextIndex < state.dueCards.length) { set({ currentCardIndex: nextIndex, currentCard: state.dueCards[nextIndex] }) } else { set({ showComplete: true }) } }, previousCard: () => { const state = get() const prevIndex = state.currentCardIndex - 1 if (prevIndex >= 0) { set({ currentCardIndex: prevIndex, currentCard: state.dueCards[prevIndex] }) } }, // 答題操作 submitAnswer: (isCorrect) => { const state = get() set({ completedCards: state.completedCards + 1, correctAnswers: state.correctAnswers + (isCorrect ? 1 : 0) }) // 自動前進到下一張卡片 get().nextCard() }, skipCard: () => { get().nextCard() } })) )