bigbuffet-rw/app/soapbox/reducers/settings.ts

53 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-01-10 14:01:24 -08:00
import { Map as ImmutableMap, fromJS } from 'immutable';
import { AnyAction } from 'redux';
2022-01-10 14:01:24 -08:00
import { ME_FETCH_SUCCESS } from 'soapbox/actions/me';
import { EMOJI_USE } from '../actions/emojis';
import { NOTIFICATIONS_FILTER_SET } from '../actions/notifications';
import { SEARCH_FILTER_SET } from '../actions/search';
import {
SETTING_CHANGE,
SETTING_SAVE,
SETTINGS_UPDATE,
FE_NAME,
} from '../actions/settings';
2020-03-27 13:59:38 -07:00
import type { Emoji } from 'soapbox/components/autosuggest_emoji';
import type { APIEntity } from 'soapbox/types/entities';
type State = ImmutableMap<string, any>;
2020-03-27 13:59:38 -07:00
const updateFrequentEmojis = (state: State, emoji: Emoji) => state.update('frequentlyUsedEmojis', ImmutableMap(), map => map.update(emoji.id, 0, (count: number) => count + 1)).set('saved', false);
2020-03-27 13:59:38 -07:00
const importSettings = (state: State, account: APIEntity) => {
2021-03-23 22:05:06 -07:00
account = fromJS(account);
const prefs = account.getIn(['pleroma', 'settings_store', FE_NAME], ImmutableMap());
return state.merge(prefs) as State;
2021-03-23 22:05:06 -07:00
};
// Default settings are in action/settings.js
//
// Settings should be accessed with `getSettings(getState()).getIn(...)`
// instead of directly from the state.
export default function settings(state: State = ImmutableMap<string, any>({ saved: true }), action: AnyAction): State {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case ME_FETCH_SUCCESS:
return importSettings(state, action.me);
case NOTIFICATIONS_FILTER_SET:
case SEARCH_FILTER_SET:
case SETTING_CHANGE:
return state
.setIn(action.path, action.value)
.set('saved', false);
case EMOJI_USE:
return updateFrequentEmojis(state, action.emoji);
case SETTING_SAVE:
return state.set('saved', true);
case SETTINGS_UPDATE:
return ImmutableMap<string, any>(fromJS(action.settings));
2022-05-11 14:06:35 -07:00
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}