|
| 1 | +import axios from 'axios' |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | +import { UrlsConstant } from '../../../shared/constants/urls.constant' |
| 4 | +import { Advertisement } from '../../../shared/interfaces/advertisement.interface' |
| 5 | + |
| 6 | +export function AdvertisementCardComponent() { |
| 7 | + const [ad, setAd] = useState<Advertisement | null>(null) |
| 8 | + const [loading, setLoading] = useState<boolean>(true) |
| 9 | + const [error, setError] = useState<boolean>(false) |
| 10 | + useEffect(() => { |
| 11 | + async function fetchAd() { |
| 12 | + try { |
| 13 | + setLoading(true) |
| 14 | + setError(false) |
| 15 | + const response = await axios.get<Advertisement>( |
| 16 | + `${UrlsConstant.ADS_STORE}?t=${Date.now()}`, |
| 17 | + ) |
| 18 | + if (!response.data || response.data.disabled) { |
| 19 | + setAd(null) |
| 20 | + setError(true) |
| 21 | + } else { |
| 22 | + setAd(response.data) |
| 23 | + } |
| 24 | + } catch (err) { |
| 25 | + console.error('Failed to load advertisement:', err) |
| 26 | + setError(true) |
| 27 | + } finally { |
| 28 | + setLoading(false) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + fetchAd() |
| 33 | + }, []) |
| 34 | + |
| 35 | + function handleAdClick() { |
| 36 | + if (ad?.url) { |
| 37 | + window.ipc.openBrowser(ad.url) |
| 38 | + } else { |
| 39 | + window.ipc.openBrowser('https://discord.gg/p9TZzEV39e') |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (loading) { |
| 44 | + return ( |
| 45 | + <div |
| 46 | + className={ |
| 47 | + 'p-2 dark:bg-[#262626] bg-base-200 rounded-2xl shadow-sm h-[70px] max-h-[70px] flex items-center justify-center' |
| 48 | + } |
| 49 | + > |
| 50 | + <div className="flex flex-row items-center gap-2"> |
| 51 | + <span className="loading loading-ring loading-xs"></span> |
| 52 | + <span className="text-xs text-gray-500 dark:text-gray-400"> |
| 53 | + Loading advertisement... |
| 54 | + </span> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + ) |
| 58 | + } |
| 59 | + if (error || !ad) { |
| 60 | + return ( |
| 61 | + <div |
| 62 | + className={ |
| 63 | + 'p-2 dark:bg-[#262626] bg-base-200 rounded-2xl shadow-sm h-[70px] max-h-[70px] flex items-center justify-center overflow-hidden cursor-pointer' |
| 64 | + } |
| 65 | + onClick={handleAdClick} |
| 66 | + > |
| 67 | + <div className="text-sm font-medium text-gray-400 dark:text-gray-500"> |
| 68 | + Ad |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + ) |
| 72 | + } |
| 73 | + return ( |
| 74 | + <div |
| 75 | + className={ |
| 76 | + 'p-1 dark:bg-[#262626] bg-base-200 rounded-2xl shadow-sm h-[70px] max-h-[70px] overflow-hidden cursor-pointer w-[360px] max-w-[360px] relative' |
| 77 | + } |
| 78 | + onClick={handleAdClick} |
| 79 | + > |
| 80 | + <div className="flex items-center h-full"> |
| 81 | + <img |
| 82 | + src={ad.banner} |
| 83 | + alt={ad.name} |
| 84 | + className="object-cover w-full h-full rounded-xl" |
| 85 | + onError={({ currentTarget }) => { |
| 86 | + currentTarget.onerror = null |
| 87 | + currentTarget.style.display = 'none' |
| 88 | + setError(true) |
| 89 | + }} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + <div className="absolute top-0 right-0 bg-gray-500/30 text-white text-[10px] px-1.5 py-0.5 rounded-bl-lg rounded-tr-lg font-medium"> |
| 93 | + Ad |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ) |
| 97 | +} |
0 commit comments