import { useState, useEffect, useRef } from 'react'; import { Link } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import api from '../api'; const API_URL = 'http://localhost:5000'; const Home = () => { const { t, i18n } = useTranslation(); const [plugins, setPlugins] = useState([]); const hasFetched = useRef(false); useEffect(() => { if (hasFetched.current) return; api.get('/plugins', { params: { q: '', sort: 'newest' } }) .then(res => { setPlugins(res.data); hasFetched.current = true; }) .catch(err => console.error('Fetch error:', err)); }, []); const getDescription = (translatedStr: string) => { try { const data = JSON.parse(translatedStr); const currentLang = i18n.language.split('-')[0]; return data[currentLang] || data.en || Object.values(data)[0] || 'No description available.'; } catch { return 'No description available.'; } }; const getAccentColor = (name: string) => { const colors = [ '#4f7eff', '#ff6b6b', '#ffd166', '#06d6a0', '#a855f7', '#f97316', '#06b6d4', '#ec4899', ]; let hash = 0; for (let i = 0; i < name.length; i++) hash = name.charCodeAt(i) + ((hash << 5) - hash); return colors[Math.abs(hash) % colors.length]; }; // Compute effective display price respecting active sale const getEffectivePrice = (plugin: any): { display: string; isSale: boolean; originalDisplay: string } => { const base = plugin.price_cents ?? 0; const saleActive = plugin.sale_active && plugin.sale_discount_percent > 0; if (saleActive) { const saleCents = Math.round(base * (1 - plugin.sale_discount_percent / 100)); return { display: `€${(saleCents / 100).toFixed(2)}`, isSale: true, originalDisplay: `€${(base / 100).toFixed(2)}`, }; } return { display: `€${(base / 100).toFixed(2)}`, isSale: false, originalDisplay: '', }; }; return ( <>

{t('welcome')}

Explore powerful plugins for your PumpkinMC experience.

{plugins.map((plugin) => { const accent = getAccentColor(plugin.name); const desc = getDescription(plugin.translated_descriptions); const initial = plugin.name.charAt(0).toUpperCase(); const priceInfo = getEffectivePrice(plugin); const isSale = plugin.type === 'paid' && priceInfo.isSale; return (
{/* Preview image */}
{plugin.preview_path ? ( {plugin.name} ) : (
{initial}
)}
{plugin.category} {/* Price badge */} {plugin.type === 'free' ? ( Free ) : isSale ? ( {priceInfo.originalDisplay} {priceInfo.display} -{plugin.sale_discount_percent}% ) : ( {priceInfo.display} )}
{/* Info */}

{plugin.name}

by {plugin.dev_name}

{desc}

{(plugin.downloads ?? 0).toLocaleString()} {plugin.rating != null && ( {Number(plugin.rating).toFixed(1)} )} {plugin.version && ( v{plugin.version} )}
{/* Sale strip at bottom */} {isSale && (
🔥 Sale — {plugin.sale_discount_percent}% off
)}
); })} {plugins.length === 0 && (

No plugins found. Check back later!

)}
); }; export default Home;