Skip to content

Commit

Permalink
update user activity use-case to use last common from feed
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Nov 29, 2023
1 parent 5227d92 commit ccd140a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
37 changes: 26 additions & 11 deletions src/shared/hooks/useCases/useUserActivity.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { useCallback, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Logger, UserActivityService } from "@/services";
import { useLoadingState } from "@/shared/hooks";
import { LoadingState } from "@/shared/interfaces";
import { UserActivity } from "@/shared/models";
import {
commonLayoutActions,
selectCommonLayoutLastCommonFromFeed,
} from "@/store/states";

interface Return extends LoadingState<UserActivity | null> {
interface Return {
lastVisitedCommon?: string;
updateUserActivity: (data: Partial<UserActivity>) => void;
}

export const useUserActivity = (userId?: string): Return => {
const [userActivityState, setUserActivityState] =
useLoadingState<UserActivity | null>(null, { loading: true });
const dispatch = useDispatch();
const lastCommonFromFeed = useSelector(selectCommonLayoutLastCommonFromFeed);

const updateUserActivity = useCallback(
async (data: Partial<UserActivity>) => {
if (data.lastVisitedCommon) {
dispatch(
commonLayoutActions.setLastCommonFromFeed({
id: data.lastVisitedCommon,
data: null,
}),
);
}
if (!userId) {
return;
}
Expand All @@ -35,19 +47,22 @@ export const useUserActivity = (userId?: string): Return => {
const unsubscribe = UserActivityService.subscribeToUserActivity(
userId,
(updatedUserActivity) => {
setUserActivityState({
loading: false,
fetched: true,
data: updatedUserActivity,
});
if (updatedUserActivity.lastVisitedCommon) {
dispatch(
commonLayoutActions.setLastCommonFromFeed({
id: updatedUserActivity.lastVisitedCommon,
data: null,
}),
);
}
},
);

return unsubscribe;
}, [userId]);

return {
...userActivityState,
lastVisitedCommon: lastCommonFromFeed?.id,
updateUserActivity,
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { CSSProperties, FC, ReactNode } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useSelector } from "react-redux";
import { useHistory } from "react-router-dom";
import classNames from "classnames";
import {
Expand All @@ -13,10 +13,6 @@ import { useModal } from "@/shared/hooks";
import { useUserActivity, useUserCommonIds } from "@/shared/hooks/useCases";
import { Avatar2Icon, Blocks2Icon, InboxIcon } from "@/shared/icons";
import { CreateCommonPrompt } from "@/shared/layouts/MultipleSpacesLayout/components/Header/components/Navigation/components";
import {
commonLayoutActions,
selectCommonLayoutLastCommonFromFeed,
} from "@/store/states";
import { LayoutTab } from "../../constants";
import { getActiveLayoutTab, getLayoutTabName } from "./utils";
import styles from "./LayoutTabs.module.scss";
Expand All @@ -35,21 +31,17 @@ interface TabConfiguration {

const LayoutTabs: FC<LayoutTabsProps> = (props) => {
const { className } = props;
const dispatch = useDispatch();
const history = useHistory();
const { getCommonPagePath, getInboxPagePath, getProfilePagePath } =
useRoutesContext();
const lastCommonIdFromFeed = useSelector(
selectCommonLayoutLastCommonFromFeed,
);
const isAuthenticated = useSelector(authentificated());
const userStreamsWithNotificationsAmount = useSelector(
selectUserStreamsWithNotificationsAmount(),
);
const user = useSelector(selectUser());
const userId = user?.uid;
const { data: userCommonIds } = useUserCommonIds();
const { data: userActivity } = useUserActivity(userId);
const { lastVisitedCommon } = useUserActivity(userId);
const {
isShowing: isCreateCommonPromptOpen,
onOpen: onCreateCommonPromptOpen,
Expand Down Expand Up @@ -89,15 +81,7 @@ const LayoutTabs: FC<LayoutTabsProps> = (props) => {
} as CSSProperties;

const handleSpacesClick = () => {
if (
lastCommonIdFromFeed &&
lastCommonIdFromFeed.id !== userActivity?.lastVisitedCommon
) {
dispatch(commonLayoutActions.setLastCommonFromFeed(null));
}

const commonForRedirectId =
userActivity?.lastVisitedCommon || userCommonIds[0];
const commonForRedirectId = lastVisitedCommon || userCommonIds[0];

if (commonForRedirectId) {
history.push(getCommonPagePath(commonForRedirectId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ const Navigation: FC<NavigationProps> = (props) => {
const user = useSelector(selectUser());
const userId = user?.uid;
const { data: userCommonIds } = useUserCommonIds();
const { data: userActivity } = useUserActivity(userId);
const mySpacesCommonId =
userActivity?.lastVisitedCommon || userCommonIds[0] || "";
const { lastVisitedCommon } = useUserActivity(userId);
const mySpacesCommonId = lastVisitedCommon || userCommonIds[0] || "";
const mySpacesPagePath = (
mySpacesCommonId ? getCommonPagePath(mySpacesCommonId) : ""
) as ROUTE_PATHS;
Expand Down

0 comments on commit ccd140a

Please sign in to comment.