| Fixed price and add button to upload own content
This commit is contained in:
parent
9cef81a3c1
commit
0531e60222
|
|
@ -5,13 +5,13 @@ import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
||||||
import { Button } from "~/shared/ui/button";
|
import { Button } from "~/shared/ui/button";
|
||||||
import { usePurchaseContent, useViewContent } from "~/shared/services/content";
|
import { usePurchaseContent, useViewContent } from "~/shared/services/content";
|
||||||
import { fromNanoTON } from "~/shared/utils";
|
import { fromNanoTON } from "~/shared/utils";
|
||||||
import { useCallback } from "react";
|
import {useCallback, useEffect, useMemo} from "react";
|
||||||
import { AudioPlayer } from "~/shared/ui/audio-player";
|
import { AudioPlayer } from "~/shared/ui/audio-player";
|
||||||
|
|
||||||
export const ViewContentPage = () => {
|
export const ViewContentPage = () => {
|
||||||
const WebApp = useWebApp();
|
const WebApp = useWebApp();
|
||||||
|
|
||||||
const { data: content } = useViewContent(WebApp.initDataUnsafe?.start_param);
|
const { data: content, refetch: refetchContent } = useViewContent(WebApp.initDataUnsafe?.start_param);
|
||||||
|
|
||||||
const { mutateAsync: purchaseContent } = usePurchaseContent();
|
const { mutateAsync: purchaseContent } = usePurchaseContent();
|
||||||
|
|
||||||
|
|
@ -50,6 +50,20 @@ export const ViewContentPage = () => {
|
||||||
}
|
}
|
||||||
}, [content]);
|
}, [content]);
|
||||||
|
|
||||||
|
const haveLicense = useMemo(() => {
|
||||||
|
return content?.data?.have_licenses?.includes("listen") || content?.data?.have_licenses?.includes("resale")
|
||||||
|
}, [content])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
void refetchContent()
|
||||||
|
}, 5000)
|
||||||
|
|
||||||
|
return () => clearInterval(interval)
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className={"flex w-full flex-col gap-[50px] px-4"}>
|
<main className={"flex w-full flex-col gap-[50px] px-4"}>
|
||||||
{content?.data?.display_options?.metadata?.image && (
|
{content?.data?.display_options?.metadata?.image && (
|
||||||
|
|
@ -69,7 +83,7 @@ export const ViewContentPage = () => {
|
||||||
playsinline={true}
|
playsinline={true}
|
||||||
controls={true}
|
controls={true}
|
||||||
width="100%"
|
width="100%"
|
||||||
config={{ file: { attributes: { playsInline: true } } }}
|
config={{ file: { attributes: { playsInline: true, } }, }}
|
||||||
url={content?.data?.display_options?.content_url}
|
url={content?.data?.display_options?.content_url}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
@ -85,12 +99,13 @@ export const ViewContentPage = () => {
|
||||||
</p>
|
</p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<Button
|
{!haveLicense && <Button
|
||||||
onClick={handleBuyContent}
|
onClick={handleBuyContent}
|
||||||
className={"mb-4 mt-[30px] h-[48px]"}
|
className={"mb-4 mt-[30px] h-[48px]"}
|
||||||
label={`Купить за ${fromNanoTON(content?.data?.encrypted?.license?.resale?.price)} ТОН`}
|
label={`Купить за ${fromNanoTON(content?.data?.encrypted?.license?.resale?.price)} ТОН`}
|
||||||
includeArrows={true}
|
includeArrows={true}
|
||||||
/>
|
/>
|
||||||
|
}
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { useMutation } from "react-query";
|
||||||
import { request } from "~/shared/libs";
|
import { request } from "~/shared/libs";
|
||||||
import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
||||||
|
|
||||||
const sessionStorageKey = "auth_v1_token";
|
const localStorageKey = "auth_v1_token";
|
||||||
const payloadTTLMS = 1000 * 60 * 20;
|
const payloadTTLMS = 1000 * 60 * 20;
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
|
|
@ -16,8 +16,12 @@ export const useAuth = () => {
|
||||||
return useMutation(["auth"], async () => {
|
return useMutation(["auth"], async () => {
|
||||||
clearInterval(interval.current);
|
clearInterval(interval.current);
|
||||||
|
|
||||||
|
// Проверяем токен в localStorage
|
||||||
|
const storedToken = localStorage.getItem(localStorageKey);
|
||||||
|
|
||||||
|
// Если нет кошелька, удаляем токен и запрашиваем новый
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
sessionStorage.removeItem(sessionStorageKey);
|
localStorage.removeItem(localStorageKey);
|
||||||
|
|
||||||
const refreshPayload = async () => {
|
const refreshPayload = async () => {
|
||||||
tonConnectUI.setConnectRequestParameters({ state: "loading" });
|
tonConnectUI.setConnectRequestParameters({ state: "loading" });
|
||||||
|
|
@ -32,9 +36,12 @@ export const useAuth = () => {
|
||||||
console.error("Error in authentication request: ", error);
|
console.error("Error in authentication request: ", error);
|
||||||
throw new Error("Failed to authenticate.");
|
throw new Error("Failed to authenticate.");
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
tonConnectUI.setConnectRequestParameters(null);
|
tonConnectUI.setConnectRequestParameters(null);
|
||||||
} else {
|
} else {
|
||||||
|
// Сохраняем токен в localStorage
|
||||||
|
localStorage.setItem(localStorageKey, value.data.auth_v1_token);
|
||||||
tonConnectUI.setConnectRequestParameters({
|
tonConnectUI.setConnectRequestParameters({
|
||||||
state: "ready",
|
state: "ready",
|
||||||
value: {
|
value: {
|
||||||
|
|
@ -44,12 +51,25 @@ export const useAuth = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Обновляем токен каждые 20 минут
|
||||||
void refreshPayload();
|
void refreshPayload();
|
||||||
setInterval(refreshPayload, payloadTTLMS);
|
interval.current = setInterval(refreshPayload, payloadTTLMS);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Если токен уже сохранён в localStorage, пропускаем повторную авторизацию
|
||||||
|
if (storedToken) {
|
||||||
|
tonConnectUI.setConnectRequestParameters({
|
||||||
|
state: "ready",
|
||||||
|
value: {
|
||||||
|
tonProof: storedToken,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Логика для случая, когда есть кошелек и требуется тонProof
|
||||||
if (
|
if (
|
||||||
wallet.connectItems?.tonProof &&
|
wallet.connectItems?.tonProof &&
|
||||||
!("error" in wallet.connectItems.tonProof)
|
!("error" in wallet.connectItems.tonProof)
|
||||||
|
|
@ -75,7 +95,8 @@ export const useAuth = () => {
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res?.data?.auth_v1_token) {
|
if (res?.data?.auth_v1_token) {
|
||||||
sessionStorage.setItem(sessionStorageKey, res?.data?.auth_v1_token);
|
// Сохраняем токен в localStorage
|
||||||
|
localStorage.setItem(localStorageKey, res?.data?.auth_v1_token);
|
||||||
} else {
|
} else {
|
||||||
alert("Please try another wallet");
|
alert("Please try another wallet");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue