@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
88 lines • 3.66 kB
JavaScript
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { useReplykeDispatch, useReplykeSelector } from "../store/hooks";
import { initializeAuthThunk } from "../store/slices/authThunks";
import { selectAccountsReady, selectAccountManagerRegistered, } from "../store/slices/accountsSlice";
import { selectInitialized } from "../store/slices/authSlice";
import { ReplykeContext } from "./replyke-context";
import useProjectData from "../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 = useReplykeDispatch();
const accountsReady = useReplykeSelector(selectAccountsReady);
const accountManagerRegistered = useReplykeSelector(selectAccountManagerRegistered);
const initialized = useReplykeSelector(selectInitialized);
const [hasWaitedForManager, setHasWaitedForManager] = useState(false);
// Give AccountManager one microtask to register itself
useEffect(() => {
Promise.resolve().then(() => setHasWaitedForManager(true));
}, []);
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(initializeAuthThunk({ projectId, signedToken }));
}, [dispatch, projectId, signedToken, hasWaitedForManager, accountManagerRegistered, accountsReady, initialized]);
return _jsx(_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>
* );
* }
* ```
*/
export const ReplykeIntegrationProvider = ({ children, projectId, signedToken, }) => {
// Provide projectId via context so hooks can access it
const data = useProjectData({ projectId });
// No Redux Provider here - user provides their own
return (_jsx(ReplykeContext.Provider, { value: data, children: _jsx(AuthInitializer, { projectId: projectId, signedToken: signedToken, children: children }) }));
};
export default ReplykeIntegrationProvider;
//# sourceMappingURL=replyke-integration-context.js.map