import { useEffect, useRef, useState } from 'react';
import { Link } from 'react-router-dom';
import { motion, useScroll, useTransform } from 'framer-motion';
import { api } from '../../api/client.js';
import usePageMeta from '../../hooks/usePageMeta.js';
import Reveal from '../../components/Reveal.jsx';
import Accordion from '../../components/Accordion.jsx';
import StarRating from '../../components/StarRating.jsx';
import TestimonialSlider from '../../components/TestimonialSlider.jsx';
import { useSiteSettings } from '../../context/SiteSettingsContext.jsx';
import { useTextStyle } from '../../context/TextStylesContext.jsx';
import { useLanguage } from '../../context/LanguageContext.jsx';
import { dragAnchorStyle } from '../../utils/dragAnchorPosition.js';
import defaultHeroImage from '../../assets/photos/gallery-dash2.webp';
import galleryAmp from '../../assets/photos/gallery-amp-purple.webp';
import gallerySpeaker from '../../assets/photos/gallery-speaker.webp';
import gallerySubs from '../../assets/photos/gallery-subs.webp';
import galleryDash from '../../assets/photos/gallery-dash.webp';
import galleryTrunk from '../../assets/photos/hero-trunk.webp';
import galleryRear from '../../assets/photos/gallery-rear.webp';
import heroHighlight from '../../assets/leistungen/hero-highlight.webp';
import youtubeVideo from '../../assets/videos/youtube-hintergrund.mp4';
// Tailwinds Build-Zeit-Scanner braucht die vollstaendigen Klassennamen woertlich im
// Quellcode - ein dynamisch zusammengesetzter String wie `text-${align}` wuerde nicht
// erkannt und aus dem Production-CSS herausgefiltert. Deshalb feste Lookup-Tabellen statt
// Template-Strings, angesteuert ueber home_sections.text_align (siehe HomeSectionController).
const ALIGN_TEXT = { left: 'text-left', center: 'text-center', right: 'text-right' };
const ALIGN_MARGIN_X = { left: '', center: 'mx-auto', right: 'ml-auto' };
const EMPTY_SECTION = {
eyebrow_de: '', eyebrow_en: '', title_de: '', title_en: '', body_html_de: '', body_html_en: '',
text_align: 'center', text_align_x: 50, text_align_y: 50,
button_label_de: '', button_label_en: '', button_url: '', font_family: null,
};
/** Rendert nur, wenn sowohl Beschriftung als auch Ziel gesetzt sind - interne Ziele (mit "/")
* bleiben in der SPA, alles andere oeffnet extern in einem neuen Tab. */
function SectionButton({ label, url, className, style }) {
if (!label || !url) return null;
if (url.startsWith('/')) {
return (
{label}
);
}
return (
{label}
);
}
// Native Pixelmasse der Bilder unten - nur fuer die width/height-Attribute auf ,
// damit der Browser vor dem Laden Platz reserviert (verhindert Layout-Sprung). Die
// tatsaechliche Anzeigegroesse wird weiterhin komplett per CSS (aspect-square etc.)
// bestimmt, diese Werte muessen also nicht exakt zum gerenderten Seitenverhaeltnis passen.
const galleryImages = [
{ src: gallerySubs, width: 900, height: 600 },
{ src: galleryAmp, width: 900, height: 600 },
{ src: gallerySpeaker, width: 900, height: 1200 },
{ src: galleryDash, width: 900, height: 814 },
{ src: galleryRear, width: 900, height: 1200 },
{ src: galleryTrunk, width: 1920, height: 1280 },
];
export default function Home() {
const { hero_image_path: heroImagePath } = useSiteSettings();
const heroImage = heroImagePath || defaultHeroImage;
const { t, language } = useLanguage();
const stats = t('home.stats');
const galleryAlts = t('home.galleryAlts');
const gallery = galleryImages.map((img, i) => ({ ...img, alt: galleryAlts[i] }));
const [faqs, setFaqs] = useState([]);
const [homeSections, setHomeSections] = useState({});
const [partners, setPartners] = useState([]);
// Kurzform, um pro Abschnitt Text/Ausrichtung/Button in der jeweils aktiven Sprache zu lesen.
const section = (slug) => {
const s = homeSections[slug] || EMPTY_SECTION;
return {
eyebrow: s[`eyebrow_${language}`] || '',
title: s[`title_${language}`] || '',
bodyHtml: s[`body_html_${language}`] || '',
align: s.text_align || 'center',
// Nur fuer hero/youtube genutzt (echter Hintergrund) - siehe dragAnchorStyle-Aufrufe unten.
alignX: s.text_align_x,
alignY: s.text_align_y,
buttonLabel: s[`button_label_${language}`] || '',
buttonUrl: s.button_url || '',
fontFamily: s.font_family || undefined,
};
};
// Pro-Feld-Stil (Schriftart/-groesse/Farbe je Vorspann/Überschrift/Button-Text), zusaetzlich
// zur bestehenden Schriftart pro ganzer Sektion (section(slug).fontFamily) - siehe
// TextStylesContext.jsx. Feste Anzahl Aufrufe (6 Sektionen x bis zu 3 Felder), keine Schleife,
// da React Hooks nicht bedingt/dynamisch aufgerufen werden duerfen.
const heroEyebrowStyle = useTextStyle('home_sections.hero.eyebrow');
const heroTitleStyle = useTextStyle('home_sections.hero.title');
const youtubeEyebrowStyle = useTextStyle('home_sections.youtube.eyebrow');
const youtubeTitleStyle = useTextStyle('home_sections.youtube.title');
const youtubeButtonStyle = useTextStyle('home_sections.youtube.button_label');
const craftEyebrowStyle = useTextStyle('home_sections.craft.eyebrow');
const craftTitleStyle = useTextStyle('home_sections.craft.title');
const adviceEyebrowStyle = useTextStyle('home_sections.advice.eyebrow');
const adviceTitleStyle = useTextStyle('home_sections.advice.title');
const insightsEyebrowStyle = useTextStyle('home_sections.insights.eyebrow');
const insightsTitleStyle = useTextStyle('home_sections.insights.title');
const moreEyebrowStyle = useTextStyle('home_sections.more.eyebrow');
const moreTitleStyle = useTextStyle('home_sections.more.title');
const moreButtonStyle = useTextStyle('home_sections.more.button_label');
useEffect(() => {
api.get('/home-sections').then(setHomeSections).catch(() => {});
}, []);
useEffect(() => {
api.get('/partners').then(setPartners).catch(() => {});
}, []);
// Solange noch keine Google-Rezensionen konfiguriert/erfolgreich abgerufen wurden
// (googleReviews === null), zeigt die Seite die festen Beispiel-Texte aus den
// Uebersetzungen - danach die echten, im Admin-Bereich zwischengespeicherten Rezensionen.
const [googleReviews, setGoogleReviews] = useState(null);
const testimonials = googleReviews
? googleReviews.reviews
// Manche Google-Rezensionen sind reine Sterne-Bewertungen ohne Text - als
// Zitat-Karte ohne Zitat waeren die nur eine leere Huelle.
.filter((r) => r.review_text)
.map((r) => ({ name: r.author_name, text: r.review_text, rating: r.rating, photo: r.profile_photo_url }))
: t('home.testimonials');
useEffect(() => {
api
.get('/faqs')
.then((rows) => {
setFaqs(
rows.map((f) => ({
question: language === 'de' ? f.question_de : f.question_en,
answer: language === 'de' ? f.answer_de : f.answer_en,
}))
);
})
.catch(() => {});
}, [language]);
useEffect(() => {
api
.get('/google-reviews')
.then((res) => {
// Nur uebernehmen, wenn mindestens eine Rezension auch echten Text hat -
// sonst bliebe die Zitat-Karten-Liste leer (siehe testimonials-Filter oben).
if (res.rating != null && res.reviews.some((r) => r.review_text)) setGoogleReviews(res);
})
.catch(() => {});
}, []);
usePageMeta({
title: t('home.metaTitle'),
description: t('home.metaDescription'),
path: '/',
});
useEffect(() => {
if (faqs.length === 0) return;
const script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify({
'@context': 'https://schema.org',
'@type': 'FAQPage',
mainEntity: faqs.map((f) => ({
'@type': 'Question',
name: f.question,
acceptedAnswer: { '@type': 'Answer', text: f.answer },
})),
});
document.head.appendChild(script);
return () => document.head.removeChild(script);
}, [faqs]);
// Das Hintergrundvideo (~30 MB) erst laden, wenn sein Abschnitt in die Naehe des Bildschirms
// kommt, und ausserhalb pausieren - vorher startete der Download (autoPlay) sofort beim
// Seitenaufruf und konkurrierte mit Hero-Bild, Schriften und Skripten um die Bandbreite.
const videoRef = useRef(null);
useEffect(() => {
const video = videoRef.current;
if (!video) return undefined;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
if (!video.getAttribute('src')) video.src = youtubeVideo;
// Kann an Energiespar-/Autoplay-Regeln scheitern - dann bleibt das Poster stehen.
video.play().catch(() => {});
} else if (!video.paused) {
video.pause();
}
},
{ rootMargin: '300px 0px' }
);
observer.observe(video);
return () => observer.disconnect();
}, []);
const heroRef = useRef(null);
const { scrollYProgress: heroProgress } = useScroll({ target: heroRef, offset: ['start start', 'end start'] });
const heroImageY = useTransform(heroProgress, [0, 1], ['-6%', '6%']);
const heroTextY = useTransform(heroProgress, [0, 1], ['0%', '60%']);
const heroOpacity = useTransform(heroProgress, [0, 0.8], [1, 0]);
const ctaOpacity = useTransform(heroProgress, [0.5, 1], [0, 1]);
const ctaPointerEvents = useTransform(ctaOpacity, (v) => (v > 0.05 ? 'auto' : 'none'));
const ctaY = useTransform(heroProgress, [0.5, 1], [16, 0]);
return (
{t('home.stickyCta')}
{section('hero').eyebrow}
)}{stat.value}
{stat.label}
{t('home.brandsHeading')}
{section('youtube').eyebrow}
)}{section('craft').eyebrow}
)}{section('advice').eyebrow}
)}{section('insights').eyebrow}
)}{t('home.reviewsText')}
{t('home.reviewsLink')}{section('more').eyebrow}
)}