This commit is contained in:
marco committed 2026-10-26 14:56:31 +01:00
1 parent 6baea55216
commit d9962e22b8
15 files changed
+358 -30

No files matched your search

@@ -1,6 +1,6 @@
'use client';
import { useLayoutEffect, useRef, useState, type FormEvent, type KeyboardEvent } from 'react';
import { useEffect, useLayoutEffect, useRef, useState, type FormEvent, type KeyboardEvent } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Select } from '@/components/ui/select';
@@ -15,8 +15,9 @@ const texts = [
] as const;
type BmkDefault = 'unit' | 'view';
const filterStorageKey = 'rk.visuAggregate.filters.v1';
function AggregateRow({ initial, bmkDefault }: { initial: VisuAggregateRow; bmkDefault: BmkDefault }) {
function AggregateRow({ initial, bmkDefault, hidden }: { initial: VisuAggregateRow; bmkDefault: BmkDefault; hidden: boolean }) {
const [row, setRow] = useState(initial);
const [persisted, setPersisted] = useState(initial.persisted);
const [antriebId, setAntriebId] = useState(String(initial.antriebId ?? ''));
@@ -63,6 +64,10 @@ function AggregateRow({ initial, bmkDefault }: { initial: VisuAggregateRow; bmkD
const controlIndex = Math.max(0, controls.indexOf(target));
let next = event.key === 'ArrowDown' ? event.currentTarget.nextElementSibling : event.currentTarget.previousElementSibling;
while (next instanceof HTMLTableRowElement) {
if (next.hidden) {
next = event.key === 'ArrowDown' ? next.nextElementSibling : next.previousElementSibling;
continue;
}
const nextCell = next.cells[cell.cellIndex];
const nextControls = nextCell ? Array.from(nextCell.querySelectorAll<HTMLElement>(selector)) : [];
const control = nextControls[controlIndex] ?? nextControls[0];
@@ -104,9 +109,10 @@ function AggregateRow({ initial, bmkDefault }: { initial: VisuAggregateRow; bmkD
} finally { setSaving(false); }
}
return <tr onKeyDown={handleKeyDown} className={persisted ? 'align-top bg-green-50/50' : 'align-top'}>
return <tr hidden={hidden} onKeyDown={handleKeyDown} className={persisted ? 'align-top bg-green-50/50' : 'align-top'}>
<th scope="row" className={`sticky left-0 z-10 min-w-56 border p-2 text-left font-normal ${persisted ? 'bg-green-50' : 'bg-white'}`}>
<div className="font-medium">{initial.componentLabel}</div>
<div className="text-xs text-gray-500">{initial.hasGraphic ? 'Mit Grafik' : 'Ohne Grafik'}</div>
{persisted && <Link className="mt-1 inline-block text-xs underline" href={`/rk2/visu-aggregate/${initial.componentId}/grafiken`}>Grafiken bearbeiten</Link>}
<div className={persisted ? 'mt-1 text-xs text-green-700' : 'mt-1 text-xs text-gray-500'}>
{persisted ? 'In DB gespeichert' : 'Noch nicht gespeichert'}
@@ -153,6 +159,35 @@ function AggregateRow({ initial, bmkDefault }: { initial: VisuAggregateRow; bmkD
export default function VisuAggregateTable({ rows }: { rows: VisuAggregateRow[] }) {
const [bmkDefault, setBmkDefault] = useState<BmkDefault>('unit');
const [filter, setFilter] = useState('');
const [graphicFilter, setGraphicFilter] = useState('all');
const [filtersLoaded, setFiltersLoaded] = useState(false);
useEffect(() => {
try {
const saved: unknown = JSON.parse(window.localStorage.getItem(filterStorageKey) ?? 'null');
if (saved && typeof saved === 'object' && !Array.isArray(saved)) {
const values = saved as Record<string, unknown>;
if (typeof values.filter === 'string') setFilter(values.filter);
if (values.graphicFilter === 'all' || values.graphicFilter === 'with' || values.graphicFilter === 'without')
setGraphicFilter(values.graphicFilter);
}
} catch { /* Bei gesperrtem Browserspeicher oder ungültigen Daten bleiben die Standardfilter aktiv. */ }
setFiltersLoaded(true);
}, []);
useEffect(() => {
// Gespeicherte Werte erst laden, bevor der initiale Zustand geschrieben werden darf.
if (!filtersLoaded) return;
try {
window.localStorage.setItem(filterStorageKey, JSON.stringify({ filter, graphicFilter }));
} catch { /* Die Filter funktionieren auch ohne verfügbaren Browserspeicher. */ }
}, [filter, graphicFilter, filtersLoaded]);
const terms = filter.trim().toLocaleLowerCase('de').split(/\s+/).filter(Boolean);
const visible = new Set(rows.filter(row => {
const text = [row.componentLabel, row.antriebId, row.analogSensorId, row.defaultView, row.defaultBmk,
...texts.map(({ key }) => row[key])].join(' ').toLocaleLowerCase('de');
return terms.every(term => text.includes(term)) &&
(graphicFilter === 'all' || (graphicFilter === 'with' ? !!row.hasGraphic : !row.hasGraphic));
}).map(row => row.componentId));
const headers = ['Komponente / Aktion', 'Antrieb ID', 'AnalogSensor ID', 'Menge ID', ...texts.slice(0, 3).map(item => item.label),
...switches.slice(0, 8).map(item => item.label), 'Anzahl Motoren', ...switches.slice(8, 11).map(item => item.label),
...texts.slice(3).map(item => item.label), ...switches.slice(11).map(item => item.label)];
@@ -170,6 +205,17 @@ export default function VisuAggregateTable({ rows }: { rows: VisuAggregateRow[]
</Select>
<p className="text-sm text-gray-600">Wird beim Klick auf „Default“ in der BMK-Spalte übernommen.</p>
</div>
<div className="mb-3 flex flex-wrap items-end gap-3">
<label className="text-sm">Suche<Input type="search" className="w-80 max-w-full" placeholder="Komponente, ID, Name, View, BMK oder HMI-Text"
value={filter} onChange={event => setFilter(event.target.value)} /></label>
<label className="text-sm">Grafik<Select className="w-44" value={graphicFilter} onChange={event => setGraphicFilter(event.target.value)}>
<option value="all">Alle</option><option value="with">Mit Grafik</option><option value="without">Ohne Grafik</option>
</Select></label>
<Button type="button" size="sm" variant="secondary" disabled={!filter && graphicFilter === 'all'}
onClick={() => { setFilter(''); setGraphicFilter('all'); }}>Filter zurücksetzen</Button>
<span role="status" className="text-xs text-gray-600">{visible.size} von {rows.length} Aggregaten</span>
</div>
<p className="mb-2 text-xs text-gray-500">„Mit Grafik“: mindestens ein gespeichertes SVG oder XAML, einschließlich Pfeilen. Die Filter beziehen sich auf den geladenen Stand; Eingaben bleiben beim Filtern erhalten.</p>
<p className="mb-3 text-sm text-gray-600">
Alt + ↑ / ↓: gleiche Spalte in der vorherigen / nächsten Zeile. Strg + S (Mac: Cmd + S): aktuelle Zeile speichern.
</p>
@@ -177,7 +223,9 @@ export default function VisuAggregateTable({ rows }: { rows: VisuAggregateRow[]
<table className="w-full border-separate border-spacing-0 text-sm">
<caption className="sr-only">VisuAggregate der ausgewählten Anlage – eine Zeile pro Antrieb-Komponente</caption>
<thead className="sticky top-0 z-20 bg-gray-100"><tr>{headers.map(label => <th scope="col" key={label} className="border p-2 text-left">{label}</th>)}</tr></thead>
<tbody>{rows.map(row => <AggregateRow key={row.componentId} initial={row} bmkDefault={bmkDefault} />)}</tbody>
<tbody>{rows.map(row => <AggregateRow key={row.componentId} initial={row} bmkDefault={bmkDefault} hidden={!visible.has(row.componentId)} />)}
{!visible.size && <tr><td colSpan={headers.length} className="p-4 text-gray-500">Keine Aggregate für diese Filter gefunden.</td></tr>}
</tbody>
</table>
</div>
</div>;