UNPKG

@oxyhq/services

Version:

Reusable OxyHQ module to handle authentication, user management, karma system, device-based session management and more 🚀

225 lines (217 loc) • 5.69 kB
"use strict"; import { create } from 'zustand'; export const useFollowStore = create((set, get) => ({ followingUsers: {}, loadingUsers: {}, fetchingUsers: {}, errors: {}, followerCounts: {}, followingCounts: {}, loadingCounts: {}, setFollowingStatus: (userId, isFollowing) => set(state => ({ followingUsers: { ...state.followingUsers, [userId]: isFollowing }, errors: { ...state.errors, [userId]: null } })), clearFollowError: userId => set(state => ({ errors: { ...state.errors, [userId]: null } })), resetFollowState: () => set({ followingUsers: {}, loadingUsers: {}, fetchingUsers: {}, errors: {}, followerCounts: {}, followingCounts: {}, loadingCounts: {} }), fetchFollowStatus: async (userId, oxyServices) => { set(state => ({ fetchingUsers: { ...state.fetchingUsers, [userId]: true }, errors: { ...state.errors, [userId]: null } })); try { const response = await oxyServices.getFollowStatus(userId); set(state => ({ followingUsers: { ...state.followingUsers, [userId]: response.isFollowing }, fetchingUsers: { ...state.fetchingUsers, [userId]: false }, errors: { ...state.errors, [userId]: null } })); } catch (error) { set(state => ({ fetchingUsers: { ...state.fetchingUsers, [userId]: false }, errors: { ...state.errors, [userId]: error?.message || 'Failed to fetch follow status' } })); } }, toggleFollowUser: async (userId, oxyServices, isCurrentlyFollowing) => { set(state => ({ loadingUsers: { ...state.loadingUsers, [userId]: true }, errors: { ...state.errors, [userId]: null } })); try { let response; let newFollowState; if (isCurrentlyFollowing) { response = await oxyServices.unfollowUser(userId); newFollowState = false; } else { response = await oxyServices.followUser(userId); newFollowState = true; } // Update follow status set(state => ({ followingUsers: { ...state.followingUsers, [userId]: newFollowState }, loadingUsers: { ...state.loadingUsers, [userId]: false }, errors: { ...state.errors, [userId]: null } })); // Update counts if the response includes them // The API returns counts for both users: // - followers: target user's follower count (the user being followed) // - following: current user's following count (the user doing the following) if (response && response.counts) { const { counts } = response; // Get current user ID from oxyServices const currentUserId = oxyServices.getCurrentUserId(); set(state => { const updates = {}; // Update target user's follower count (the user being followed) updates.followerCounts = { ...state.followerCounts, [userId]: counts.followers }; // Update current user's following count (the user doing the following) if (currentUserId) { updates.followingCounts = { ...state.followingCounts, [currentUserId]: counts.following }; } return updates; }); } } catch (error) { set(state => ({ loadingUsers: { ...state.loadingUsers, [userId]: false }, errors: { ...state.errors, [userId]: error?.message || 'Failed to update follow status' } })); } }, setFollowerCount: (userId, count) => set(state => ({ followerCounts: { ...state.followerCounts, [userId]: count } })), setFollowingCount: (userId, count) => set(state => ({ followingCounts: { ...state.followingCounts, [userId]: count } })), updateCountsFromFollowAction: (targetUserId, action, counts, currentUserId) => { set(state => { const updates = {}; // Update target user's follower count (the user being followed) updates.followerCounts = { ...state.followerCounts, [targetUserId]: counts.followers }; // Update current user's following count (the user doing the following) if (currentUserId) { updates.followingCounts = { ...state.followingCounts, [currentUserId]: counts.following }; } return updates; }); }, fetchUserCounts: async (userId, oxyServices) => { set(state => ({ loadingCounts: { ...state.loadingCounts, [userId]: true } })); try { const user = await oxyServices.getUserById(userId); if (user && user._count) { set(state => ({ followerCounts: { ...state.followerCounts, [userId]: user._count?.followers || 0 }, followingCounts: { ...state.followingCounts, [userId]: user._count?.following || 0 }, loadingCounts: { ...state.loadingCounts, [userId]: false } })); } } catch (error) { set(state => ({ loadingCounts: { ...state.loadingCounts, [userId]: false } })); } } })); //# sourceMappingURL=followStore.js.map