| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import { create } from 'zustand';
- import type { ClientState, EmotionType } from '@/types';
- export type PageType = 'welcome' | 'loading' | 'video' | 'naming' | 'chatbot';
- interface AppState {
- // 页面状态
- currentPage: PageType;
-
- // 状态
- currentState: ClientState;
- currentRequestId: string | null;
- currentEmotion: EmotionType;
- recognizedText: string;
- errorMessage: string | null;
- isConnected: boolean;
- conversationProgress: number; // 当前进度 0-100
- progressLoopCount: number; // 已完成的整圈数量
- // Actions
- setPage: (page: PageType) => void;
- setState: (state: ClientState) => void;
- setRequestId: (id: string | null) => void;
- setEmotion: (emotion: EmotionType) => void;
- setRecognizedText: (text: string) => void;
- setError: (message: string | null) => void;
- setConnected: (connected: boolean) => void;
- updateProgressByDuration: (durationMs: number) => void; // 根据说话时长更新进度(30秒=100%)
- setProgress: (progress: number, loopCount?: number) => void; // 直接设置进度值(0-100),可选指定整圈数量
- reset: () => void;
- }
- export const useAppStore = create<AppState>((set) => ({
- // 初始状态
- currentPage: 'welcome',
- currentState: 'idle',
- currentRequestId: null,
- currentEmotion: 'neutral',
- recognizedText: '',
- errorMessage: null,
- isConnected: false,
- conversationProgress: 0,
- progressLoopCount: 0,
- // Actions
- setPage: (page) => {
- set({ currentPage: page });
-
- // 保存当前页面到 localStorage
- try {
- localStorage.setItem('Lyrria_current_page', page);
- } catch (error) {
- // 保存页面状态失败,静默处理
- }
- },
- setState: (state) => set({ currentState: state }),
- setRequestId: (id) => set({ currentRequestId: id }),
- setEmotion: (emotion) => set({ currentEmotion: emotion }),
- setRecognizedText: (text) => set({ recognizedText: text }),
- setError: (message) => set({ errorMessage: message, currentState: message ? 'error' : 'idle' }),
- setConnected: (connected) => set({ isConnected: connected }),
- updateProgressByDuration: (durationMs) => set((state) => {
- // 30秒 = 100% 进度
- const PROGRESS_FULL_DURATION_MS = 30 * 1000; // 30000ms
- // 计算本次增加的进度百分比
- const progressIncrement = (durationMs / PROGRESS_FULL_DURATION_MS) * 100;
- // 计算新的进度值
- const newProgress = state.conversationProgress + progressIncrement;
-
- // 🔍 日志:记录状态更新
- console.log('[Progress] [store] updateProgressByDuration:', {
- durationMs,
- progressIncrement: progressIncrement.toFixed(2),
- currentProgress: state.conversationProgress,
- currentLoopCount: state.progressLoopCount,
- newProgress: newProgress.toFixed(2),
- willLoop: newProgress >= 100,
- });
-
- // 如果超过100%,转一圈后再继续
- // 例如:当前90%,说话31秒,则:90% + 103.33% = 193.33% → 转一圈后剩余 93.33%
- if (newProgress >= 100) {
- const cycles = Math.floor(newProgress / 100); // 转了几圈
- const remainder = newProgress % 100; // 剩余进度
- console.log('[Progress] [store] 转圈:', {
- cycles,
- remainder: remainder.toFixed(2),
- newLoopCount: state.progressLoopCount + cycles,
- });
- return {
- conversationProgress: remainder,
- progressLoopCount: state.progressLoopCount + cycles,
- };
- }
-
- console.log('[Progress] [store] 更新进度(不转圈):', {
- newProgress: newProgress.toFixed(2),
- });
- return { conversationProgress: newProgress };
- }),
- setProgress: (progress, loopCount) =>
- set((state) => ({
- conversationProgress: Math.max(0, Math.min(100, progress)),
- progressLoopCount:
- typeof loopCount === 'number'
- ? Math.max(0, Math.floor(loopCount))
- : state.progressLoopCount,
- })),
- reset: () =>
- set({
- currentState: 'idle',
- currentRequestId: null,
- currentEmotion: 'neutral',
- recognizedText: '',
- errorMessage: null,
- }),
- }));
|