'use client'; import { useEffect, useState } from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import Link from 'next/link'; import { ArrowLeftIcon } from '@heroicons/react/24/outline'; import { useLanguage } from '@/context/LanguageContext'; import { legalPagesApi } from '@/lib/api'; interface LegalPageLayoutProps { slug: string; initialLocale: 'en' | 'es'; title: string; content: string; lastUpdated?: string; } function extractLastUpdated(contentMarkdown: string, updatedAt?: string): string | undefined { const match = contentMarkdown?.match(/Last updated:\s*(.+)/i); return match ? match[1].trim() : updatedAt; } export default function LegalPageLayout({ slug, initialLocale, title: initialTitle, content: initialContent, lastUpdated: initialLastUpdated, }: LegalPageLayoutProps) { const { locale, t } = useLanguage(); const [title, setTitle] = useState(initialTitle); const [content, setContent] = useState(initialContent); const [lastUpdated, setLastUpdated] = useState(initialLastUpdated); const [loadedLocale, setLoadedLocale] = useState(initialLocale); useEffect(() => { if (locale === loadedLocale) { return; } // Returning to the server-rendered language: restore SSR content without a fetch if (locale === initialLocale) { setTitle(initialTitle); setContent(initialContent); setLastUpdated(initialLastUpdated); setLoadedLocale(initialLocale); return; } let cancelled = false; legalPagesApi .getBySlug(slug, locale) .then(({ page }) => { if (cancelled || !page) { return; } setTitle(page.title); setContent(page.contentMarkdown); setLastUpdated(extractLastUpdated(page.contentMarkdown, page.updatedAt)); setLoadedLocale(locale); }) .catch(() => { // Keep the server-rendered content if the re-fetch fails }); return () => { cancelled = true; }; }, [locale, loadedLocale, initialLocale, slug, initialTitle, initialContent, initialLastUpdated]); return (
{/* Back link */} {t('legalPage.backToHome')} {/* Title */}

{title}

{lastUpdated && lastUpdated !== '[Insert Date]' && (

{t('legalPage.lastUpdated', { date: lastUpdated })}

)}
{/* Markdown content */}
(

{children}

), h2: ({ children }) => (

{children}

), h3: ({ children }) => (

{children}

), h4: ({ children }) => (

{children}

), // Style paragraphs p: ({ children }) => (

{children}

), // Style lists ul: ({ children }) => (
    {children}
), ol: ({ children }) => (
    {children}
), li: ({ children }) => (
  • {children}
  • ), // Style links a: ({ href, children }) => ( {children} ), // Style horizontal rules hr: () => (
    ), // Style blockquotes blockquote: ({ children }) => (
    {children}
    ), // Style tables table: ({ children }) => (
    {children}
    ), thead: ({ children }) => ( {children} ), tbody: ({ children }) => ( {children} ), tr: ({ children }) => ( {children} ), th: ({ children }) => ( {children} ), td: ({ children }) => ( {children} ), // Style code blocks code: ({ className, children }) => { const isInline = !className; if (isInline) { return ( {children} ); } return ( {children} ); }, pre: ({ children }) => (
                      {children}
                    
    ), // Style strong and emphasis strong: ({ children }) => ( {children} ), em: ({ children }) => ( {children} ), }} > {content}
    {/* Back to top link */}
    ); }