/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see .
*/
import {useLingui} from '@lingui/react/macro';
import {GiftIcon, QuestionIcon} from '@phosphor-icons/react';
import {observer} from 'mobx-react-lite';
import React from 'react';
import * as GiftActionCreators from '~/actions/GiftActionCreators';
import {
EmbedCard,
EmbedSkeletonButton,
EmbedSkeletonCircle,
EmbedSkeletonSubtitle,
EmbedSkeletonTitle,
} from '~/components/embeds/EmbedCard/EmbedCard';
import cardStyles from '~/components/embeds/EmbedCard/EmbedCard.module.css';
import {useEmbedSkeletonOverride} from '~/components/embeds/EmbedCard/useEmbedSkeletonOverride';
import {Button} from '~/components/uikit/Button/Button';
import i18n from '~/i18n';
import {ComponentDispatch} from '~/lib/ComponentDispatch';
import GiftStore from '~/stores/GiftStore';
import UserStore from '~/stores/UserStore';
import {getGiftDurationText} from '~/utils/giftUtils';
import styles from './GiftEmbed.module.css';
interface GiftEmbedProps {
code: string;
}
export const GiftEmbed = observer(function GiftEmbed({code}: GiftEmbedProps) {
const {t} = useLingui();
const giftState = GiftStore.gifts.get(code) ?? null;
const gift = giftState?.data;
const creator = UserStore.getUser(gift?.created_by?.id ?? '');
const shouldForceSkeleton = useEmbedSkeletonOverride();
React.useEffect(() => {
if (!giftState) {
void GiftActionCreators.fetchWithCoalescing(code).catch(() => {});
}
}, [code, giftState]);
const prevLoadingRef = React.useRef(true);
React.useEffect(() => {
const isLoading = !!giftState?.loading;
if (prevLoadingRef.current && !isLoading && giftState) {
ComponentDispatch.dispatch('LAYOUT_RESIZED');
}
prevLoadingRef.current = isLoading;
}, [giftState?.loading]);
if (shouldForceSkeleton || !giftState || giftState.loading) {
return ;
}
if (giftState.invalid || giftState.error || !gift) {
return ;
}
const durationText = getGiftDurationText(i18n, gift);
const handleRedeem = async () => {
try {
await GiftActionCreators.redeem(i18n, code);
} catch (error) {
console.error('Failed to redeem gift', error);
}
};
const subtitleNode = creator ? (
{t`From ${creator.username}#${creator.discriminator}`}
) : undefined;
const helpText = gift.redeemed ? t`Already redeemed` : t`Click to claim your gift!`;
const footer = gift.redeemed ? (
) : (
);
return (
}
title={