UNPKG

@replyke/core

Version:

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

75 lines 3.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useRemoveAccount; const react_1 = require("react"); const hooks_1 = require("../../store/hooks"); const accountsSlice_1 = require("../../store/slices/accountsSlice"); const authSlice_1 = require("../../store/slices/authSlice"); const userSlice_1 = require("../../store/slices/userSlice"); const authThunks_1 = require("../../store/slices/authThunks"); const baseApi_1 = require("../../store/api/baseApi"); const useProject_1 = __importDefault(require("../projects/useProject")); const axios_1 = __importDefault(require("../../config/axios")); const handleError_1 = require("../../utils/handleError"); function useRemoveAccount() { const dispatch = (0, hooks_1.useReplykeDispatch)(); const { projectId } = (0, useProject_1.default)(); const accounts = (0, hooks_1.useReplykeSelector)(accountsSlice_1.selectAccounts); const activeAccountId = (0, hooks_1.useReplykeSelector)(accountsSlice_1.selectActiveAccountId); const [isRemoving, setIsRemoving] = (0, react_1.useState)(false); const [error, setError] = (0, react_1.useState)(null); const removeAccount = (0, react_1.useCallback)(async ({ userId }) => { if (!projectId) throw new Error("No projectId available"); const targetAccount = accounts[userId]; if (!targetAccount) throw new Error(`Account ${userId} not found`); setIsRemoving(true); setError(null); const isActiveAccount = userId === activeAccountId; try { // Best-effort server sign-out try { await axios_1.default.post(`/${projectId}/auth/sign-out`, { refreshToken: targetAccount.refreshToken }); } catch (signOutError) { (0, handleError_1.handleError)(signOutError, "Server sign-out failed during account removal"); } // Remove from accounts map dispatch((0, accountsSlice_1.removeAccount)(userId)); if (isActiveAccount) { const remainingIds = Object.keys(accounts).filter((id) => id !== userId); if (remainingIds.length > 0) { const nextId = remainingIds[0]; const nextAccount = accounts[nextId]; dispatch((0, authSlice_1.resetAuth)()); dispatch((0, userSlice_1.clearUser)()); dispatch(baseApi_1.baseApi.util.resetApiState()); dispatch((0, authSlice_1.setTokens)({ accessToken: null, refreshToken: nextAccount.refreshToken, })); dispatch((0, authSlice_1.setInitialized)(false)); await dispatch((0, authThunks_1.requestNewAccessTokenThunk)({ projectId })); dispatch((0, authSlice_1.setInitialized)(true)); } else { dispatch((0, authSlice_1.resetAuth)()); dispatch((0, userSlice_1.clearUser)()); dispatch(baseApi_1.baseApi.util.resetApiState()); } } } catch (err) { setError(err instanceof Error ? err.message : "Failed to remove account"); } finally { setIsRemoving(false); } }, [dispatch, projectId, accounts, activeAccountId]); return { removeAccount, isRemoving, error }; } //# sourceMappingURL=useRemoveAccount.js.map