Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real-time sorting of feed and inbox by last activity #2212 #2231

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
padding: 0 1.5rem;
display: flex;
flex-direction: column;
overflow-anchor: none;

& > * {
margin-bottom: 0;
Expand Down
23 changes: 20 additions & 3 deletions src/store/states/common/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const initialState: CommonState = {
recentAssignedCircle: null,
};

const sortFeedItems = (data: FeedItemFollowLayoutItem[]): void => {
data.sort(
(prevItem, nextItem) =>
nextItem.feedItem.updatedAt.toMillis() -
prevItem.feedItem.updatedAt.toMillis(),
);
};

const updateFeedItemInList = (
state: WritableDraft<CommonState>,
payload: {
Expand Down Expand Up @@ -76,10 +84,17 @@ const updateFeedItemInList = (
...updatedItem,
},
};
sortFeedItems(nextData);
}

const firstDocTimestamp = nextData[0]?.feedItem.updatedAt || null;
const lastDocTimestamp =
nextData[nextData.length - 1]?.feedItem.updatedAt || null;

state.feedItems = {
...state.feedItems,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand All @@ -95,8 +110,6 @@ const addNewFeedItems = (
}[],
shouldSortNewItems = false,
) => {
let firstDocTimestamp = state.feedItems.firstDocTimestamp;

const data = payload.reduceRight((acc, { commonFeedItem, statuses }) => {
const nextData = [...acc];
const itemIndex = nextData.findIndex(
Expand All @@ -117,7 +130,6 @@ const addNewFeedItems = (
itemId: commonFeedItem.id,
feedItem: commonFeedItem,
};
firstDocTimestamp = commonFeedItem.updatedAt;

if (itemIndex >= 0) {
nextData[itemIndex] = finalItem;
Expand All @@ -139,11 +151,16 @@ const addNewFeedItems = (

return nextData;
}, state.feedItems.data || []);
sortFeedItems(data);

const firstDocTimestamp = data[0]?.feedItem.updatedAt || null;
const lastDocTimestamp = data[data.length - 1]?.feedItem.updatedAt || null;

state.feedItems = {
...state.feedItems,
data,
firstDocTimestamp,
lastDocTimestamp,
};
};

Expand Down
40 changes: 36 additions & 4 deletions src/store/states/inbox/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
checkIsFeedItemFollowLayoutItem,
FeedLayoutItemWithFollowData,
} from "@/shared/interfaces";
import { ChatChannel, CommonFeed } from "@/shared/models";
import { ChatChannel, CommonFeed, Timestamp } from "@/shared/models";
import * as actions from "./actions";
import { InboxItems, InboxState } from "./types";
import { getFeedLayoutItemDateForSorting } from "./utils";
Expand All @@ -30,6 +30,26 @@ const initialState: InboxState = {
nextChatChannelItemId: null,
};

const sortInboxItems = (data: FeedLayoutItemWithFollowData[]): void => {
data.sort(
(prevItem, nextItem) =>
getFeedLayoutItemDateForSorting(nextItem).toMillis() -
getFeedLayoutItemDateForSorting(prevItem).toMillis(),
);
};

const getDocTimestamps = (
data: FeedLayoutItemWithFollowData[],
): {
firstDocTimestamp: Timestamp | null;
lastDocTimestamp: Timestamp | null;
} => ({
firstDocTimestamp: data[0] ? getFeedLayoutItemDateForSorting(data[0]) : null,
lastDocTimestamp: data[data.length - 1]
? getFeedLayoutItemDateForSorting(data[data.length - 1])
: null,
});

const updateInboxItemInList = (
state: WritableDraft<InboxState>,
payload: {
Expand Down Expand Up @@ -64,10 +84,14 @@ const updateInboxItemInList = (
...nextData[itemIndex],
...updatedItem,
};
sortInboxItems(nextData);
}
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(nextData);

state.items = {
...state.items,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand Down Expand Up @@ -162,9 +186,13 @@ const updateFeedItemInInboxItem = (
feedItem: { ...newFeedItem },
},
};
sortInboxItems(nextData);
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(nextData);

state.items = {
...state.items,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand Down Expand Up @@ -285,9 +313,13 @@ const updateChatChannelItemInInboxItem = (
lastMessage: updatedChatChannelItem.lastMessage || undefined,
},
};
sortInboxItems(nextData);
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(nextData);

state.items = {
...state.items,
firstDocTimestamp,
lastDocTimestamp,
data: nextData,
};
};
Expand Down Expand Up @@ -421,8 +453,6 @@ export const reducer = createReducer<InboxState, Action>(initialState)
(chatChannelItem) => chatChannelItem.itemId === item.item.itemId,
),
);
let firstDocTimestamp = nextState.items.firstDocTimestamp;

const data = payload.reduceRight((acc, { item, statuses }) => {
const nextData = [...acc];
const itemIndex = nextData.findIndex(
Expand All @@ -443,7 +473,6 @@ export const reducer = createReducer<InboxState, Action>(initialState)
}

const finalItem: FeedLayoutItemWithFollowData = { ...item };
firstDocTimestamp = getFeedLayoutItemDateForSorting(item);

if (itemIndex < 0) {
return [finalItem, ...nextData];
Expand All @@ -453,11 +482,14 @@ export const reducer = createReducer<InboxState, Action>(initialState)

return nextData;
}, nextState.items.data || []);
sortInboxItems(data);
const { firstDocTimestamp, lastDocTimestamp } = getDocTimestamps(data);

nextState.items = {
...nextState.items,
data,
firstDocTimestamp,
lastDocTimestamp,
};
}),
)
Expand Down