Files
rk-frontend/src/components/navigation/appSidebar.tsx
T
2026-12-02 14:47:39 +01:00

109 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useEffect, useState } from 'react';
import { ChevronDown } from 'lucide-react';
import { proconNavigation } from '@/_config/procon-navigation';
interface NavigationItem { label: string; href?: string; children?: NavigationItem[] }
interface NavigationGroup { id: string; label: string; items: NavigationItem[] }
const groups: NavigationGroup[] = [
{ id: 'anlage', label: 'Anlage', items: [
{ label: 'Anlagenübersicht', href: '/' },
{ label: 'Anlage auswählen', href: '/alt/rk/anlagen' },
{ label: 'Neue Anlage', href: '/alt/rk/anlagen/new' },
{ label: 'Gruppen', href: '/groups' },
{ label: 'Units', href: '/units' },
{ label: 'Anlagen-PDF', href: '/anlagen/pdf' },
{ label: 'EA’s', href: '/signals' },
{ label: 'IO-Übersicht', href: '/ios' },
{ label: 'Datensicherung', href: '/datensicherung' },
] },
{ id: 'import', label: 'Import', items: [
{ label: 'IO Import', href: '/signals/import' },
] },
{ id: 'sps', label: 'SPS', items: [
{ label: 'SPS-Exporte', href: '/sps' },
{ label: 'OPC UA', href: '/sps/opcua' },
{ label: 'E/A-Diagnose', href: '/sps/io-diagnose' },
{ label: 'E/A-Simulation', href: '/sps/simulation' },
{ label: 'OPC-Strukturen', href: '/sps/opcua-strukturen' },
{ label: 'Motor-OPC-Zuordnung', href: '/motor-opc' },
{ label: 'Digitalsensoren Live', href: '/sensoren-digital/live' },
{ label: 'Aggregate', href: '/sps/aggregate' },
{ label: 'Sensoren Digital', href: '/sps/sensoren-digital' },
{ label: 'Sensoren Analog', href: '/sps/sensoren-analog' },
] },
{ id: 'procon', label: 'ProCon', items: [
{ label: 'ProCon-Übersicht', href: '/procon' },
...proconNavigation,
] },
{ id: 'visualisierung', label: 'Visualisierung', items: [
{ label: 'Web-Visu', href: '/visualisierung' },
{ label: 'Live-Anlagenansicht', href: '/live-anlage' },
{ label: 'Grafikbibliothek', href: '/visualisierung/bibliothek' },
{ label: 'VisuAggregate', href: '/visu-aggregate', children: [
{ label: 'Grafikübersicht', href: '/visu-aggregate/grafiken' },
] },
{ label: 'Positionstabelle', href: '/procon/positionen' },
{ label: 'Zuordnung Aggregate', href: '/procon/zuordnung' },
{ label: 'Zuordnung Signale Analog' },
{ label: 'Zuordnung Signale Digital', href: '/sensoren-digital/bearbeiten' },
] },
];
export default function AppSidebar() {
const pathname = usePathname();
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
// Only the most specific route is active, including on detail and export pages.
const flatten = (items: NavigationItem[]): NavigationItem[] => items.flatMap(item => [item, ...flatten(item.children ?? [])]);
const active = [...groups.flatMap(group => flatten(group.items)), { label: 'Alt', href: '/alt' }]
.filter(item => item.href && (pathname === item.href || item.href !== '/' && pathname.startsWith(item.href + '/')))
.sort((a, b) => b.href!.length - a.href!.length)[0]?.href;
const activeGroup = groups.find(group => flatten(group.items).some(item => item.href === active))?.id;
useEffect(() => {
if (activeGroup) setExpanded(current => ({ ...current, [activeGroup]: true }));
}, [activeGroup, pathname]);
function navigationLink(item: NavigationItem) {
if (!item.href) return <span aria-disabled="true" className="flex items-center justify-between gap-2 rounded px-3 py-1.5 text-gray-400">
<span>{item.label}</span><span className="text-[10px]">Geplant</span>
</span>;
const selected = active === item.href;
return <Link href={item.href} aria-current={selected ? 'page' : undefined}
className={`block rounded px-3 py-1.5 transition-colors focus-visible:outline-2 focus-visible:outline-blue-600 ${selected ? 'bg-blue-100 font-medium text-blue-900' : 'text-slate-700 hover:bg-slate-100'}`}>
{item.label}
</Link>;
}
function navigationEntry(item: NavigationItem) {
return <li key={item.label}>
{navigationLink(item)}
{item.children && <ul aria-label={`Unterpunkte ${item.label}`} className="ml-4 space-y-0.5 border-l pl-2">
{item.children.map(navigationEntry)}
</ul>}
</li>;
}
return <aside className="w-56 shrink-0 overflow-y-auto border-r bg-gray-50 p-3 sm:w-64">
<nav aria-label="Hauptnavigation" className="space-y-3 text-sm">
{groups.map(group => {
const open = expanded[group.id] ?? true;
return <section key={group.id}>
<button type="button" aria-expanded={open} aria-controls={`navigation-${group.id}`}
className="flex w-full items-center justify-between rounded px-3 py-2 text-left font-semibold text-slate-900 hover:bg-slate-100"
onClick={() => setExpanded(current => ({ ...current, [group.id]: !open }))}>
{group.label}<ChevronDown aria-hidden="true" size={16} className={open ? '' : '-rotate-90'} />
</button>
<ul id={`navigation-${group.id}`} hidden={!open} className="space-y-0.5">
{group.items.map(navigationEntry)}
</ul>
</section>;
})}
<div className="border-t pt-3">{navigationLink({ label: 'Alt – bisherige Seiten', href: '/alt' })}</div>
</nav>
</aside>;
}