Unique Gifts: Follow-up (#5468)

This commit is contained in:
zubiden 2025-01-21 18:21:01 +01:00 committed by Alexander Zinchuk
parent d1304c621d
commit aabf78a8fa
7 changed files with 62 additions and 35 deletions

View File

@ -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({

View File

@ -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

View File

@ -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 {

View File

@ -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();
});

View File

@ -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<void
const newGifts = currentGifts && !shouldRefresh ? currentGifts.gifts.concat(result.gifts) : result.gifts;
global = {
...global,
users: {
...global.users,
giftsById: {
...global.users.giftsById,
[userId]: {
gifts: newGifts,
nextOffset: result.nextOffset,
},
},
},
};
global = replaceUserGifts(global, userId, newGifts, result.nextOffset);
setGlobal(global);
});
@ -223,32 +213,43 @@ addActionHandler('fulfillStarsSubscription', async (global, actions, payload): P
});
addActionHandler('changeGiftVisibility', async (global, actions, payload): Promise<void> => {
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<void> => {
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,
});

View File

@ -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<T extends GlobalState>(
},
};
}
export function replaceUserGifts<T extends GlobalState>(
global: T,
userId: string,
gifts: ApiUserStarGift[],
nextOffset?: string,
): T {
global = {
...global,
users: {
...global.users,
giftsById: {
...global.users.giftsById,
[userId]: {
gifts,
nextOffset,
},
},
},
};
return global;
}

View File

@ -2303,12 +2303,10 @@ export interface ActionPayloads {
shouldRefresh?: boolean;
};
changeGiftVisibility: {
userId: string;
messageId: number;
shouldUnsave?: boolean;
};
convertGiftToStars: {
userId: string;
messageId: number;
} & WithTabId;