import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { api } from '../../api/client.js';
import { useAuth } from '../../context/AuthContext.jsx';
const ICONS = {
tag: 'M4 4h7l9 9-7 7-9-9V4Zm3.5 3.5h.01',
car: 'M3 13.5 4.5 9A2 2 0 0 1 6.4 7.5h11.2A2 2 0 0 1 19.5 9L21 13.5M3 13.5v4a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1v-1h12v1a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1v-4M3 13.5h18M6.5 16.5h.01M17.5 16.5h.01',
box: 'M12 3 4 7.5v9L12 21l8-4.5v-9L12 3Zm0 9 8-4.5M12 12v9M12 12 4 7.5',
mail: 'M4 6h16a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1Zm0 0 8 7 8-7',
};
const Icon = ({ path, className = 'h-6 w-6' }) => (
);
function AnalyticsWidget() {
const [report, setReport] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
api.get('/analytics/report').then(setReport).catch((e) => setError(e.message));
}, []);
if (error) {
return (
);
}
if (!report) return null;
if (!report.configured) {
return (
Google Analytics
Noch nicht eingerichtet. Unter{' '}
Einstellungen → Website
{' '}
kannst du die Dashboard-Anbindung konfigurieren, um hier Besucherzahlen zu sehen.
);
}
const maxPageviews = Math.max(1, ...report.daily.map((d) => d.pageviews));
return (
Google Analytics
Letzte 7 Tage.
{report.totals.users}
Nutzer
{report.totals.sessions}
Sitzungen
{report.totals.pageviews}
Seitenaufrufe
{report.daily.length > 0 && (
{report.daily.map((d) => (
{d.date.slice(6, 8)}.{d.date.slice(4, 6)}.
))}
)}
{report.top_pages.length > 0 && (
Meistbesuchte Seiten
{report.top_pages.map((p) => (
-
{p.path}
{p.pageviews}
))}
)}
);
}
export default function Dashboard() {
const { user, hasPermission } = useAuth();
const [stats, setStats] = useState({});
useEffect(() => {
if (hasPermission('brands.manage')) api.get('/brands').then((rows) => setStats((s) => ({ ...s, brands: rows.length })));
if (hasPermission('models.manage') || hasPermission('packages.manage')) {
api.get('/models').then((rows) => setStats((s) => ({ ...s, models: rows.length })));
}
if (hasPermission('packages.manage')) api.get('/packages').then((rows) => setStats((s) => ({ ...s, packages: rows.length })));
if (hasPermission('contact.manage')) {
api.get('/contact').then((rows) => setStats((s) => ({ ...s, newContacts: rows.filter((c) => c.status === 'new').length })));
}
}, []);
const cards = [
{ key: 'brands', label: 'Marken', icon: 'tag', hint: 'Die Hersteller, z. B. Audi oder BMW.', value: stats.brands, to: '/admin/brands', show: hasPermission('brands.manage') },
{ key: 'models', label: 'Modelle', icon: 'car', hint: 'Die Fahrzeuge einer Marke, z. B. A4.', value: stats.models, to: '/admin/models', show: hasPermission('models.manage') },
{ key: 'packages', label: 'Pakete', icon: 'box', hint: 'Die Sound-Pakete für ein Modell.', value: stats.packages, to: '/admin/packages', show: hasPermission('packages.manage') },
{ key: 'contacts', label: 'Neue Anfragen', icon: 'mail', hint: 'Kunden, die noch auf Antwort warten.', value: stats.newContacts, to: '/admin/contact-requests', show: hasPermission('contact.manage'), highlight: true },
].filter((c) => c.show);
return (
Willkommen, {user?.username}!
Hier siehst du auf einen Blick, wie viel in deinem Shop hinterlegt ist. Klicke auf eine Kachel, um dorthin zu gelangen.
{cards.length === 0 ? (
Für deinen Account sind noch keine Bereiche freigeschaltet. Bitte wende dich an einen Administrator.
) : (
{cards.map((card) => (
0
? 'border-brand-300 bg-brand-50 dark:border-brand-900 dark:bg-brand-900/20'
: 'border-neutral-200 bg-white dark:border-neutral-800 dark:bg-neutral-900'
}`}
>
{card.label}
{card.value ?? '…'}
{card.hint}
))}
)}
{hasPermission('settings.manage') &&
}
);
}