import { createSlice, PayloadAction } from '@reduxjs/toolkit'; export type CheckinExercise = { key: string; name: string; category: string; sets: number; // 组数 reps?: number; // 每组重复(计次型) durationSec?: number; // 每组时长(计时型) completed?: boolean; // 是否已完成该动作 }; export type CheckinRecord = { id: string; date: string; // YYYY-MM-DD items: CheckinExercise[]; note?: string; }; export type CheckinState = { byDate: Record; currentDate: string | null; }; const initialState: CheckinState = { byDate: {}, currentDate: null, }; function ensureRecord(state: CheckinState, date: string): CheckinRecord { if (!state.byDate[date]) { state.byDate[date] = { id: `rec_${date}`, date, items: [], }; } return state.byDate[date]; } const checkinSlice = createSlice({ name: 'checkin', initialState, reducers: { setCurrentDate(state, action: PayloadAction) { state.currentDate = action.payload; // 期望格式 YYYY-MM-DD ensureRecord(state, action.payload); }, addExercise(state, action: PayloadAction<{ date: string; item: CheckinExercise }>) { const rec = ensureRecord(state, action.payload.date); // 若同 key 已存在则覆盖参数(更接近用户“重新选择/编辑”的心智) const idx = rec.items.findIndex((it) => it.key === action.payload.item.key); const normalized: CheckinExercise = { ...action.payload.item, completed: false }; if (idx >= 0) rec.items[idx] = normalized; else rec.items.push(normalized); }, removeExercise(state, action: PayloadAction<{ date: string; key: string }>) { const rec = ensureRecord(state, action.payload.date); rec.items = rec.items.filter((it) => it.key !== action.payload.key); }, toggleExerciseCompleted(state, action: PayloadAction<{ date: string; key: string }>) { const rec = ensureRecord(state, action.payload.date); const idx = rec.items.findIndex((it) => it.key === action.payload.key); if (idx >= 0) rec.items[idx].completed = !rec.items[idx].completed; }, setNote(state, action: PayloadAction<{ date: string; note: string }>) { const rec = ensureRecord(state, action.payload.date); rec.note = action.payload.note; }, resetDate(state, action: PayloadAction) { delete state.byDate[action.payload]; }, }, }); export const { setCurrentDate, addExercise, removeExercise, toggleExerciseCompleted, setNote, resetDate } = checkinSlice.actions; export default checkinSlice.reducer;