/viewContent tg starts buy button. /uploadContent parsing address fix

This commit is contained in:
Verticool 2025-02-26 22:46:59 +06:00
parent d10065cfcc
commit 0bc77e44f8
2 changed files with 80 additions and 22 deletions

View File

@ -106,7 +106,7 @@ export const RoyaltyStep = ({ nextStep, prevStep }: RoyaltyStepProps) => {
// First initialization with 100% // First initialization with 100%
setRoyalty([{ setRoyalty([{
address: Address.parse(tonConnectUI.account.address).toString({ address: Address.parse(tonConnectUI.account.address).toString({
bounceable: true, bounceable: false,
urlSafe: true, urlSafe: true,
testOnly: false, testOnly: false,
}), }),

View File

@ -11,6 +11,15 @@ import {useAuth} from "~/shared/services/auth";
import { CongratsModal } from "./components/congrats-modal"; import { CongratsModal } from "./components/congrats-modal";
import { ErrorModal } from "./components/error-modal"; import { ErrorModal } from "./components/error-modal";
type InvoiceStatus = 'paid' | 'failed' | 'cancelled' | 'pending';
// Add type for invoice event
interface InvoiceEvent {
url: string;
status: InvoiceStatus;
}
export const ViewContentPage = () => { export const ViewContentPage = () => {
const WebApp = useWebApp(); const WebApp = useWebApp();
@ -24,7 +33,7 @@ export const ViewContentPage = () => {
const [isCongratsModal, setIsCongratsModal] = useState(false); const [isCongratsModal, setIsCongratsModal] = useState(false);
const [isErrorModal, setIsErrorModal] = useState(false); const [isErrorModal, setIsErrorModal] = useState(false);
const handleBuyContent = useCallback(async () => { const handleBuyContentTON = useCallback(async () => {
try { try {
if (!tonConnectUI.connected) { if (!tonConnectUI.connected) {
await tonConnectUI.openModal(); await tonConnectUI.openModal();
@ -64,6 +73,49 @@ export const ViewContentPage = () => {
} }
}, [content, tonConnectUI.connected]); }, [content, tonConnectUI.connected]);
const handleBuyContentStars = useCallback(async () => {
try {
if (!content?.data?.invoice.url) {
console.error('No invoice URL available');
return;
}
// Add event listener for invoice closing with typed event
const handleInvoiceClosed = (event: InvoiceEvent) => {
if (event.url === content.data.invoice.url) {
if (event.status === 'paid') {
void refetchContent();
setIsCongratsModal(true);
} else if (event.status === 'failed' || event.status === 'cancelled') {
// setIsErrorModal(true); // Turn on if need in error modal. Update text in it to match both way of payment errors
}
}
};
WebApp.onEvent('invoiceClosed', handleInvoiceClosed);
await WebApp.openInvoice(
content.data.invoice.url,
(status: InvoiceStatus) => {
console.log('Invoice status:', status);
if (status === 'paid') {
void refetchContent();
setIsCongratsModal(true);
} else if (status === 'failed' || status === 'cancelled') {
// setIsErrorModal(true); // Turn on if need in error modal. Update text in it to match both way of payment errors
}
}
);
return () => {
WebApp.offEvent('invoiceClosed', handleInvoiceClosed);
};
} catch (error) {
console.error('Payment failed:', error);
// setIsErrorModal(true); // Turn on if need in error modal. Update text in it to match both way of payment errors
}
}, [content, refetchContent]);
const haveLicense = useMemo(() => { const haveLicense = useMemo(() => {
return content?.data?.have_licenses?.includes("listen") || content?.data?.have_licenses?.includes("resale") return content?.data?.have_licenses?.includes("listen") || content?.data?.have_licenses?.includes("resale")
}, [content]) }, [content])
@ -126,16 +178,22 @@ export const ViewContentPage = () => {
</section> </section>
<div className="mt-auto pb-2"> <div className="mt-auto pb-2">
{!haveLicense && <Button {!haveLicense && <div className="flex flex-row gap-4">
onClick={handleBuyContent} <Button
className={"mb-4 h-[48px]"} onClick={handleBuyContentTON}
className={"mb-4 h-[48px] px-2"}
label={`Купить за ${fromNanoTON(content?.data?.encrypted?.license?.resale?.price)} ТОН`} label={`Купить за ${fromNanoTON(content?.data?.encrypted?.license?.resale?.price)} ТОН`}
includeArrows={true} includeArrows={content?.data?.invoice ? false : true}
/> />
{content?.data?.invoice && (
<Button
onClick={handleBuyContentStars}
className={"mb-4 h-[48px] px-2"}
label={`Купить за ${content?.data?.invoice?.amount} ⭐️`}
/>
)}
</div>
} }
{tonConnectUI.connected && (
<>
<Button <Button
onClick={() => { onClick={() => {
WebApp.openTelegramLink(`https://t.me/MY_UploaderRobot`); WebApp.openTelegramLink(`https://t.me/MY_UploaderRobot`);
@ -143,6 +201,7 @@ export const ViewContentPage = () => {
className={"h-[48px] bg-darkred"} className={"h-[48px] bg-darkred"}
label={`Загрузить свой контент`} label={`Загрузить свой контент`}
/> />
{tonConnectUI.connected && (
<Button <Button
onClick={() => { onClick={() => {
tonConnectUI.disconnect(); tonConnectUI.disconnect();
@ -150,7 +209,6 @@ export const ViewContentPage = () => {
className={"h-[48px] bg-darkred mt-4"} className={"h-[48px] bg-darkred mt-4"}
label={`Отключить кошелек`} label={`Отключить кошелек`}
/> />
</>
)} )}
</div> </div>
</main> </main>