Sticker Sets: Reorder sets after sending (#2223)

This commit is contained in:
Alexander Zinchuk 2022-12-27 02:46:07 +01:00
parent 3c9acde250
commit f1a82ff079
9 changed files with 70 additions and 21 deletions

View File

@ -217,6 +217,7 @@ export function sendMessage(
noWebPage, noWebPage,
sendAs, sendAs,
serverTimeOffset, serverTimeOffset,
shouldUpdateStickerSetsOrder,
}: { }: {
chat: ApiChat; chat: ApiChat;
text?: string; text?: string;
@ -234,6 +235,7 @@ export function sendMessage(
noWebPage?: boolean; noWebPage?: boolean;
sendAs?: ApiUser | ApiChat; sendAs?: ApiUser | ApiChat;
serverTimeOffset?: number; serverTimeOffset?: number;
shouldUpdateStickerSetsOrder?: boolean;
}, },
onProgress?: ApiOnProgress, onProgress?: ApiOnProgress,
) { ) {
@ -329,6 +331,7 @@ export function sendMessage(
...(media && { media }), ...(media && { media }),
...(noWebPage && { noWebpage: noWebPage }), ...(noWebPage && { noWebpage: noWebPage }),
...(sendAs && { sendAs: buildInputPeer(sendAs.id, sendAs.accessHash) }), ...(sendAs && { sendAs: buildInputPeer(sendAs.id, sendAs.accessHash) }),
...(shouldUpdateStickerSetsOrder && { updateStickersetsOrder: shouldUpdateStickerSetsOrder }),
}), true); }), true);
})(); })();

View File

