Forward Picker: Show pinned contacts first (#1241)

This commit is contained in:
Alexander Zinchuk 2021-07-06 19:12:39 +03:00
parent 3d0369a002
commit f15d616e20
2 changed files with 13 additions and 4 deletions

View File

@ -11,7 +11,7 @@ import {
getCanPostInChat, getChatTitle, isChatPrivate, sortChatIds, getCanPostInChat, getChatTitle, isChatPrivate, sortChatIds,
} from '../../modules/helpers'; } from '../../modules/helpers';
import searchWords from '../../util/searchWords'; import searchWords from '../../util/searchWords';
import { pick } from '../../util/iteratees'; import { pick, unique } from '../../util/iteratees';
import useInfiniteScroll from '../../hooks/useInfiniteScroll'; import useInfiniteScroll from '../../hooks/useInfiniteScroll';
import useLang from '../../hooks/useLang'; import useLang from '../../hooks/useLang';
import useKeyboardListNavigation from '../../hooks/useKeyboardListNavigation'; import useKeyboardListNavigation from '../../hooks/useKeyboardListNavigation';
@ -33,6 +33,7 @@ export type OwnProps = {
type StateProps = { type StateProps = {
chatsById: Record<number, ApiChat>; chatsById: Record<number, ApiChat>;
pinnedIds?: number[];
activeListIds?: number[]; activeListIds?: number[];
archivedListIds?: number[]; archivedListIds?: number[];
orderedPinnedIds?: number[]; orderedPinnedIds?: number[];
@ -48,6 +49,7 @@ const MODAL_HIDE_DELAY_MS = 300;
const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
chatsById, chatsById,
pinnedIds,
activeListIds, activeListIds,
archivedListIds, archivedListIds,
currentUserId, currentUserId,
@ -91,6 +93,11 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
...archivedListIds || [], ...archivedListIds || [],
]; ];
let priorityIds = pinnedIds || [];
if (currentUserId) {
priorityIds = unique([currentUserId, ...priorityIds]);
}
return sortChatIds([ return sortChatIds([
...listIds.filter((id) => { ...listIds.filter((id) => {
const chat = chatsById[id]; const chat = chatsById[id];
@ -108,8 +115,8 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter);
}), }),
], chatsById, undefined, currentUserId ? [currentUserId] : undefined, serverTimeOffset); ], chatsById, undefined, priorityIds, serverTimeOffset);
}, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, serverTimeOffset]); }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds, serverTimeOffset]);
const [viewportIds, getMore] = useInfiniteScroll(loadMoreChats, chatIds, Boolean(filter)); const [viewportIds, getMore] = useInfiniteScroll(loadMoreChats, chatIds, Boolean(filter));
@ -192,12 +199,14 @@ export default memo(withGlobal<OwnProps>(
chats: { chats: {
byId: chatsById, byId: chatsById,
listIds, listIds,
orderedPinnedIds,
}, },
currentUserId, currentUserId,
} = global; } = global;
return { return {
chatsById, chatsById,
pinnedIds: orderedPinnedIds.active,
activeListIds: listIds.active, activeListIds: listIds.active,
archivedListIds: listIds.archived, archivedListIds: listIds.archived,
currentUserId, currentUserId,

View File

@ -496,7 +496,7 @@ export function sortChatIds(
// Assuming that last message date can't be less than now, // Assuming that last message date can't be less than now,
// this should place prioritized on top of the list. // this should place prioritized on top of the list.
// Then we subtract index of `id` in `priorityIds` to preserve selected order // Then we subtract index of `id` in `priorityIds` to preserve selected order
priority += Date.now() + serverTimeOffset * 1000 + (priorityIds.length - priorityIds.indexOf(id)); priority += Date.now() + serverTimeOffset * 1000 + (priorityIds.length - priorityIds.indexOf(id)) * 3e8;
} }
return priority; return priority;