UNPKG

@replyke/core

Version:

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

95 lines 4.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReplykeIntegrationProvider = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const hooks_1 = require("../store/hooks"); const authThunks_1 = require("../store/slices/authThunks"); const accountsSlice_1 = require("../store/slices/accountsSlice"); const authSlice_1 = require("../store/slices/authSlice"); const replyke_context_1 = require("./replyke-context"); const useProjectData_1 = __importDefault(require("../hooks/projects/useProjectData")); /** * Component that initializes auth state in Redux. * Must be inside a 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 initialized = (0, hooks_1.useReplykeSelector)(authSlice_1.selectInitialized); 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)(() => { // Auth already bootstrapped (e.g. by OAuth callback) — skip if (initialized) return; // 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, initialized]); return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }); }; /** * Integration provider for Replyke (Integration Mode). * * Use this when you HAVE your own Redux store and want to integrate * Replyke's reducers into it. This provider does NOT create a Redux store - * you must wrap your app with your own Redux Provider. * * Prerequisites: * 1. Add replykeReducers under the 'replyke' key in your store * 2. Add replykeApiReducer under the 'replykeApi' key * 3. Add replykeMiddleware to your middleware chain * * @example * ```tsx * import { configureStore } from '@reduxjs/toolkit'; * import { Provider } from 'react-redux'; * import { * ReplykeIntegrationProvider, * replykeReducers, * replykeApiReducer, * replykeMiddleware * } from '@replyke/react-js'; * * const store = configureStore({ * reducer: { * replyke: replykeReducers, * replykeApi: replykeApiReducer, * ...yourReducers * }, * middleware: (getDefault) => getDefault().concat(...replykeMiddleware) * }); * * function App() { * return ( * <Provider store={store}> * <ReplykeIntegrationProvider projectId="..." signedToken={token}> * <YourApp /> * </ReplykeIntegrationProvider> * </Provider> * ); * } * ``` */ const ReplykeIntegrationProvider = ({ children, projectId, signedToken, }) => { // Provide projectId via context so hooks can access it const data = (0, useProjectData_1.default)({ projectId }); // No Redux Provider here - user provides their own return ((0, jsx_runtime_1.jsx)(replyke_context_1.ReplykeContext.Provider, { value: data, children: (0, jsx_runtime_1.jsx)(AuthInitializer, { projectId: projectId, signedToken: signedToken, children: children }) })); }; exports.ReplykeIntegrationProvider = ReplykeIntegrationProvider; exports.default = exports.ReplykeIntegrationProvider; //# sourceMappingURL=replyke-integration-context.js.map