// Copyright (c) 2026 PowerOn AG // All rights reserved. import { type FC, useState, useEffect, useCallback } from 'react'; import { useLocation } from 'react-router-dom'; import { FaChevronDown, FaChevronRight } from 'react-icons/fa'; import type { PanelProps } from './types'; import styles from './Panel.module.css'; function _loadCollapsed(key: string, fallback: boolean): boolean { try { const stored = localStorage.getItem(`panel-collapse:${key}`); if (stored !== null) return stored === '1'; } catch { /* noop */ } return fallback; } function _saveCollapsed(key: string, value: boolean): void { try { localStorage.setItem(`panel-collapse:${key}`, value ? '1' : '0'); } catch { /* noop */ } } export const Panel: FC = ({ variant = 'card', title, id, subtitle, actions, collapsible = true, defaultCollapsed = false, collapseKey, className = '', style, fill = false, children, }) => { const { pathname } = useLocation(); const persistKey = collapseKey ?? `${pathname}:${id}`; const [collapsed, setCollapsed] = useState(() => _loadCollapsed(persistKey, defaultCollapsed)); useEffect(() => { _saveCollapsed(persistKey, collapsed); }, [persistKey, collapsed]); const _toggleCollapsed = useCallback(() => { if (collapsible) setCollapsed((prev) => !prev); }, [collapsible]); return (
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); _toggleCollapsed(); } } : undefined } >
{title} {subtitle && {subtitle}}
{collapsible && ( {collapsed ? : } )} {actions &&
e.stopPropagation()}>{actions}
}
{children}
); };