// user.ts
import { createSlice } from '@reduxjs/toolkit';
import { HYDRATE } from 'next-redux-wrapper';

// @ts-ignore
export type UserState = API.User.UserInfo;

const initialState: UserState = {
  username: '', // 示例
  avatar: '',
};

const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    setUser: (state, action) => {
      return action.payload;
    },
  },
  extraReducers: {
    // hydrated 用于获取服务端注入的state并选择更新
    [HYDRATE]: (state, action) => {
      return {
        ...action.payload.user,
      };
    },
  },
});

export const { setUser } = userSlice.actions;
export default userSlice.reducer;
