"use client"; import React, { useState, useEffect } from 'react'; import { Search, ChevronDown } from 'lucide-react'; import { usePathname } from 'next/navigation'; import { getClient } from '../clients/actions'; interface EPDHeaderProps { className?: string; } interface ClientData { id: string; first_name: string; last_name: string; birth_date: string; } export function EPDHeader({ className = "" }: EPDHeaderProps) { const pathname = usePathname(); const [selectedClient, setSelectedClient] = useState(null); const [isLoading, setIsLoading] = useState(false); // Context detection: Level 2 if URL contains /clients/[id] const isClientContext = pathname.match(/\/epd\/clients\/[^\/]+/); const clientId = isClientContext ? pathname.split('/')[3] : null; useEffect(() => { async function fetchClient() { if (!clientId) { setSelectedClient(null); return; } // Don't re-fetch if we already have the correct client if (selectedClient?.id === clientId) return; setIsLoading(true); try { const client = await getClient(clientId); if (client) { setSelectedClient(client); } } catch (error) { console.error('Failed to fetch client for header:', error); } finally { setIsLoading(false); } } fetchClient(); }, [clientId, selectedClient?.id]); return (
{/* Left: Logo */}
Mini-ECD
{/* Center: Client Selector (only in Level 2) */}
{(selectedClient || isLoading) && clientId && ( )}
{/* Right: Search */}
); }