"use client"; import React, { useMemo, useRef, useEffect } from "react"; import { Input, Button, ScrollShadow } from "@nextui-org/react"; import { Search } from "lucide-react"; import { useTranslation } from "@/shared/lib/useTranslation"; import { SettingModalTab } from "./console.type"; export interface SettingsMenuItem { key: SettingModalTab; label: string; description?: string; icon?: React.ReactNode; } export interface SettingsGroup { key: string; title: string; items: SettingsMenuItem[]; } interface Props { activeKey: SettingModalTab; onSelect: (key: SettingModalTab) => void; searchQuery: string; onSearchChange: (val: string) => void; groups: SettingsGroup[]; filteredKeys: SettingModalTab[]; // which item keys match the current search } const SystemSettingsSidebar: React.FC = ({ activeKey, onSelect, searchQuery, onSearchChange, groups, filteredKeys, }) => { const { t } = useTranslation(); const sidebarScrollRef = useRef(null); const activeItemRefs = useRef>({}); // filter groups based on filteredKeys const visibleGroups = useMemo(() => { const keySet = new Set(filteredKeys); return groups .map((g) => ({ ...g, items: g.items.filter((it) => keySet.has(it.key)), })) .filter((g) => g.items.length > 0); }, [groups, filteredKeys]); const hasResults = visibleGroups.length > 0; // Auto-scroll to active item when activeKey changes useEffect(() => { const activeElement = activeItemRefs.current[activeKey]; const scrollContainer = sidebarScrollRef.current; if (activeElement && scrollContainer) { // Get the scroll container element (ScrollShadow wraps content) const scrollElement = (scrollContainer.querySelector( '[data-scrollable="true"]' ) as HTMLElement) || scrollContainer; const containerRect = scrollElement.getBoundingClientRect(); const elementRect = activeElement.getBoundingClientRect(); // Check if element is visible in container const isElementVisible = elementRect.top >= containerRect.top && elementRect.bottom <= containerRect.bottom; if (!isElementVisible) { // Calculate scroll position to center the active item const containerHeight = containerRect.height; const elementTop = activeElement.offsetTop; const elementHeight = activeElement.offsetHeight; const scrollTop = elementTop - containerHeight / 2 + elementHeight / 2; scrollElement.scrollTo({ top: Math.max(0, scrollTop), behavior: "smooth", }); } } }, [activeKey]); return (
} isClearable onClear={() => onSearchChange("")} /> {!hasResults ? (
{t("No results")}
) : ( )}
); }; export default SystemSettingsSidebar;