import { useEffect, useRef, useState } from 'react';
import { Link } from 'react-router-dom';
import { motion, useScroll, useTransform, useReducedMotion } 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 { useIsAreaHidden, useSiteSettings } from '../../context/SiteSettingsContext.jsx';
import { isPathHidden } from '../../utils/siteAreas.js';
import { useTextStyle } from '../../context/TextStylesContext.jsx';
import { useLanguage } from '../../context/LanguageContext.jsx';
import { dragAnchorStyle } from '../../utils/dragAnchorPosition.js';
import { useLiveEdit } from '../../context/LiveEditContext.jsx';
import { EditableArea, EditableHtml, EditableT, EditableText } from '../../components/live/Editable.jsx';
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';
// Kurze 480p-Schleife (< 1 MB) fuer Handys statt der vollen 8,9-MB-Fassung.
import youtubeVideoMobile from '../../assets/videos/youtube-hintergrund-mobil.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. Zeigt das Ziel auf eine
* im Dashboard ausgeblendete Seite, entfaellt der Button (er wuerde nur zur Startseite fuehren). */
function SectionButton({ slug, label, url, className, style }) {
const { hidden_areas: hiddenAreas } = useSiteSettings();
const live = useLiveEdit();
// Im Bearbeiten-Modus immer sichtbar (auch ohne Text/Ziel), damit man ihn befuellen kann.
if (slug && live.canEdit('home_sections')) {
return (
,
// 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 isHidden = useIsAreaHidden();
const live = useLiveEdit();
const editing = live.canEdit('home_sections');
const showVehicles = !isHidden('page.vehicles');
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({});
// Faellt /home-sections aus, greifen die Standardtexte - sonst bliebe z. B. die
${t('home.heroSubtitle')}
` : ''; return { // Inkl. noch nicht gespeicherter Live-Aenderungen (Bearbeiten-Modus). eyebrow: live.getValue('home_sections', slug, 'eyebrow', language, s[`eyebrow_${language}`] || ''), title: live.getValue('home_sections', slug, 'title', language, s[`title_${language}`] || fallbackTitle), bodyHtml: live.getValue('home_sections', slug, 'body_html', language, s[`body_html_${language}`] || fallbackBody), 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: live.getValue('home_sections', slug, 'button_label', language, s[`button_label_${language}`] || ''), buttonUrl: live.getValue('home_sections', slug, 'button_url', 'de', 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(() => setHomeSectionsFailed(true)); }, []); 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) => ({ id: f.id, 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 || isHidden('home.faq')) 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]); // Link "/#faq" (z. B. aus dem Admin): die FAQ laden erst nach, daher erst danach hinscrollen. useEffect(() => { if (faqs.length > 0 && window.location.hash === '#faq') { document.getElementById('faq')?.scrollIntoView(); } }, [faqs]); // Das Hintergrundvideo 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; // Bei "Bewegung reduzieren" oder Datensparmodus bleibt es beim Standbild (Poster). const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches; const saveData = navigator.connection?.saveData; if (reduceMotion || saveData) return undefined; const isSmallScreen = window.matchMedia?.('(max-width: 767px), (pointer: coarse)').matches; const source = isSmallScreen ? youtubeVideoMobile : youtubeVideo; const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { if (!video.getAttribute('src')) video.src = source; // 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'] }); // Parallax laeuft ueber Motion-Values im style - das deckt MotionConfig (App.jsx) nicht ab, // deshalb bei "Bewegung reduzieren" hier selbst auf feste Werte schalten. const reduceMotion = useReducedMotion(); const heroImageY = useTransform(heroProgress, [0, 1], reduceMotion ? ['0%', '0%'] : ['-6%', '6%']); const heroTextY = useTransform(heroProgress, [0, 1], reduceMotion ? ['0%', '0%'] : ['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 ({stat.value}
{stat.label}