Files
rk-frontend/src/components/rk2/visu-aggregate/graphicsOverview.tsx
T
2026-10-26 07:34:27 +01:00

71 lines
5.5 KiB
TypeScript

'use client';
import Link from 'next/link';
import { useState } from 'react';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { api_viewAggregate_exportXaml } from '@/actions/rk2/api-procon-view-aggregate';
import type { ViewAggregateOverview } from '@/types/anlagen/procon-view-aggregate';
export default function GraphicsOverview({ aggregates, anlageId }: { aggregates: ViewAggregateOverview[]; anlageId: string }) {
const [filter, setFilter] = useState('');
const [busy, setBusy] = useState(false);
const [message, setMessage] = useState('');
const [error, setError] = useState('');
const terms = filter.trim().toLocaleLowerCase('de').split(/\s+/).filter(Boolean);
const visible = aggregates.map(aggregate => ({ ...aggregate, elemente: aggregate.elemente.filter(element => {
const text = [aggregate.antriebId, aggregate.name, aggregate.viewName, element.key, element.name, element.typ,
...(element.darstellung === 'xaml' ? element.varianten.map(variant => variant.zustand) : [])].join(' ').toLocaleLowerCase('de');
return terms.every(term => text.includes(term));
}) })).filter(aggregate => aggregate.elemente.length > 0 || (!terms.length && aggregates.find(item => item.componentId === aggregate.componentId)?.elemente.length === 0));
async function exportFiles() {
if (busy) return;
setBusy(true); setMessage(''); setError('');
try {
const result = await api_viewAggregate_exportXaml(anlageId, visible.map(aggregate => ({
componentId: aggregate.componentId, elementKeys: aggregate.elemente.map(element => element.key),
})));
if (result.success) setMessage(result.count + ' XAML-Dateien in ' + result.directory + ' angelegt. ' + result.skipped + ' Varianten ohne XAML übersprungen.');
else setError(result.error);
} finally { setBusy(false); }
}
const count = visible.reduce((sum, item) => sum + item.elemente.length, 0);
return <div>
<Link href="/rk2/visu-aggregate" className="text-sm underline">Zur VisuAggregate-Tabelle</Link>
<h1 className="my-3 text-xl font-semibold">Grafikübersicht</h1>
<p className="mb-4 text-sm text-gray-600">{count} Elemente in {visible.length} gespeicherten Aggregaten der ausgewählten Anlage. Angezeigt wird der gespeicherte Stand.</p>
{!visible.length && <p>Noch keine VisuAggregate gespeichert.</p>}
<div className="mb-3 flex flex-wrap items-center gap-2">
<label htmlFor="graphics-filter" className="text-sm">Filter</label>
<Input id="graphics-filter" className="max-w-md" type="search" placeholder="Antrieb-ID, Name, View, Element oder Zustand" value={filter} disabled={busy} onChange={event => setFilter(event.target.value)} />
<Button type="button" size="sm" disabled={busy || !visible.some(item => item.elemente.some(element => element.darstellung === 'xaml'))} onClick={exportFiles}>{busy ? 'Dateien werden angelegt …' : 'XAML anlegen'}</Button>
</div>
<p className="mb-3 text-xs text-gray-500">Exportiert die gefilterten Elemente aus der Datenbank. Gleichnamige Dateien werden ersetzt; andere Dateien bleiben erhalten.</p>
{message && <p role="status" className="mb-3 text-sm text-green-700">{message}</p>}
{error && <p role="alert" className="mb-3 text-sm text-red-600">{error}</p>}
<div className="space-y-3">{visible.map(aggregate => <section key={aggregate.componentId} className="rounded border bg-white">
<div className="flex flex-wrap items-center justify-between gap-2 bg-gray-50 p-2">
<div><h2 className="text-sm font-semibold">{aggregate.antriebId} · {aggregate.name}</h2><p className="text-xs text-gray-500">{aggregate.viewName || 'Kein View-Name'}</p></div>
<Link className="text-sm underline" href={`/rk2/visu-aggregate/${aggregate.componentId}/grafiken`}>Grafiken bearbeiten</Link>
</div>
{!aggregate.elemente.length ? <p className="p-2 text-xs text-gray-500">Noch keine Grafikelemente.</p> : <div className="overflow-x-auto">
<table className="w-full border-collapse text-left text-xs">
<thead><tr>{['Element', 'Typ', 'Breite', 'Höhe', 'X', 'Y', 'Z', 'Sichtbar', 'Zustände / Dateien'].map(label =>
<th key={label} className="border-b p-2" scope="col">{label}</th>)}</tr></thead>
<tbody>{aggregate.elemente.map(element => <tr key={element._id ?? element.key} className="align-top">
<th scope="row" className="border-b p-2 font-medium">{element.key}{element.name && <span className="block font-normal text-gray-500">{element.name}</span>}</th>
<td className="border-b p-2">{element.typ}</td>
{[element.width, element.height, element.posX, element.posY, element.zIndex].map((value, index) => <td key={index} className="border-b p-2">{value}</td>)}
<td className="border-b p-2">{element.show ? 'Ja' : 'Nein'}</td>
<td className="border-b p-2">{element.darstellung === 'standard' ? <span className="text-gray-500">Standardanzeige · keine XAML</span>
: !element.varianten.length ? <span className="text-gray-500">Keine Zustände</span>
: <ul className="space-y-1">{element.varianten.map(variant => <li key={variant.zustand}>
<span className={variant.xamlBase64 ? 'text-green-700' : 'text-gray-500'}>{variant.zustand} · {variant.xamlBase64 ? 'XAML vorhanden' : 'ohne XAML'}</span>
<span className="block break-all text-gray-500">{aggregate.viewName}_{element.key}_{variant.zustand}.xaml</span>
</li>)}</ul>}</td>
</tr>)}</tbody>
</table>
</div>}
</section>)}</div>
</div>;
}