UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

63 lines 3.16 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectAccountManagerRegistered = exports.selectAccountsReady = exports.selectActiveAccountId = exports.selectAccounts = exports.registerAccountManager = exports.setAccountsReady = exports.clearAllAccounts = exports.setActiveAccount = exports.removeAccount = exports.upsertAccount = exports.setAccountMap = exports.MAX_ACCOUNTS = void 0; const toolkit_1 = require("@reduxjs/toolkit"); exports.MAX_ACCOUNTS = 5; // Slice const initialState = { accounts: {}, activeAccountId: null, isReady: false, accountManagerRegistered: false, }; const accountsSlice = (0, toolkit_1.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 >= exports.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; }, }, }); _a = accountsSlice.actions, exports.setAccountMap = _a.setAccountMap, exports.upsertAccount = _a.upsertAccount, exports.removeAccount = _a.removeAccount, exports.setActiveAccount = _a.setActiveAccount, exports.clearAllAccounts = _a.clearAllAccounts, exports.setAccountsReady = _a.setAccountsReady, exports.registerAccountManager = _a.registerAccountManager; // Selectors — namespaced for dual-mode support const selectAccounts = (state) => state.replyke.accounts.accounts; exports.selectAccounts = selectAccounts; const selectActiveAccountId = (state) => state.replyke.accounts.activeAccountId; exports.selectActiveAccountId = selectActiveAccountId; const selectAccountsReady = (state) => state.replyke.accounts.isReady; exports.selectAccountsReady = selectAccountsReady; const selectAccountManagerRegistered = (state) => state.replyke.accounts.accountManagerRegistered; exports.selectAccountManagerRegistered = selectAccountManagerRegistered; exports.default = accountsSlice.reducer; //# sourceMappingURL=accountsSlice.js.map