Skip to content

Commit

Permalink
CW-mention-streams
Browse files Browse the repository at this point in the history
Fixed redirects
  • Loading branch information
pvm-code committed Dec 29, 2024
1 parent 51eb554 commit a90937c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 88 deletions.
175 changes: 91 additions & 84 deletions src/pages/commonFeed/components/FeedLayout/FeedLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React, {
ForwardRefRenderFunction,
} from "react";
import { useDispatch, useSelector } from "react-redux";
import { useHistory } from "react-router-dom";
import { useHistory, useLocation } from "react-router-dom";
import { useDeepCompareEffect, useKey, useWindowSize } from "react-use";
import classNames from "classnames";
import { selectUser } from "@/pages/Auth/store/selectors";
Expand Down Expand Up @@ -165,7 +165,6 @@ interface FeedLayoutProps {
settings?: FeedLayoutSettings;
renderChatInput?: () => ReactNode;
onPullToRefresh?: () => void;
isInboxItems?: boolean;
}

const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
Expand Down Expand Up @@ -203,7 +202,6 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
settings,
renderChatInput,
onPullToRefresh,
isInboxItems = false,
} = props;
useDisableOverscroll();
const { getCommonPagePath } = useRoutesContext();
Expand All @@ -215,7 +213,10 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
const isTabletView = useIsTabletView();
const user = useSelector(selectUser());
const pendingFeedItemId = useSelector(selectPendingFeedItemId);
console.log('---pendingFeedItemId',pendingFeedItemId);
const location = useLocation();

const isInboxItems = useMemo(() => location.pathname === ROUTE_PATHS.INBOX, [location.pathname]);

