import { useTranslation } from 'react-i18next'; import { useRef, useEffect, useState } from 'react'; import { SETTINGS_TREE } from './constants'; import { StarIcon } from './icons'; interface SettingsMobileTabsProps { activeSection: string; setActiveSection: (section: string) => void; favoritesCount: number; } export function SettingsMobileTabs({ activeSection, setActiveSection, favoritesCount, }: SettingsMobileTabsProps) { const { t } = useTranslation(); const scrollRef = useRef(null); const activeRef = useRef(null); const [expandedGroup, setExpandedGroup] = useState(null); // Scroll active tab into view useEffect(() => { if (activeRef.current && scrollRef.current) { const container = scrollRef.current; const activeEl = activeRef.current; const containerRect = container.getBoundingClientRect(); const activeRect = activeEl.getBoundingClientRect(); if (activeRect.left < containerRect.left || activeRect.right > containerRect.right) { activeEl.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' }); } } }, [activeSection]); // Auto-expand the group containing the active section useEffect(() => { for (const group of SETTINGS_TREE.groups) { if (group.children.some((child) => child.id === activeSection)) { setExpandedGroup(group.id); return; } } }, [activeSection]); const handleGroupTap = (groupId: string) => { if (expandedGroup === groupId) { setExpandedGroup(null); } else { setExpandedGroup(groupId); // Auto-select first child when expanding const group = SETTINGS_TREE.groups.find((g) => g.id === groupId); if (group && group.children.length > 0) { setActiveSection(group.children[0].id); } } }; const isGroupActive = (groupId: string) => { const group = SETTINGS_TREE.groups.find((g) => g.id === groupId); return group?.children.some((child) => child.id === activeSection) ?? false; }; const isFavoritesActive = activeSection === 'favorites'; // Check if active section is a special item const isSpecialActive = (itemId: string) => activeSection === itemId; // Special items excluding favorites const specialItems = SETTINGS_TREE.specialItems.filter((item) => item.id !== 'favorites'); return (
{/* Level 1: Favorites + special items + group chips */}
{/* Favorites chip */} {/* Special item chips (branding, theme, analytics, buttons) */} {specialItems.map((item) => { const isActive = isSpecialActive(item.id); return ( ); })} {/* Group chips */} {SETTINGS_TREE.groups.map((group) => { const hasActiveChild = isGroupActive(group.id); const isExpanded = expandedGroup === group.id; return ( ); })}
{/* Level 2: Sub-item chips (shown when a group is expanded) */} {expandedGroup && (
{SETTINGS_TREE.groups .find((g) => g.id === expandedGroup) ?.children.map((child) => { const isActive = activeSection === child.id; return ( ); })}
)}
); }