import { useEffect, useState, useMemo } from 'react'; import { API_URL } from '../config'; import { ROUTES } from '../lib/routes'; import { useLanguage } from '../i18n'; import MarketingHeader from './layout/MarketingHeader'; import MarketingFooter from './layout/MarketingFooter'; function categoryKey(type) { const t = (type || '').toLowerCase(); if (t.includes('restaurant') || t.includes('cafe') || t.includes('food')) return 'restaurant'; if (t.includes('hotel') || t.includes('riad') || t.includes('kasbah')) return 'hotel'; if (t.includes('service') || t.includes('salon') || t.includes('garage') || t.includes('cabinet') || t.includes('studio') || t.includes('agence')) return 'service'; if (t.includes('retail') || t.includes('boutique') || t.includes('commerce') || t.includes('épicerie') || t.includes('shop')) return 'retail'; return 'autre'; } const CATEGORY_COVERS = { restaurant: 'https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?auto=format&fit=crop&q=80&w=600&h=400', hotel: 'https://images.unsplash.com/photo-1540541338287-41700207dee6?auto=format&fit=crop&q=80&w=600&h=400', service: 'https://images.unsplash.com/photo-1497366216548-37526070297c?auto=format&fit=crop&q=80&w=600&h=400', retail: 'https://images.unsplash.com/photo-1441986300917-64674bd600d8?auto=format&fit=crop&q=80&w=600&h=400', autre: 'https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&q=80&w=600&h=400', }; function coverFor(business) { if (business.cover_image_url) return business.cover_image_url; const key = categoryKey(business.business_type); return CATEGORY_COVERS[key] || CATEGORY_COVERS.autre; } export default function ShowcasePage() { const { t } = useLanguage(); const [businesses, setBusinesses] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [activeCategory, setActiveCategory] = useState('all'); const CATEGORIES = useMemo(() => [ { key: 'all', label: t('showcase.all'), icon: 'apps' }, { key: 'restaurant', label: t('showcase.restaurants'), icon: 'restaurant' }, { key: 'hotel', label: t('showcase.hotels'), icon: 'bed' }, { key: 'service', label: t('showcase.services'), icon: 'build' }, { key: 'retail', label: t('showcase.shops'), icon: 'storefront' }, { key: 'autre', label: t('showcase.other'), icon: 'category' }, ], [t]); useEffect(() => { setLoading(true); setError(null); const controller = new AbortController(); fetch(`${API_URL}/businesses`, { signal: controller.signal }) .then((res) => { if (!res.ok) throw new Error('error'); return res.json(); }) .then((data) => { setBusinesses(data.businesses || []); setLoading(false); }) .catch((err) => { if (err.name === 'AbortError') return; setLoading(false); setError(true); }); return () => controller.abort(); }, []); const grouped = {}; businesses.forEach((b) => { const cat = categoryKey(b.business_type); if (!grouped[cat]) grouped[cat] = []; grouped[cat].push(b); }); const filtered = activeCategory === 'all' ? businesses : (grouped[activeCategory] || []); const hasBusinesses = businesses.length > 0; return ( <>
{t('showcase.subtitle')}
{t('common.error')}
{t('showcase.empty')}
{t('showcase.emptyFilter')}
{b.description}
)} {(() => { try { const sm = JSON.parse(b.social_media || '{}'); const entries = Object.entries(sm).filter(([, v]) => v && v.trim()).slice(0, 4); if (entries.length === 0) return null; return (