chore: migrate to reducer pattern instead of useState

This commit is contained in:
Alexander Daichendt 2025-02-12 11:33:48 +01:00
parent 18dd8b83f0
commit 7150ca87c3
7 changed files with 85 additions and 29 deletions

View file

@ -0,0 +1,28 @@
import { ChatAction, ChatState } from "../contexts/ChatContext";
export function chatReducer(state: ChatState, action: ChatAction): ChatState {
switch (action.type) {
case "ADD_MESSAGE":
return {
...state,
messages: [...state.messages, action.payload],
};
case "SET_USER":
return {
...state,
currentUser: action.payload,
};
case "SET_CONNECTED":
return {
...state,
isConnected: action.payload,
};
case "CLEAR_MESSAGES":
return {
...state,
messages: [],
};
default:
return state;
}
}