All files / hooks usePetStore.ts

100% Statements 18/18
75% Branches 3/4
100% Functions 7/7
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55        2x     2x 2x     2x 456x 456x 152x         2x 2x     2x     2x                       2x           1x 1x 1x         6x 6x    
import { create } from 'zustand';
import coldFactsData from '../../assets/cold_facts/ColdFacts.json';
 
// 提示语 - 出现频率较高
const TIP_MESSAGE = '长按可以将我隐藏哦~';
 
// 创建包含高频提示的消息列表
const createMessagesWithTip = () => {
  const messages: string[] = [];
 
  // 每3条冷知识插入一条提示
  coldFactsData.forEach((fact, index) => {
    messages.push(fact);
    if ((index + 1) % 3 === 0) {
      messages.push(TIP_MESSAGE);
    }
  });
 
  // 确保提示语在开头也出现
  Eif (messages[0] !== TIP_MESSAGE) {
    messages.unshift(TIP_MESSAGE);
  }
 
  return messages;
};
 
const allMessages = createMessagesWithTip();
 
interface PetState {
  currentFactIndex: number;
  currentFact: string;
  isTalking: boolean;
  isVisible: boolean;
  nextFact: () => void;
  setTalking: (talking: boolean) => void;
  setVisible: (visible: boolean) => void;
}
 
export const usePetStore = create<PetState>((set) => ({
  currentFactIndex: 0,
  currentFact: allMessages[0],
  isTalking: true,
  isVisible: true,
  nextFact: () =>
    set((state) => {
      const nextIndex = (state.currentFactIndex + 1) % allMessages.length;
      return {
        currentFactIndex: nextIndex,
        currentFact: allMessages[nextIndex],
      };
    }),
  setTalking: (talking: boolean) => set({ isTalking: talking }),
  setVisible: (visible: boolean) => set({ isVisible: visible }),
}));