'use client'; import { useState, useRef, useEffect } from 'react'; import { useSelectedChild } from '@/contexts/SelectedChildContext'; import type { ChildStats } from '@/api/dashboard'; function getAvatarUrl(child: ChildStats | null): string | null { if (!child) return null; const url = child.avatar_url || child.avatar; if (!url) return null; if (typeof url === 'string' && url.startsWith('http')) return url; const base = typeof window !== 'undefined' ? `${window.location.protocol}//${window.location.hostname}:8123` : ''; return typeof url === 'string' && url.startsWith('/') ? `${base}${url}` : `${base}/${url}`; } /** Минималистичный выбор ребёнка слева от нижней навигации */ export function ChildSelectorCompact() { const { selectedChild, childrenList, setSelectedChild, loading } = useSelectedChild(); const [open, setOpen] = useState(false); const ref = useRef(null); useEffect(() => { const onOutside = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener('mousedown', onOutside); return () => document.removeEventListener('mousedown', onOutside); }, []); if (loading && childrenList.length === 0) { return (
person
); } if (childrenList.length === 0) return null; const avatarUrl = getAvatarUrl(selectedChild); const initial = selectedChild?.name?.charAt(0)?.toUpperCase() ?? '?'; return (
{open && (
{childrenList.map((child) => { const isSelected = selectedChild?.id === child.id; const url = getAvatarUrl(child); return ( ); })}
)}
); } export function ChildSelector() { const { selectedChild, childrenList, setSelectedChild, loading } = useSelectedChild(); const [open, setOpen] = useState(false); const ref = useRef(null); useEffect(() => { const onOutside = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener('mousedown', onOutside); return () => document.removeEventListener('mousedown', onOutside); }, []); if (loading && childrenList.length === 0) { return (
Загрузка...
); } if (childrenList.length === 0) return null; const avatarUrl = getAvatarUrl(selectedChild); const displayName = selectedChild?.name ?? 'Выберите ребёнка'; return (
{open && (
{childrenList.map((child) => { const isSelected = selectedChild?.id === child.id; const url = getAvatarUrl(child); return ( ); })}
)}
); }