@selfcommunity/react-core
Version:
React Core Components useful for integrating UI Community components (react-ui).
104 lines (102 loc) • 4.65 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = require("react");
const api_services_1 = require("@selfcommunity/api-services");
const types_1 = require("@selfcommunity/types");
const SCPreferencesProvider_1 = require("../components/provider/SCPreferencesProvider");
const Preferences_1 = require("../constants/Preferences");
const useSCCachingManager_1 = tslib_1.__importDefault(require("./useSCCachingManager"));
const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js"));
const Notification_1 = require("../constants/Notification");
/**
:::info
This custom hook is used to manage followers users.
:::
:::tip How to use it:
Follow these steps:
```jsx
1. const scUserContext: SCUserContextType = useSCUser();
2. const scFollowersManager: SCFollowersManagerType = scUserContext.manager.followers;
3. scFollowersManager.isFollowers(user)
```
:::
*/
function useSCFollowersManager(user) {
const { cache, updateCache, emptyCache, data, setData, loading, setLoading, setUnLoading, isLoading } = (0, useSCCachingManager_1.default)();
const scPreferencesContext = (0, SCPreferencesProvider_1.useSCPreferences)();
const followEnabled = Preferences_1.CONFIGURATIONS_FOLLOW_ENABLED in scPreferencesContext.preferences && scPreferencesContext.preferences[Preferences_1.CONFIGURATIONS_FOLLOW_ENABLED].value;
const notificationFollowSubscription = (0, react_1.useRef)(null);
const notificationUnFollowSubscription = (0, react_1.useRef)(null);
/**
* Notification subscriber only for FOLLOW
* @param msg
* @param data
*/
const notificationSubscriber = (msg, d) => {
if (Notification_1.SCNotificationMapping[d.data.activity_type] === types_1.SCNotificationTypologyType.USER_FOLLOW) {
updateCache([d.data.follower.id]);
if (!data.includes(d.data.follower.id)) {
setData((prev) => [...[d.data.follower.id], ...prev]);
}
}
else if (Notification_1.SCNotificationMapping[d.data.activity_type] === types_1.SCNotificationTypologyType.USER_UNFOLLOW) {
updateCache([d.data.unfollower.id]);
if (data.includes(d.data.unfollower.id)) {
setData((prev) => prev.filter((id) => id !== d.data.unfollower.id));
}
}
};
/**
* Subscribe to notification types user_follow, user_unfollow
*/
(0, react_1.useEffect)(() => {
notificationFollowSubscription.current = pubsub_js_1.default.subscribe(`${types_1.SCNotificationTopicType.INTERACTION}.${types_1.SCNotificationTypologyType.USER_FOLLOW}`, notificationSubscriber);
notificationUnFollowSubscription.current = pubsub_js_1.default.subscribe(`${types_1.SCNotificationTopicType.INTERACTION}.${types_1.SCNotificationTypologyType.USER_UNFOLLOW}`, notificationSubscriber);
return () => {
pubsub_js_1.default.unsubscribe(notificationFollowSubscription.current);
pubsub_js_1.default.unsubscribe(notificationUnFollowSubscription.current);
};
}, [data.length, cache.length]);
/**
* Check if the user is a followers of the authenticated user
* Update the followers cached
* @param user
*/
const checkIsUserFollowers = (user) => {
setLoading(user.id);
api_services_1.http
.request({
url: api_services_1.Endpoints.CheckUserFollower.url({ id: user.id }),
method: api_services_1.Endpoints.CheckUserFollower.method,
})
.then((res) => {
if (res.status >= 300) {
return Promise.reject(res);
}
updateCache([user.id]);
setData((prev) => (res.data.is_follower ? [...prev, ...[user.id]] : prev.filter((id) => id !== user.id)));
setUnLoading(user.id);
return Promise.resolve(res.data);
});
};
/**
* Memoized isFollower
* If user is already in cache -> check if the user is in followers,
* otherwise, check if user is a followers of the authenticated user
*/
const isFollower = (0, react_1.useMemo)(() => (user) => {
if (cache.includes(user.id)) {
return Boolean(data.includes(user.id));
}
if (!isLoading(user)) {
checkIsUserFollowers(user);
}
return false;
}, [data, loading, cache]);
if (!followEnabled || !user) {
return { followers: data, loading, isLoading };
}
return { followers: data, loading, isLoading, isFollower, emptyCache };
}
exports.default = useSCFollowersManager;
;