UNPKG

@replyke/core

Version:

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

69 lines 3.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const hooks_1 = require("../../store/hooks"); const userSlice_1 = require("../../store/slices/userSlice"); const useUserActions_1 = require("./useUserActions"); const useProject_1 = __importDefault(require("../projects/useProject")); const authSlice_1 = require("../../store/slices/authSlice"); /** * Redux-powered hook that provides comprehensive user management * This replaces useUserData and provides the same interface with Redux state management */ function useUser(_ = {}) { const dispatch = (0, hooks_1.useReplykeDispatch)(); // Get external context const { projectId } = (0, useProject_1.default)(); // Get Redux state const user = (0, hooks_1.useReplykeSelector)(userSlice_1.selectUser); const authUser = (0, hooks_1.useReplykeSelector)(authSlice_1.selectUser); // Fallback to auth user const loading = (0, hooks_1.useReplykeSelector)(userSlice_1.selectUserLoading); const updating = (0, hooks_1.useReplykeSelector)(userSlice_1.selectUserUpdating); const error = (0, hooks_1.useReplykeSelector)(userSlice_1.selectUserError); const currentProjectId = (0, hooks_1.useReplykeSelector)(userSlice_1.selectCurrentProjectId); // Get actions const { setUser, updateUser: updateUserAction, clearError, } = (0, useUserActions_1.useUserActions)(); // Update Redux state when project changes (0, react_1.useEffect)(() => { if (projectId && projectId !== currentProjectId) { dispatch((0, userSlice_1.setProjectContext)(projectId)); } }, [dispatch, projectId, currentProjectId]); // Sync auth user to user slice when auth user changes and we don't have a user yet // IMPORTANT: Only sync if authUser has valid data (has an id) to prevent empty objects from being set (0, react_1.useEffect)(() => { if (authUser && authUser.id && !user) { setUser(authUser); } }, [authUser, user, setUser]); // Current user operations with projectId included automatically const handleUpdateUser = (0, react_1.useCallback)(async (update) => { if (!user) { throw new Error("No user available to update"); } if (!projectId) { throw new Error("No projectId available"); } // Pass current user for optimistic update reversion if needed return await updateUserAction({ projectId, userId: user.id, update, currentUser: user, }); }, [updateUserAction, user, projectId]); // Return focused interface for current user management return (0, react_1.useMemo)(() => ({ user: user || authUser, // Fallback to auth user if user slice is empty loading, updating, error, updateUser: handleUpdateUser, clearError, }), [user, authUser, loading, updating, error, handleUpdateUser, clearError]); } exports.default = useUser; //# sourceMappingURL=useUser.js.map