-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2381 from daostack/feature/CW-2317-unread-enhance…
…ments Inbox unread filter fixes - Real-time items update #2317
- Loading branch information
Showing
12 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { InboxItemType } from "@/shared/constants"; | ||
import { ChatChannelLayoutItem } from "@/shared/interfaces"; | ||
import { ChatChannel } from "@/shared/models"; | ||
import { Converter } from "./Converter"; | ||
|
||
class ChatChannelToLayoutItemConverter extends Converter< | ||
ChatChannel, | ||
ChatChannelLayoutItem | ||
> { | ||
public toTargetEntity = ( | ||
chatChannel: ChatChannel, | ||
): ChatChannelLayoutItem => ({ | ||
type: InboxItemType.ChatChannel, | ||
itemId: chatChannel.id, | ||
chatChannel, | ||
}); | ||
|
||
public toBaseEntity = ( | ||
chatChannelLayoutItem: ChatChannelLayoutItem, | ||
): ChatChannel => chatChannelLayoutItem.chatChannel; | ||
} | ||
|
||
export default new ChatChannelToLayoutItemConverter(); |
25 changes: 25 additions & 0 deletions
25
src/shared/converters/FeedItemFollowToLayoutItemWithFollowDataConverter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { InboxItemType } from "@/shared/constants"; | ||
import { FeedItemFollowLayoutItemWithFollowData } from "@/shared/interfaces"; | ||
import { FeedItemFollowWithMetadata } from "@/shared/models"; | ||
import { Converter } from "./Converter"; | ||
|
||
class FeedItemFollowToLayoutItemWithFollowDataConverter extends Converter< | ||
FeedItemFollowWithMetadata, | ||
FeedItemFollowLayoutItemWithFollowData | ||
> { | ||
public toTargetEntity = ( | ||
feedItemFollowWithMetadata: FeedItemFollowWithMetadata, | ||
): FeedItemFollowLayoutItemWithFollowData => ({ | ||
type: InboxItemType.FeedItemFollow, | ||
itemId: feedItemFollowWithMetadata.feedItemId, | ||
feedItem: feedItemFollowWithMetadata.feedItem, | ||
feedItemFollowWithMetadata: feedItemFollowWithMetadata, | ||
}); | ||
|
||
public toBaseEntity = ( | ||
feedItemFollowLayoutItemWithFollowData: FeedItemFollowLayoutItemWithFollowData, | ||
): FeedItemFollowWithMetadata => | ||
feedItemFollowLayoutItemWithFollowData.feedItemFollowWithMetadata; | ||
} | ||
|
||
export default new FeedItemFollowToLayoutItemWithFollowDataConverter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { default as ChatChannelToDiscussionConverter } from "./ChatChannelToDiscussionConverter"; | ||
export { default as ChatChannelToLayoutItemConverter } from "./ChatChannelToLayoutItemConverter"; | ||
export { default as ChatMessageToUserDiscussionMessageConverter } from "./ChatMessageToUserDiscussionMessageConverter"; | ||
export { default as FeedItemFollowToLayoutItemWithFollowDataConverter } from "./FeedItemFollowToLayoutItemWithFollowDataConverter"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useEffect } from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { usePreviousDistinct, useUpdateEffect } from "react-use"; | ||
import { selectUserStreamsWithNotificationsAmount } from "@/pages/Auth/store/selectors"; | ||
import { inboxActions } from "@/store/states"; | ||
|
||
export const useUnreadInboxItems = (unread?: boolean): void => { | ||
const dispatch = useDispatch(); | ||
const notificationsAmount = useSelector( | ||
selectUserStreamsWithNotificationsAmount(), | ||
); | ||
const previousNotificationsAmount = usePreviousDistinct(notificationsAmount); | ||
|
||
useUpdateEffect(() => { | ||
if ( | ||
!unread || | ||
!notificationsAmount || | ||
(typeof previousNotificationsAmount === "number" && | ||
notificationsAmount < previousNotificationsAmount) | ||
) { | ||
return; | ||
} | ||
|
||
dispatch(inboxActions.refreshUnreadInboxItems.request()); | ||
}, [notificationsAmount]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
dispatch( | ||
inboxActions.refreshUnreadInboxItems.cancel( | ||
"Cancel unread inbox items refresh on unmount", | ||
), | ||
); | ||
}; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import { takeLatestWithCancel } from "@/shared/utils/saga"; | ||
import * as actions from "../actions"; | ||
import { getInboxItems } from "./getInboxItems"; | ||
import { refreshUnreadInboxItems } from "./refreshUnreadInboxItems"; | ||
|
||
export function* mainSaga() { | ||
yield takeLatestWithCancel( | ||
actions.getInboxItems.request, | ||
actions.getInboxItems.cancel, | ||
getInboxItems, | ||
); | ||
yield takeLatestWithCancel( | ||
actions.refreshUnreadInboxItems.request, | ||
actions.refreshUnreadInboxItems.cancel, | ||
refreshUnreadInboxItems, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { call, put, select } from "redux-saga/effects"; | ||
import { Logger, UserService } from "@/services"; | ||
import { | ||
ChatChannelToLayoutItemConverter, | ||
FeedItemFollowToLayoutItemWithFollowDataConverter, | ||
} from "@/shared/converters"; | ||
import { Awaited, FeedLayoutItemWithFollowData } from "@/shared/interfaces"; | ||
import { Timestamp } from "@/shared/models"; | ||
import * as actions from "../actions"; | ||
import { selectInboxItems } from "../selectors"; | ||
import { InboxItems } from "../types"; | ||
|
||
export function* refreshUnreadInboxItems() { | ||
try { | ||
const currentItems = (yield select(selectInboxItems)) as InboxItems; | ||
const newInboxItems: FeedLayoutItemWithFollowData[] = []; | ||
let startAfter: Timestamp | null = null; | ||
let keepItemsFetching = true; | ||
|
||
while (keepItemsFetching) { | ||
const { data, lastDocTimestamp, hasMore } = (yield call( | ||
UserService.getInboxItems, | ||
{ | ||
startAfter, | ||
limit: 5, | ||
unread: true, | ||
}, | ||
)) as Awaited<ReturnType<typeof UserService.getInboxItems>>; | ||
const chatChannelItems = data.chatChannels | ||
.filter( | ||
(chatChannel) => | ||
chatChannel.messageCount > 0 && | ||
(!currentItems.data || | ||
currentItems.data.every( | ||
(item) => item.itemId !== chatChannel.id, | ||
)), | ||
) | ||
.map((item) => ChatChannelToLayoutItemConverter.toTargetEntity(item)); | ||
const feedItemFollowItems = data.feedItemFollows | ||
.filter( | ||
(feedItemFollow) => | ||
!currentItems.data || | ||
currentItems.data.every( | ||
(item) => item.itemId !== feedItemFollow.id, | ||
), | ||
) | ||
.map((item) => | ||
FeedItemFollowToLayoutItemWithFollowDataConverter.toTargetEntity( | ||
item, | ||
), | ||
); | ||
newInboxItems.push(...chatChannelItems, ...feedItemFollowItems); | ||
keepItemsFetching = hasMore; | ||
startAfter = lastDocTimestamp; | ||
} | ||
|
||
if (newInboxItems.length > 0) { | ||
yield put( | ||
actions.addNewInboxItems( | ||
newInboxItems.map((item) => ({ | ||
item, | ||
statuses: { | ||
isAdded: false, | ||
isRemoved: false, | ||
}, | ||
})), | ||
), | ||
); | ||
} | ||
} catch (err) { | ||
Logger.error(err); | ||
} | ||
} |