import { Reducer } from 'redux' ; import Status from 'constants/status' ; import { Emote , EmoteProviderPrefix } from 'libs/EmotesProvider' ; import { SerializedRoomState } from 'libs/RoomState' ; import { createAction } from 'utils/redux' ; /**
 * Actions types.
 */ export enum Actions { SET_CHANNEL = 'app/SET_CHANNEL' , UPDATE_STATUS = 'app/UPDATE_STATUS' , UPDATE_ROOM_STATE = 'app/UPDATE_ROOM_STATE' , SET_SHOULD_READ_CHANGELOG = 'app/SET_SHOULD_READ_CHANGELOG' , ADD_TO_HISTORY = 'app/ADD_TO_HISTORY' , UPDATE_HISTORY_INDEX = 'app/UPDATE_HISTORY_INDEX' , UPDATE_EMOTES = 'app/UPDATE_EMOTES' , RESET_APP_STATE = 'app/RESET_APP_STATE' , SET_LAST_WHISPER_SENDER = 'app/SET_LAST_WHISPER_SENDER' } /**
 * Initial state.
 */ export const initialState = { channel : null , emotes : { } , history : [ ] , historyIndex : - 1 , lastWhisperSender : '' , roomState : null , shouldReadChangelog : false , status : Status . Default , } ; param state param action return const appReducer = ( state = initialState , action ) => { switch ( action . type ) { case Actions . SET_CHANNEL : { return { ... state , channel : action . payload . channel , } ; } case Actions . UPDATE_STATUS : { return { ... state , status : action . payload . status , } ; } case Actions . UPDATE_ROOM_STATE : { return { ... state , roomState : { ... state . roomState , ... action . payload . state , } , } ; } case Actions . SET_SHOULD_READ_CHANGELOG : { return { ... state , shouldReadChangelog : action . payload . shouldRead , } ; } case Actions . ADD_TO_HISTORY : { return { ... state , history : [ action . payload . message , ... state . history ] , historyIndex : initialState . historyIndex , } ; } case Actions . UPDATE_HISTORY_INDEX : { return { ... state , historyIndex : action . payload . index , } ; } case Actions . UPDATE_EMOTES : { const { prefix , emotes } = action . payload ; return { ... state , emotes : { ... state . emotes , [ prefix ] : emotes } , } ; } case Actions . RESET_APP_STATE : { return { ... initialState , channel : state . channel , shouldReadChangelog : state . shouldReadChangelog , } ; } case Actions . SET_LAST_WHISPER_SENDER : { return { ... state , lastWhisperSender : action . payload . username , } ; } default : { return state ; } } } ; export default appReducer ; param channel return export const setChannel = ( channel ) => createAction ( Actions . SET_CHANNEL , { channel , } ) ; param status return export const updateStatus = ( status ) => createAction ( Actions . UPDATE_STATUS , { status , } ) ; param state return export const updateRoomState = ( state ) => createAction ( Actions . UPDATE_ROOM_STATE , { state , } ) ; param shouldRead return export const setShouldReadChangelog = ( shouldRead ) => createAction ( Actions . SET_SHOULD_READ_CHANGELOG , { shouldRead , } ) ; param message return export const addToHistory = ( message ) => createAction ( Actions . ADD_TO_HISTORY , { message , } ) ; param index return export const updateHistoryIndex = ( index ) => createAction ( Actions . UPDATE_HISTORY_INDEX , { index , } ) ; param prefix param emotes return export const updateEmotes = ( prefix , emotes ) => createAction ( Actions . UPDATE_EMOTES , { emotes , prefix , } ) ; return export const resetAppState = ( ) => createAction ( Actions . RESET_APP_STATE ) ; param username return export const setLastWhisperSender = ( username ) => createAction ( Actions . SET_LAST_WHISPER_SENDER , { username , } ) ; /**
 * App actions.
 */ export type AppActions = ReturnType < typeof setChannel > | ReturnType < typeof updateStatus > | ReturnType < typeof updateRoomState > | ReturnType < typeof setShouldReadChangelog > | ReturnType < typeof addToHistory > | ReturnType < typeof updateHistoryIndex > | ReturnType < typeof updateEmotes > | ReturnType < typeof resetAppState > | ReturnType < typeof setLastWhisperSender > ; /**
 * App state.
 */ export type AppState = { /**
     * The current channel.
     */ channel : string | null ; /**
     * Connection status.
     */ status : Status ; /**
     * Room state.
     */ roomState : SerializedRoomState | null ; /**
     * Defines if the user is using a new version of the application and should read the associated changelog.
     */ shouldReadChangelog : boolean ; /**
     * Messages sent by the current user.
     */ history : string [ ] ; /**
     * Current position in the history.
     */ historyIndex : number ; /**
     * Emotes.
     */ emotes : Record < string , Emote [ ] > ; /**
     * The username of the last user that sent us a whisper.
     */ lastWhisperSender : string ; } ;