'use client'; import { useEffect, useMemo, useState } from 'react'; function normalize(value) { return (value || '').toLowerCase().trim(); } function getDomain(url) { try { return new URL(url).hostname.replace('www.', ''); } catch { return ''; } } export default function LinksPage({ profile, socialLinks }) { const [query, setQuery] = useState(''); const [activeCategory, setActiveCategory] = useState('Tous'); const [theme, setTheme] = useState('light'); const [links, setLinks] = useState(socialLinks || []); useEffect(() => { const storedTheme = localStorage.getItem('theme') || 'light'; setTheme(storedTheme); document.documentElement.setAttribute('data-theme', storedTheme); async function loadLinks() { const response = await fetch('/api/links'); if (!response.ok) { return; } const data = await response.json(); setLinks(data.links || []); } loadLinks(); }, []); const categories = useMemo(() => { const set = new Set((links || []).map((link) => link.category || 'Autres')); return ['Tous', ...Array.from(set)]; }, [links]); const filteredLinks = useMemo(() => { return (links || []).filter((link) => { const matchesCategory = activeCategory === 'Tous' || (link.category || 'Autres') === activeCategory; const target = normalize(`${link.name} ${link.description} ${link.url}`); const matchesSearch = target.includes(normalize(query)); return matchesCategory && matchesSearch; }); }, [links, activeCategory, query]); function toggleTheme() { const nextTheme = theme === 'light' ? 'dark' : 'light'; setTheme(nextTheme); localStorage.setItem('theme', nextTheme); document.documentElement.setAttribute('data-theme', nextTheme); } async function trackClick(linkId) { await fetch('/api/analytics/click', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ linkId }) }); } return (
Avatar

{profile.name}

{profile.bio}

setQuery(event.target.value)} placeholder="Rechercher un lien..." aria-label="Rechercher un lien" />
{categories.map((category) => ( ))}
{filteredLinks.length === 0 ? (

Aucun lien ne correspond à ta recherche.

) : ( filteredLinks.map((link) => ( trackClick(link.id)} > {link.name}

{link.name}

{link.description}

{getDomain(link.url)}
)) )}
); }