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

169 lines
4.5 KiB
TypeScript
Raw Normal View History

import { Record as ImmutableRecord } from 'immutable';
import { combineReducers } from 'redux-immutable';
2020-09-27 17:09:35 -07:00
import { AUTH_LOGGED_OUT } from 'soapbox/actions/auth';
2022-11-15 12:48:54 -08:00
import * as BuildConfig from 'soapbox/build-config';
2022-12-04 14:58:13 -08:00
import entities from 'soapbox/entity-store/reducer';
2022-11-16 05:32:32 -08:00
import accounts_meta from './accounts-meta';
import admin from './admin';
import admin_announcements from './admin-announcements';
2022-11-15 12:39:43 -08:00
import admin_log from './admin-log';
import admin_user_index from './admin-user-index';
import aliases from './aliases';
import announcements from './announcements';
import auth from './auth';
import backups from './backups';
2022-11-15 12:39:43 -08:00
import chat_message_lists from './chat-message-lists';
import chat_messages from './chat-messages';
import chats from './chats';
2020-03-27 13:59:38 -07:00
import compose from './compose';
import compose_event from './compose-event';
import contexts from './contexts';
import conversations from './conversations';
2022-11-15 12:39:43 -08:00
import custom_emojis from './custom-emojis';
import domain_lists from './domain-lists';
import dropdown_menu from './dropdown-menu';
2020-03-27 13:59:38 -07:00
import filters from './filters';
import followed_tags from './followed-tags';
import group_memberships from './group-memberships';
import group_relationships from './group-relationships';
import groups from './groups';
import history from './history';
2020-04-01 13:05:52 -07:00
import instance from './instance';
2022-11-15 12:39:43 -08:00
import listAdder from './list-adder';
import listEditor from './list-editor';
import lists from './lists';
import locations from './locations';
import me from './me';
import meta from './meta';
import modals from './modals';
import mutes from './mutes';
import notifications from './notifications';
2022-05-02 13:55:52 -07:00
import onboarding from './onboarding';
2023-06-25 10:37:37 -07:00
import patron from './patron';
2022-11-15 12:39:43 -08:00
import pending_statuses from './pending-statuses';
import polls from './polls';
2022-11-15 12:39:43 -08:00
import profile_hover_card from './profile-hover-card';
import push_notifications from './push-notifications';
import relationships from './relationships';
import reports from './reports';
2022-04-27 07:11:21 -07:00
import rules from './rules';
2022-11-15 12:39:43 -08:00
import scheduled_statuses from './scheduled-statuses';
import search from './search';
import security from './security';
import settings from './settings';
import sidebar from './sidebar';
import soapbox from './soapbox';
2022-06-17 12:39:17 -07:00
import status_hover_card from './status-hover-card';
2022-11-15 12:39:43 -08:00
import status_lists from './status-lists';
import statuses from './statuses';
import suggestions from './suggestions';
import tags from './tags';
import timelines from './timelines';
2022-11-15 12:39:43 -08:00
import trending_statuses from './trending-statuses';
import trends from './trends';
2022-11-15 12:39:43 -08:00
import user_lists from './user-lists';
2022-03-21 11:09:01 -07:00
import verification from './verification';
2020-03-27 13:59:38 -07:00
const reducers = {
accounts_meta,
admin,
admin_announcements,
admin_log,
admin_user_index,
aliases,
announcements,
auth,
backups,
chat_message_lists,
chat_messages,
chats,
2020-03-27 13:59:38 -07:00
compose,
compose_event,
contexts,
conversations,
2020-03-27 13:59:38 -07:00
custom_emojis,
domain_lists,
dropdown_menu,
entities,
2020-03-27 13:59:38 -07:00
filters,
followed_tags,
group_memberships,
group_relationships,
groups,
history,
2020-04-01 13:05:52 -07:00
instance,
2020-03-27 13:59:38 -07:00
listAdder,
listEditor,
lists,
locations,
me,
meta,
modals,
mutes,
notifications,
onboarding,
2023-06-25 10:37:37 -07:00
patron,
pending_statuses,
2020-03-27 13:59:38 -07:00
polls,
profile_hover_card,
push_notifications,
relationships,
reports,
rules,
2021-06-27 12:58:40 -07:00
scheduled_statuses,
search,
security,
settings,
2020-03-27 13:59:38 -07:00
sidebar,
soapbox,
2022-06-16 21:19:53 -07:00
status_hover_card,
status_lists,
statuses,
suggestions,
tags,
timelines,
2022-03-21 11:09:01 -07:00
trending_statuses,
trends,
user_lists,
2022-03-21 11:09:01 -07:00
verification,
};
// Build a default state from all reducers: it has the key and `undefined`
2022-03-16 19:15:38 -07:00
export const StateRecord = ImmutableRecord(
2022-03-15 06:48:18 -07:00
Object.keys(reducers).reduce((params: Record<string, any>, reducer) => {
params[reducer] = undefined;
return params;
}, {}),
);
const appReducer = combineReducers(reducers, StateRecord);
2020-09-27 17:09:35 -07:00
// Clear the state (mostly) when the user logs out
2022-03-15 06:48:18 -07:00
const logOut = (state: any = StateRecord()): ReturnType<typeof appReducer> => {
2022-03-21 11:09:01 -07:00
if (BuildConfig.NODE_ENV === 'production') {
location.href = '/login';
}
2022-03-15 06:48:18 -07:00
const whitelist: string[] = ['instance', 'soapbox', 'custom_emojis', 'auth'];
2020-09-27 17:09:35 -07:00
return StateRecord(
2022-03-15 06:48:18 -07:00
whitelist.reduce((acc: Record<string, any>, curr) => {
2020-09-27 17:09:35 -07:00
acc[curr] = state.get(curr);
return acc;
}, {}),
2022-03-15 06:48:18 -07:00
) as unknown as ReturnType<typeof appReducer>;
2020-09-27 17:09:35 -07:00
};
2022-03-15 06:48:18 -07:00
const rootReducer: typeof appReducer = (state, action) => {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case AUTH_LOGGED_OUT:
return appReducer(logOut(state), action);
default:
return appReducer(state, action);
2020-09-27 17:09:35 -07:00
}
2020-03-27 13:59:38 -07:00
};
export default rootReducer;