import { Map , List } from "immutable" ; import { combineReducers } from "redux" ; import { ActionType , Action } from "./actions" ; import { State , LoginState } from "./state" ; import { Server , ServerId , Channel , ChannelId , Message } from "./discord" ; import { Store } from "redux" ; function servers ( state = Map < number , Server > ( ) , action ) { switch ( action . type ) { case ActionType . ADD_SERVER : let server = action . payload . server ; return state . set ( server . id , server ) ; default : return state ; } } function channels ( state = Map < number , Channel > ( ) , action ) { switch ( action . type ) { case ActionType . ADD_CHANNEL : let channel = action . payload . channel ; return state . set ( channel . id , channel ) ; default : return state ; } } function serversChannelsRelation ( state = Map < ServerId , List < ChannelId > > ( ) , action ) { switch ( action . type ) { case ActionType . ADD_SERVER : return state . set ( action . payload . server . id , List < ChannelId > ( ) ) ; case ActionType . ADD_CHANNEL : let channel = action . payload . channel ; return state . set ( channel . serverId , state . get ( channel . serverId ) . push ( channel . id ) ) ; default : return state ; } } function log ( state = List < Message > ( ) , action ) { switch ( action . type ) { case ActionType . CHANGE_CHANNEL : return List < Message > ( ) ; case ActionType . LOG_LINE : return state . push ( Object . freeze ( { id : - 1 , channel : - 1 , username : "***" , content : action . payload . log , timestamp : new Date ( ) } ) ) ; case ActionType . CHAT_MESSAGE : return state . push ( action . payload . message ) ; default : return state ; } } function currentChannelId ( state = - 1 , action ) { switch ( action . type ) { case ActionType . CHANGE_CHANNEL : return action . payload . channelId ; default : return state ; } } function collapsedServers ( state = List < ServerId > ( ) , action ) { switch ( action . type ) { case ActionType . TOGGLE_SERVER_COLLAPSE : let serverId = action . payload . serverId ; if ( state . includes ( serverId ) ) { return state . filterNot ( it => it == serverId ) as List < ServerId > ; } else { return state . push ( serverId ) ; } default : return state ; } } function screenHeight ( state = 0 , action ) { switch ( action . type ) { case ActionType . RESIZE_SCREEN : return action . payload . height ; default : return state ; } } function screenWidth ( state = 0 , action ) { switch ( action . type ) { case ActionType . RESIZE_SCREEN : return action . payload . width ; default : return state ; } } function scrolledUpLines ( state = 0 , action ) { switch ( action . type ) { case ActionType . CHAT_MESSAGE : if ( state != 0 ) { return state + 1 ; } else { return state ; } case ActionType . SCROLL_UP : return Math . max ( state + action . payload . lines , 0 ) ; default : return state ; } } function corked ( state = false , action ) { switch ( action . type ) { case ActionType . CORK : return true ; case ActionType . UNCORK : return false ; default : return state ; } } function channelListFocused ( state = true , action ) { switch ( action . type ) { case ActionType . CHANGE_CHANNEL : return false ; case ActionType . FOCUS_CHANNEL_LIST : return true ; default : return state ; } } function loadingChannelLogs ( state = false , action ) { switch ( action . type ) { case ActionType . CHANGE_CHANNEL : return true ; case ActionType . FINISH_LOG_LOADING : return false ; default : return state ; } } function loginState ( state = null , action ) { switch ( action . type ) { case ActionType . START_LOGIN : return LoginState . LOGGING_IN ; case ActionType . LOGGED_IN : if ( action . error ) { return LoginState . LOGIN_ERROR ; } else { return LoginState . LOGGED_IN ; } default : return state ; } } function loginError ( state = "" , action ) { switch ( action . type ) { case ActionType . LOGGED_IN : if ( action . error ) { return action . payload ; } else { return "" ; } default : return state ; } } export const filterChatMessageMiddleware = ( store ) => next => ( action ) => { if ( action . type == ActionType . CHAT_MESSAGE ) { if ( action . payload . message . channel == store . getState ( ) . currentChannelId ) { return next ( action ) ; } } else { return next ( action ) ; } } ; export const reducers = combineReducers < State > ( { servers , channels , serversChannelsRelation , log , currentChannelId , collapsedServers , screenHeight , screenWidth , scrolledUpLines , corked , channelListFocused , loadingChannelLogs , loginState , loginError } ) ;