Skip to content

Commit

Permalink
Merge pull request #2744 from daostack/CW-fix-mark-as-read-unread
Browse files Browse the repository at this point in the history
Fixed mark as unread FeedCardTags
  • Loading branch information
matanfield authored Oct 14, 2024
2 parents 983a905 + 69b9193 commit 05f3419
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ function DiscussionFeedCard(props, ref) {

useEffect(() => {
if(item.data.lastMessage?.content && discussion?.id && isOptimisticallyCreated) {
// markFeedItemAsSeen({feedObjectId: item.id, commonId})
dispatch(commonActions.clearCreatedOptimisticFeedItem(discussion?.id));
markFeedItemAsSeen({feedObjectId: item.id, commonId})
setTimeout(() => dispatch(commonActions.clearCreatedOptimisticFeedItem(discussion?.id)), 10000);
}
},[item.id, item.data.lastMessage?.content, discussion?.id, isOptimisticallyCreated, commonId])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,18 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
);
},
[FeedItemMenuItem.MarkUnread]: ({ feedItemUserMetadata }) => {
const { count, seen, isSeenUpdating } = feedItemUserMetadata || {};
const { count, seen } = feedItemUserMetadata || {};

if (!feedItemUserMetadata) {
return true;
}

if (isSeenUpdating) {
return false;
}

return notEmpty(count) && notEmpty(seen) && count === 0 && seen;
},
[FeedItemMenuItem.MarkRead]: ({ feedItemUserMetadata }) => {
const { count, seenOnce, seen, isSeenUpdating } =
const { count, seenOnce, seen } =
feedItemUserMetadata || {};

if (isSeenUpdating) {
return false;
}

return (
Boolean(count) ||
(notEmpty(seen) && !seen) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const MemoizedFeedCardTags: FC<FeedCardTagsProps> = (props) => {
const isNewTagVisible =
notEmpty(seenOnce) && notEmpty(isOwner) && !seenOnce && !isOwner;
const isUnseenTagVisible =
!isNewTagVisible && !unreadMessages && notEmpty(seen) && !seen && !isOwner;
!isNewTagVisible && !unreadMessages && notEmpty(seen) && !seen;

return (
<>
{type === CommonFeedType.Proposal && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
(options: Options) => boolean
> = {
[ChatChannelMenuItem.MarkUnread]: ({ chatChannelUserStatus }) => {
const { notSeenCount, seen, isSeenUpdating } = chatChannelUserStatus || {};
const { notSeenCount, seen } = chatChannelUserStatus || {};

if (isSeenUpdating) {
return false;
}

return (
notEmpty(notSeenCount) && notEmpty(seen) && notSeenCount === 0 && seen
);
},
[ChatChannelMenuItem.MarkRead]: ({ chatChannelUserStatus }) => {
const { notSeenCount, seenOnce, seen, isSeenUpdating } =
const { notSeenCount, seenOnce, seen } =
chatChannelUserStatus || {};

if (isSeenUpdating) {
return false;
}

return (
Boolean(notSeenCount) ||
Expand Down
37 changes: 33 additions & 4 deletions src/shared/hooks/useCases/useUpdateFeedItemSeenState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useCallback } from "react";
import { useCallback, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { selectUser } from "@/pages/Auth/store/selectors";
import { CommonFeedService } from "@/services";
import { getFeedItemUserMetadataKey } from "@/shared/constants";
import axios, { CancelTokenSource } from "axios";
import {
MarkCommonFeedItemAsSeenPayload,
MarkCommonFeedItemAsUnseenPayload,
Expand All @@ -21,6 +22,9 @@ export const useUpdateFeedItemSeenState = (): Return => {
const dispatch = useDispatch();
const user = useSelector(selectUser());
const userId = user?.uid;

// Ref to store the current CancelTokenSource
const cancelTokenRef = useRef<CancelTokenSource | null>(null);

const updateSeenState = async (
payload:
Expand All @@ -32,6 +36,15 @@ export const useUpdateFeedItemSeenState = (): Return => {
return;
}

// Cancel the previous request if it exists
if (cancelTokenRef.current) {
cancelTokenRef.current.cancel("Operation canceled due to a new request.");
}

// Create a new CancelToken for the current request
const cancelTokenSource = axios.CancelToken.source();
cancelTokenRef.current = cancelTokenSource;

const { commonId, feedObjectId } = payload;
const key = getFeedItemUserMetadataKey({
commonId,
Expand All @@ -49,11 +62,27 @@ export const useUpdateFeedItemSeenState = (): Return => {
);

if (newSeenValue) {
await CommonFeedService.markCommonFeedItemAsSeen(payload);
await CommonFeedService.markCommonFeedItemAsSeen(payload, {
cancelToken: cancelTokenSource.token,
});
} else {
await CommonFeedService.markCommonFeedItemAsUnseen(payload);
await CommonFeedService.markCommonFeedItemAsUnseen(payload, {
cancelToken: cancelTokenSource.token,
});
}
} catch (error) {
} catch (error: unknown) {
if (error instanceof Error) {
if (error.name === 'AbortError') {
console.log('Request was aborted');
return;
}

// Handle other types of errors here, like logging or displaying a message
console.error("An error occurred:", error.message);
} else {
console.error("An unknown error occurred");
}

dispatch(
cacheActions.updateFeedItemUserSeenState({
key,
Expand Down

0 comments on commit 05f3419

Please sign in to comment.