@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
47 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReplykeStoreProvider = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_redux_1 = require("react-redux");
const store_1 = require("../store");
const hooks_1 = require("../store/hooks");
const authThunks_1 = require("../store/slices/authThunks");
const accountsSlice_1 = require("../store/slices/accountsSlice");
/**
* Component that initializes auth state in Redux
* Must be inside the Redux Provider to dispatch actions
*/
const AuthInitializer = ({ children, projectId, signedToken }) => {
const dispatch = (0, hooks_1.useReplykeDispatch)();
const accountsReady = (0, hooks_1.useReplykeSelector)(accountsSlice_1.selectAccountsReady);
const accountManagerRegistered = (0, hooks_1.useReplykeSelector)(accountsSlice_1.selectAccountManagerRegistered);
const [hasWaitedForManager, setHasWaitedForManager] = (0, react_1.useState)(false);
// Give AccountManager one microtask to register itself
(0, react_1.useEffect)(() => {
Promise.resolve().then(() => setHasWaitedForManager(true));
}, []);
(0, react_1.useEffect)(() => {
// Still waiting for the microtask check
if (!hasWaitedForManager)
return;
// If an AccountManager registered, wait until it signals ready
if (accountManagerRegistered && !accountsReady)
return;
// Either: no AccountManager (core-only user) OR AccountManager is ready
dispatch((0, authThunks_1.initializeAuthThunk)({ projectId, signedToken }));
}, [dispatch, projectId, signedToken, hasWaitedForManager, accountManagerRegistered, accountsReady]);
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
};
/**
* Redux store provider for Replyke
* This component provides the Redux store and initializes auth state
*/
const ReplykeStoreProvider = ({ children, projectId, signedToken }) => {
return ((0, jsx_runtime_1.jsx)(react_redux_1.Provider, { store: store_1.replykeStore, children: (0, jsx_runtime_1.jsx)(AuthInitializer, { projectId: projectId, signedToken: signedToken, children: children }) }));
};
exports.ReplykeStoreProvider = ReplykeStoreProvider;
// Clean Redux-only architecture
// Always integrated with ReplykeProvider
exports.default = exports.ReplykeStoreProvider;
//# sourceMappingURL=replyke-store-context.js.map