From aabf78a8fa2733a5d28682311b7bd2e2d1d8a604 Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:21:01 +0100 Subject: [PATCH] Unique Gifts: Follow-up (#5468) --- src/api/gramjs/methods/payments.ts | 2 - .../profile/RadialPatternBackground.tsx | 2 +- src/components/middle/MessageList.scss | 9 +++- .../modals/gift/info/GiftInfoModal.tsx | 11 ++--- src/global/actions/api/stars.ts | 47 ++++++++++--------- src/global/reducers/users.ts | 24 ++++++++++ src/global/types/actions.ts | 2 - 7 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/api/gramjs/methods/payments.ts b/src/api/gramjs/methods/payments.ts index ce9fa1ecd..947765d01 100644 --- a/src/api/gramjs/methods/payments.ts +++ b/src/api/gramjs/methods/payments.ts @@ -468,7 +468,6 @@ export function saveStarGift({ messageId, shouldUnsave, }: { - user: ApiUser; messageId: number; shouldUnsave?: boolean; }) { @@ -481,7 +480,6 @@ export function saveStarGift({ export function convertStarGift({ messageId, }: { - user: ApiUser; messageId: number; }) { return invokeRequest(new GramJs.payments.ConvertStarGift({ diff --git a/src/components/common/profile/RadialPatternBackground.tsx b/src/components/common/profile/RadialPatternBackground.tsx index d692700c6..25d26337f 100644 --- a/src/components/common/profile/RadialPatternBackground.tsx +++ b/src/components/common/profile/RadialPatternBackground.tsx @@ -133,7 +133,7 @@ const RadialPatternBackground = ({ } const radialGradient = ctx.createRadialGradient(width / 2, height / 2, 0, width / 2, height / 2, width / 2); - radialGradient.addColorStop(0, '#FFFFFF00'); + radialGradient.addColorStop(0, '#FFFFFF77'); radialGradient.addColorStop(1, '#FFFFFF'); // Alpha mask diff --git a/src/components/middle/MessageList.scss b/src/components/middle/MessageList.scss index 8a3056067..8bf5cfc11 100644 --- a/src/components/middle/MessageList.scss +++ b/src/components/middle/MessageList.scss @@ -341,17 +341,24 @@ .action-message-unique-properties { display: grid; - grid-template-columns: 1fr 1fr; + grid-template-columns: min-content 1fr; + justify-content: center; gap: 0.375rem; font-size: 0.875rem; margin-top: 0.5rem; position: relative; + + white-space: nowrap; } .action-message-unique-value { color: white; justify-self: flex-start; + width: 100%; // Grid ellipsis hack + text-align: initial; + overflow: hidden; + text-overflow: ellipsis; } .action-message-unique-property { diff --git a/src/components/modals/gift/info/GiftInfoModal.tsx b/src/components/modals/gift/info/GiftInfoModal.tsx index 2308ad244..62b4dbde8 100644 --- a/src/components/modals/gift/info/GiftInfoModal.tsx +++ b/src/components/modals/gift/info/GiftInfoModal.tsx @@ -81,8 +81,7 @@ const GiftInfoModal = ({ const giftSticker = gift && getStickerFromGift(gift); const canFocusUpgrade = Boolean(userGift?.upgradeMsgId); - const canUpdate = gift?.type === 'starGiftUnique' - ? gift.ownerId === currentUserId : Boolean(userGift?.messageId) && !isSender && !canFocusUpgrade; + const canUpdate = Boolean(userGift?.messageId) && !isSender && !canFocusUpgrade; const handleClose = useLastCallback(() => { closeGiftInfoModal(); @@ -96,14 +95,14 @@ const GiftInfoModal = ({ }); const handleTriggerVisibility = useLastCallback(() => { - const { fromId, messageId, isUnsaved } = userGift!; - changeGiftVisibility({ userId: fromId!, messageId: messageId!, shouldUnsave: !isUnsaved }); + const { messageId, isUnsaved } = userGift!; + changeGiftVisibility({ messageId: messageId!, shouldUnsave: !isUnsaved }); handleClose(); }); const handleConvertToStars = useLastCallback(() => { - const { fromId, messageId } = userGift!; - convertGiftToStars({ userId: fromId!, messageId: messageId! }); + const { messageId } = userGift!; + convertGiftToStars({ messageId: messageId! }); closeConvertConfirm(); handleClose(); }); diff --git a/src/global/actions/api/stars.ts b/src/global/actions/api/stars.ts index 56d816395..3997562c7 100644 --- a/src/global/actions/api/stars.ts +++ b/src/global/actions/api/stars.ts @@ -1,3 +1,4 @@ +import type { ApiUserStarGift } from '../../../api/types'; import type { StarGiftCategory } from '../../../types'; import { getCurrentTabId } from '../../../util/establishMultitabRole'; @@ -7,6 +8,7 @@ import { addActionHandler, getGlobal, setGlobal } from '../../index'; import { appendStarsSubscriptions, appendStarsTransactions, + replaceUserGifts, updateStarsBalance, updateStarsSubscriptionLoading, } from '../../reducers'; @@ -152,19 +154,7 @@ addActionHandler('loadUserGifts', async (global, actions, payload): Promise => { - const { userId, messageId, shouldUnsave } = payload; + const { messageId, shouldUnsave } = payload; - const user = selectUser(global, userId); - if (!user) return; + const currentUserId = global.currentUserId!; + + const oldGifts = global.users.giftsById[currentUserId]; + const newGifts = oldGifts.gifts.map((gift) => { + if (gift.messageId === messageId) { + return { + ...gift, + isUnsaved: shouldUnsave, + } satisfies ApiUserStarGift; + } + return gift; + }); + global = replaceUserGifts(global, currentUserId, newGifts, oldGifts.nextOffset); + setGlobal(global); const result = await callApi('saveStarGift', { - user, messageId, shouldUnsave, }); + global = getGlobal(); if (!result) { + global = replaceUserGifts(global, currentUserId, oldGifts.gifts, oldGifts.nextOffset); + setGlobal(global); return; } - actions.loadUserGifts({ userId: global.currentUserId!, shouldRefresh: true }); + // Reload gift list to avoid issues with pagination + actions.loadUserGifts({ userId: currentUserId, shouldRefresh: true }); }); addActionHandler('convertGiftToStars', async (global, actions, payload): Promise => { - const { userId, messageId, tabId = getCurrentTabId() } = payload; - - const user = selectUser(global, userId); - if (!user) return; + const { messageId, tabId = getCurrentTabId() } = payload; const result = await callApi('convertStarGift', { - user, messageId, }); diff --git a/src/global/reducers/users.ts b/src/global/reducers/users.ts index e2debb640..bfcbf5f6e 100644 --- a/src/global/reducers/users.ts +++ b/src/global/reducers/users.ts @@ -3,6 +3,7 @@ import type { ApiUser, ApiUserCommonChats, ApiUserFullInfo, + ApiUserStarGift, ApiUserStatus, } from '../../api/types'; import type { BotAppPermissions } from '../../types'; @@ -322,3 +323,26 @@ export function updateBotAppPermissions( }, }; } + +export function replaceUserGifts( + global: T, + userId: string, + gifts: ApiUserStarGift[], + nextOffset?: string, +): T { + global = { + ...global, + users: { + ...global.users, + giftsById: { + ...global.users.giftsById, + [userId]: { + gifts, + nextOffset, + }, + }, + }, + }; + + return global; +} diff --git a/src/global/types/actions.ts b/src/global/types/actions.ts index 3863759b0..1f7447932 100644 --- a/src/global/types/actions.ts +++ b/src/global/types/actions.ts @@ -2303,12 +2303,10 @@ export interface ActionPayloads { shouldRefresh?: boolean; }; changeGiftVisibility: { - userId: string; messageId: number; shouldUnsave?: boolean; }; convertGiftToStars: { - userId: string; messageId: number; } & WithTabId;