@ -915,14 +915,24 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) {
onUpdate({ '@type': 'updateFavoriteStickers' }); onUpdate({ '@type': 'updateFavoriteStickers' });
} else if (update instanceof GramJs.UpdateRecentStickers) { } else if (update instanceof GramJs.UpdateRecentStickers) {
onUpdate({ '@type': 'updateRecentStickers' }); onUpdate({ '@type': 'updateRecentStickers' });
} else if (update instanceof GramJs.UpdateMoveStickerSetToTop) {
if (!update.masks) {
onUpdate({
'@type': 'updateMoveStickerSetToTop',
isCustomEmoji: update.emojis,
id: update.stickerset.toString(),
});
}
} else if (update instanceof GramJs.UpdateStickerSets) { } else if (update instanceof GramJs.UpdateStickerSets) {
onUpdate({ '@type': 'updateStickerSets' }); onUpdate({ '@type': 'updateStickerSets' });
} else if (update instanceof GramJs.UpdateStickerSetsOrder) { } else if (update instanceof GramJs.UpdateStickerSetsOrder) {
onUpdate({ if (!update.masks) {
'@type': 'updateStickerSetsOrder', onUpdate({
order: update.order.map((n) => n.toString()), '@type': 'updateStickerSetsOrder',
isCustomEmoji: update.emojis, order: update.order.map((n) => n.toString()),
}); isCustomEmoji: update.emojis,
});
}
} else if (update instanceof GramJs.UpdateNewStickerSet) { } else if (update instanceof GramJs.UpdateNewStickerSet) {
if (update.stickerset instanceof GramJs.messages.StickerSet) { if (update.stickerset instanceof GramJs.messages.StickerSet) {
const stickerSet = buildStickerSet(update.stickerset.set); const stickerSet = buildStickerSet(update.stickerset.set);

View File

@ -391,6 +391,12 @@ export type ApiUpdateRecentStickers = {
'@type': 'updateRecentStickers'; '@type': 'updateRecentStickers';
}; };
export type ApiUpdateMoveStickerSetToTop = {
'@type': 'updateMoveStickerSetToTop';
isCustomEmoji?: boolean;
id: string;
};
export type ApiUpdateStickerSets = { export type ApiUpdateStickerSets = {
'@type': 'updateStickerSets'; '@type': 'updateStickerSets';
}; };
@ -570,7 +576,7 @@ export type ApiUpdate = (
ApiUpdateAvatar | ApiUpdateMessageImage | ApiUpdateDraftMessage | ApiUpdateAvatar | ApiUpdateMessageImage | ApiUpdateDraftMessage |
ApiUpdateError | ApiUpdateResetContacts | ApiUpdateStartEmojiInteraction | ApiUpdateError | ApiUpdateResetContacts | ApiUpdateStartEmojiInteraction |
ApiUpdateFavoriteStickers | ApiUpdateStickerSet | ApiUpdateStickerSets | ApiUpdateStickerSetsOrder | ApiUpdateFavoriteStickers | ApiUpdateStickerSet | ApiUpdateStickerSets | ApiUpdateStickerSetsOrder |
ApiUpdateRecentStickers | ApiUpdateSavedGifs | ApiUpdateNewScheduledMessage | ApiUpdateRecentStickers | ApiUpdateSavedGifs | ApiUpdateNewScheduledMessage | ApiUpdateMoveStickerSetToTop |
ApiUpdateScheduledMessageSendSucceeded | ApiUpdateScheduledMessage | ApiUpdateScheduledMessageSendSucceeded | ApiUpdateScheduledMessage |
ApiUpdateDeleteScheduledMessages | ApiUpdateResetMessages | ApiUpdateDeleteScheduledMessages | ApiUpdateResetMessages |
ApiUpdateTwoFaError | ApiUpdateTwoFaStateWaitCode | ApiUpdateWebViewResultSent | ApiUpdateTwoFaError | ApiUpdateTwoFaStateWaitCode | ApiUpdateWebViewResultSent |

View File

@ -79,8 +79,9 @@ const StickerSetModal: FC<OwnProps & StateProps> = ({
const prevStickerSet = usePrevious(stickerSet); const prevStickerSet = usePrevious(stickerSet);
const renderingStickerSet = stickerSet || prevStickerSet; const renderingStickerSet = stickerSet || prevStickerSet;
const isAdded = renderingStickerSet?.installedDate;
const isEmoji = renderingStickerSet?.isEmoji; const isEmoji = renderingStickerSet?.isEmoji;
const isButtonLocked = !renderingStickerSet?.installedDate && isSetPremium && !isCurrentUserPremium; const isButtonLocked = !isAdded && isSetPremium && !isCurrentUserPremium;
const [requestCalendar, calendar] = useSchedule(canScheduleUntilOnline); const [requestCalendar, calendar] = useSchedule(canScheduleUntilOnline);
@ -104,14 +105,16 @@ const StickerSetModal: FC<OwnProps & StateProps> = ({
if (shouldSchedule || isScheduleRequested) { if (shouldSchedule || isScheduleRequested) {
requestCalendar((scheduledAt) => { requestCalendar((scheduledAt) => {
sendMessage({ sticker, isSilent, scheduledAt }); sendMessage({
sticker, isSilent, scheduledAt,
});
onClose(); onClose();
}); });
} else { } else {
sendMessage({ sticker, isSilent }); sendMessage({ sticker, isSilent, shouldUpdateStickerSetsOrder: isAdded });
onClose(); onClose();
} }
}, [onClose, requestCalendar, sendMessage, shouldSchedule]); }, [onClose, requestCalendar, sendMessage, shouldSchedule, isAdded]);
const handleButtonClick = useCallback(() => { const handleButtonClick = useCallback(() => {
if (renderingStickerSet) { if (renderingStickerSet) {
@ -133,7 +136,7 @@ const StickerSetModal: FC<OwnProps & StateProps> = ({
const suffix = isEmoji ? 'Emoji' : 'Sticker'; const suffix = isEmoji ? 'Emoji' : 'Sticker';
return lang( return lang(
renderingStickerSet.installedDate ? `StickerPack.Remove${suffix}Count` : `StickerPack.Add${suffix}Count`, isAdded ? `StickerPack.Remove${suffix}Count` : `StickerPack.Add${suffix}Count`,
renderingStickerSet.count, renderingStickerSet.count,
'i', 'i',
); );
@ -171,7 +174,7 @@ const StickerSetModal: FC<OwnProps & StateProps> = ({
<Button <Button
size="smaller" size="smaller"
fluid fluid
color={renderingStickerSet.installedDate ? 'danger' : 'primary'} color={isAdded ? 'danger' : 'primary'}
isShiny={isButtonLocked} isShiny={isButtonLocked}
withPremiumGradient={isButtonLocked} withPremiumGradient={isButtonLocked}
onClick={handleButtonClick} onClick={handleButtonClick}

View File

@ -712,6 +712,7 @@ const Composer: FC<OwnProps & StateProps> = ({
attachments: currentAttachments, attachments: currentAttachments,
scheduledAt, scheduledAt,
isSilent, isSilent,
shouldUpdateStickerSetsOrder: true,
}); });
} }
@ -820,7 +821,11 @@ const Composer: FC<OwnProps & StateProps> = ({
}, [insertCustomEmojiAndUpdateCursor, isChatWithSelf, isCurrentUserPremium, showCustomEmojiPremiumNotification]); }, [insertCustomEmojiAndUpdateCursor, isChatWithSelf, isCurrentUserPremium, showCustomEmojiPremiumNotification]);
const handleStickerSelect = useCallback(( const handleStickerSelect = useCallback((
sticker: ApiSticker, isSilent?: boolean, isScheduleRequested?: boolean, shouldPreserveInput = false, sticker: ApiSticker,
isSilent?: boolean,
isScheduleRequested?: boolean,
shouldPreserveInput = false,
shouldUpdateStickerSetsOrder?: boolean,
) => { ) => {
sticker = { sticker = {
...sticker, ...sticker,
@ -837,7 +842,7 @@ const Composer: FC<OwnProps & StateProps> = ({
}); });
}); });
} else { } else {
sendMessage({ sticker, isSilent }); sendMessage({ sticker, isSilent, shouldUpdateStickerSetsOrder });
requestAnimationFrame(() => { requestAnimationFrame(() => {
resetComposer(shouldPreserveInput); resetComposer(shouldPreserveInput);
}); });

View File

@ -45,7 +45,9 @@ type OwnProps = {
className: string; className: string;
loadAndPlay: boolean; loadAndPlay: boolean;
canSendStickers: boolean; canSendStickers: boolean;
onStickerSelect: (sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean) => void; onStickerSelect: (
sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean, shouldUpdateStickerSetsOrder?: boolean,
) => void;
}; };
type StateProps = { type StateProps = {
@ -233,7 +235,7 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
}, []); }, []);
const handleStickerSelect = useCallback((sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean) => { const handleStickerSelect = useCallback((sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean) => {
onStickerSelect(sticker, isSilent, shouldSchedule); onStickerSelect(sticker, isSilent, shouldSchedule, true);
addRecentSticker({ sticker }); addRecentSticker({ sticker });
}, [addRecentSticker, onStickerSelect]); }, [addRecentSticker, onStickerSelect]);

View File

@ -41,7 +41,11 @@ export type OwnProps = {
onEmojiSelect: (emoji: string) => void; onEmojiSelect: (emoji: string) => void;
onCustomEmojiSelect: (emoji: ApiSticker) => void; onCustomEmojiSelect: (emoji: ApiSticker) => void;
onStickerSelect: ( onStickerSelect: (
sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean, shouldPreserveInput?: boolean sticker: ApiSticker,
isSilent?: boolean,
shouldSchedule?: boolean,
shouldPreserveInput?: boolean,
shouldUpdateStickerSetsOrder?: boolean
) => void; ) => void;
onGifSelect: (gif: ApiVideo, isSilent?: boolean, shouldSchedule?: boolean) => void; onGifSelect: (gif: ApiVideo, isSilent?: boolean, shouldSchedule?: boolean) => void;
onRemoveSymbol: () => void; onRemoveSymbol: () => void;
@ -171,8 +175,10 @@ const SymbolMenu: FC<OwnProps & StateProps> = ({
onSearchOpen(type); onSearchOpen(type);
}, [onClose, onSearchOpen]); }, [onClose, onSearchOpen]);
const handleStickerSelect = useCallback((sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean) => { const handleStickerSelect = useCallback((
onStickerSelect(sticker, isSilent, shouldSchedule, true); sticker: ApiSticker, isSilent?: boolean, shouldSchedule?: boolean, shouldUpdateStickerSetsOrder?: boolean,
) => {
onStickerSelect(sticker, isSilent, shouldSchedule, true, shouldUpdateStickerSetsOrder);
}, [onStickerSelect]); }, [onStickerSelect]);
const lang = useLang(); const lang = useLang();

View File

@ -36,6 +36,14 @@ addActionHandler('apiUpdate', (global, actions, update) => {
actions.loadRecentStickers(); actions.loadRecentStickers();
break; break;
case 'updateMoveStickerSetToTop': {
const oldOrder = update.isCustomEmoji ? global.customEmojis.added.setIds : global.stickers.added.setIds;
if (!oldOrder) return global;
const newOrder = [update.id, ...oldOrder.filter((id) => id !== update.id)];
actions.reorderStickerSets({ order: newOrder, isCustomEmoji: update.isCustomEmoji });
break;
}
case 'updateStickerSets': case 'updateStickerSets':
actions.loadStickerSets(); actions.loadStickerSets();
break; break;

View File

@ -227,8 +227,14 @@ addActionHandler('reorderStickerSets', (global, action, payload) => {
...global, ...global,
stickers: { stickers: {
...global.stickers, ...global.stickers,
[isCustomEmoji ? 'customEmoji' : 'added']: { added: {
setIds: order, setIds: (!isCustomEmoji ? order : global.stickers.added.setIds),
},
},
customEmojis: {
...global.customEmojis,
added: {
setIds: (isCustomEmoji ? order : global.customEmojis.added.setIds),
}, },
}, },
}; };