@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
55 lines • 2.33 kB
JavaScript
import { createSlice } from "@reduxjs/toolkit";
export const MAX_ACCOUNTS = 5;
// Slice
const initialState = {
accounts: {},
activeAccountId: null,
isReady: false,
accountManagerRegistered: false,
};
const accountsSlice = createSlice({
name: "accounts",
initialState,
reducers: {
setAccountMap: (state, action) => {
state.accounts = action.payload.accounts;
state.activeAccountId = action.payload.activeAccountId;
},
upsertAccount: (state, action) => {
const isNewAccount = !(action.payload.userId in state.accounts);
if (isNewAccount && Object.keys(state.accounts).length >= MAX_ACCOUNTS) {
// Silently ignore — limit reached. useAddAccount guards this in the UI.
return;
}
state.accounts[action.payload.userId] = action.payload.entry;
},
removeAccount: (state, action) => {
delete state.accounts[action.payload];
if (state.activeAccountId === action.payload) {
const remaining = Object.keys(state.accounts);
state.activeAccountId = remaining.length > 0 ? remaining[0] : null;
}
},
setActiveAccount: (state, action) => {
state.activeAccountId = action.payload;
},
clearAllAccounts: (state) => {
state.accounts = {};
state.activeAccountId = null;
},
setAccountsReady: (state, action) => {
state.isReady = action.payload;
},
registerAccountManager: (state) => {
state.accountManagerRegistered = true;
},
},
});
export const { setAccountMap, upsertAccount, removeAccount, setActiveAccount, clearAllAccounts, setAccountsReady, registerAccountManager, } = accountsSlice.actions;
// Selectors — namespaced for dual-mode support
export const selectAccounts = (state) => state.replyke.accounts.accounts;
export const selectActiveAccountId = (state) => state.replyke.accounts.activeAccountId;
export const selectAccountsReady = (state) => state.replyke.accounts.isReady;
export const selectAccountManagerRegistered = (state) => state.replyke.accounts.accountManagerRegistered;
export default accountsSlice.reducer;
//# sourceMappingURL=accountsSlice.js.map