const createdOptimisticFeedItems = useSelector(
selectCreatedOptimisticFeedItems,
);
Expand Down Expand Up @@ -427,20 +428,7 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (

const scrollToFeedItem = useCallback(
(feedItemId: string) => {
const itemIndex = allFeedItems.findIndex(
(item) =>
item.itemId === feedItemId ||
(item as FeedItemFollowLayoutItem)?.feedItem?.data.id === feedItemId
);

if (itemIndex !== -1 && listRef.current && !isInboxItems) {
// Item is found, scroll to it
console.log('--itemIndex',itemIndex, feedItemId, '--allFeedItems',allFeedItems);
listRef.current.scrollToRow(itemIndex);
dispatch(commonActions.setPendingFeedItemId(feedItemId));
} else {
dispatch(commonActions.setPendingFeedItemId(feedItemId));
}
dispatch(commonActions.setPendingFeedItemId(feedItemId));
},
[allFeedItems, isInboxItems]
);
Expand All @@ -456,7 +444,6 @@ useEffect(() => {
pendingFeedItemId
);

console.log('--itemIndex',itemIndex);
if (itemIndex !== -1 && listRef.current) {
setTimeout(() => listRef.current?.scrollToRow(itemIndex), 200);
dispatch(commonActions.setPendingFeedItemId(null));
Expand Down Expand Up @@ -663,7 +650,6 @@ useEffect(() => {
) => {
const { commonId = selectedItemCommonData?.id, messageId } = options;

console.log('--commonId',commonId, '---feedItemId',feedItemId);
if (commonId && onFeedItemSelect) {
onFeedItemSelect(commonId, feedItemId, messageId);
setActiveChatItem({ feedItemId });
Expand All @@ -676,68 +662,90 @@ useEffect(() => {
[selectedItemCommonData?.id, onFeedItemSelect],
);

const handleFeedItemClickInternal = (
feedItemId: string,
options: { commonId?: string; messageId?: string } = {},
) => {
const { commonId, messageId } = options;

if (chatItem?.feedItemId === feedItemId && !messageId) {
return;
}

const queryParamsForPath = {
item: feedItemId,
message: messageId,
};

if (commonId && commonId !== outerCommon?.id) {
history.push(getCommonPagePath(commonId, queryParamsForPath));
return;
}
if (chatItem?.nestedItemData) {
history.push(
getCommonPagePath(
chatItem?.nestedItemData.common.id,
queryParamsForPath,
),
);
return;
}

setActiveChatItem({ feedItemId });

const itemExists = allFeedItems.some((item) => item.itemId === feedItemId);

if (itemExists) {
refsByItemId.current[feedItemId]?.scrollToItem();
} else {
onFetchNext(feedItemId);
const paneEl = document.getElementsByClassName("Pane Pane1")[0];
const containerEl = isTabletView ? window : paneEl;
const scrollHeight = isTabletView
? document.body.scrollHeight
: paneEl?.scrollHeight;

if (containerEl) {
setTimeout(() => {
containerEl.scrollTo({
top: scrollHeight,
behavior: "smooth",
});
}, 50);
const handleFeedItemClickInternal = useCallback(
(
feedItemId: string,
options: { commonId?: string; messageId?: string } = {}
) => {
const { commonId, messageId } = options;

if (chatItem?.feedItemId === feedItemId && !messageId) {
setActiveChatItem({ feedItemId });
return;
}
}

if (messageId) {
addQueryParam(QueryParamKey.Message, messageId);
}
};

const handleFeedItemClick = useMemoizedFunction(
onFeedItemSelect
? handleFeedItemClickExternal
: handleFeedItemClickInternal,

const queryParamsForPath = {
item: feedItemId,
message: messageId,
};

if (commonId && commonId !== outerCommon?.id) {
history.push(getCommonPagePath(commonId, queryParamsForPath));
return;
}

if (chatItem?.nestedItemData) {
history.push(
getCommonPagePath(
chatItem?.nestedItemData.common.id,
queryParamsForPath
)
);
return;
}

setActiveChatItem({ feedItemId });

const itemExists = allFeedItems.some((item) => item.itemId === feedItemId);

if (itemExists) {
const isInbox = window.location.pathname === ROUTE_PATHS.INBOX;
if(isInbox && commonId) {
history.push(getCommonPagePath(commonId, queryParamsForPath));
} else {
refsByItemId.current[feedItemId]?.scrollToItem();
}
} else {
onFetchNext(feedItemId);
const paneEl = document.getElementsByClassName('Pane Pane1')[0];
const containerEl = isTabletView ? window : paneEl;
const scrollHeight = isTabletView
? document.body.scrollHeight
: paneEl?.scrollHeight;

if (containerEl) {
setTimeout(() => {
containerEl.scrollTo({
top: scrollHeight,
behavior: 'smooth',
});
}, 50);
}
}

if (messageId) {
addQueryParam(QueryParamKey.Message, messageId);
}
},
[
chatItem?.feedItemId,
chatItem?.nestedItemData,
outerCommon?.id,
history,
getCommonPagePath,
setActiveChatItem,
allFeedItems,
isInboxItems,
refsByItemId,
onFetchNext,
isTabletView,
addQueryParam,
]
);

const handleFeedItemClick = useCallback(
onFeedItemSelect ? handleFeedItemClickExternal : handleFeedItemClickInternal,
[onFeedItemSelect, handleFeedItemClickExternal, handleFeedItemClickInternal, isInboxItems]
);

const handleInternalLinkClick = useMemoizedFunction(
Expand Down Expand Up @@ -1164,7 +1172,6 @@ useEffect(() => {
)}
</InfiniteLoader>
)}
{/* {console.log('--isInboxItems',isInboxItems, isInboxItems ? handleFeedItemClickExternal : handleInternalLinkClick)} */}
{!isTabletView &&
(chatItem?.discussion ? (
<DesktopChat
Expand All @@ -1180,7 +1187,7 @@ useEffect(() => {
renderChatInput={renderChatInput}
onUserClick={handleUserClick}
onFeedItemClick={handleFeedItemClick}
onStreamMentionClick={isInboxItems ? handleFeedItemClickExternal : handleInternalLinkClick}
onStreamMentionClick={handleFeedItemClickExternal}
onInternalLinkClick={handleInternalLinkClick}
/>
) : (
Expand All @@ -1207,7 +1214,7 @@ useEffect(() => {
renderChatInput={renderChatInput}
onUserClick={handleUserClick}
onFeedItemClick={handleFeedItemClick}
onStreamMentionClick={isInboxItems ? handleFeedItemClickExternal : handleInternalLinkClick}
onStreamMentionClick={handleFeedItemClickExternal}
onInternalLinkClick={handleInternalLinkClick}
>
{selectedItemCommonData &&
Expand Down
1 change: 0 additions & 1 deletion src/pages/inbox/BaseInbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ const InboxPage: FC<InboxPageProps> = (props) => {
return (
<>
<FeedLayout
isInboxItems={true}
ref={setFeedLayoutRef}
className={styles.feedLayout}
renderContentWrapper={renderContentWrapper}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { useQuery } from "@tanstack/react-query";
import { CommonFeedService } from "@/services";
import { useDispatch } from "react-redux";
import { commonActions } from "@/store/states";
import { InternalLinkData } from "@/shared/utils";

interface StreamMentionProps {
commonId: string;
discussionId: string;
title: string;
mentionTextClassName?: string;
onStreamMentionClick?: (feedItemId: string) => void;
onStreamMentionClick?: ((feedItemId: string, options?: { commonId?: string; messageId?: string }) => void) | ((data: InternalLinkData) => void);
}

const StreamMention: FC<StreamMentionProps> = (props) => {
Expand All @@ -37,7 +38,8 @@ const StreamMention: FC<StreamMentionProps> = (props) => {
limit: 15,
}),
);
onStreamMentionClick(feedItemId);

(onStreamMentionClick as ((feedItemId: string, options?: { commonId?: string; messageId?: string }) => void))(feedItemId, { commonId });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface TextFromDescendant {
commonId?: string;
directParent?: DirectParent | null;
onUserClick?: (userId: string) => void;
onStreamMentionClick?: (feedItemId: string) => void;
onStreamMentionClick?: ((feedItemId: string, options?: { commonId?: string; messageId?: string }) => void) | ((data: InternalLinkData) => void);
onInternalLinkClick?: (data: InternalLinkData) => void;
showPlainText?: boolean;
}
Expand Down

0 comments on commit a90937c

Please sign in to comment.