2026-03-02 10:13:50 +01:00
|
|
|
import { useState, useMemo } from "react";
|
|
|
|
|
import { Radio, ChevronDown, ChevronUp, Zap, ZapOff } from "lucide-react";
|
|
|
|
|
import type { MqttData, MqttEntity } from "../api";
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
data: MqttData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function MqttCard({ data }: Props) {
|
|
|
|
|
const [expanded, setExpanded] = useState(false);
|
|
|
|
|
const [filter, setFilter] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
// Group entities by category
|
|
|
|
|
const grouped = useMemo(() => {
|
|
|
|
|
const map: Record<string, MqttEntity[]> = {};
|
|
|
|
|
for (const e of data.entities) {
|
|
|
|
|
const cat = e.category || "other";
|
|
|
|
|
if (!map[cat]) map[cat] = [];
|
|
|
|
|
map[cat].push(e);
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}, [data.entities]);
|
|
|
|
|
|
|
|
|
|
const categories = Object.keys(grouped).sort();
|
|
|
|
|
const filtered = filter ? grouped[filter] || [] : data.entities;
|
|
|
|
|
const shown = expanded ? filtered : filtered.slice(0, 8);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-02 10:54:28 +01:00
|
|
|
<div className="deck-card p-5 animate-fade-in" data-accent="iris">
|
2026-03-02 10:13:50 +01:00
|
|
|
{/* Header */}
|
|
|
|
|
<div className="flex items-center justify-between mb-4">
|
2026-03-02 10:54:28 +01:00
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<Radio className="w-4 h-4 text-iris" />
|
|
|
|
|
<h3 className="text-sm font-semibold text-base-900">MQTT</h3>
|
|
|
|
|
<span className="tag border-iris/30 text-iris bg-iris/5">
|
|
|
|
|
{data.entities.length}
|
2026-03-02 10:13:50 +01:00
|
|
|
</span>
|
|
|
|
|
</div>
|
2026-03-02 10:54:28 +01:00
|
|
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
2026-03-02 10:13:50 +01:00
|
|
|
{data.connected ? (
|
2026-03-02 10:54:28 +01:00
|
|
|
<Zap className="w-3.5 h-3.5 text-iris" />
|
2026-03-02 10:13:50 +01:00
|
|
|
) : (
|
2026-03-02 10:54:28 +01:00
|
|
|
<ZapOff className="w-3.5 h-3.5 text-base-500" />
|
2026-03-02 10:13:50 +01:00
|
|
|
)}
|
|
|
|
|
<span
|
2026-03-02 10:54:28 +01:00
|
|
|
className={`text-[10px] font-mono font-medium ${
|
|
|
|
|
data.connected ? "text-mint" : "text-cherry"
|
2026-03-02 10:13:50 +01:00
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{data.connected ? "Verbunden" : "Getrennt"}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Category filter tabs */}
|
|
|
|
|
{categories.length > 1 && (
|
2026-03-02 10:54:28 +01:00
|
|
|
<div className="flex gap-1 mb-4 flex-wrap">
|
2026-03-02 10:13:50 +01:00
|
|
|
<button
|
|
|
|
|
onClick={() => setFilter(null)}
|
2026-03-02 10:54:28 +01:00
|
|
|
className={`tab-btn ${filter === null ? "active" : ""}`}
|
2026-03-02 10:13:50 +01:00
|
|
|
>
|
|
|
|
|
Alle
|
|
|
|
|
</button>
|
|
|
|
|
{categories.map((cat) => (
|
|
|
|
|
<button
|
|
|
|
|
key={cat}
|
|
|
|
|
onClick={() => setFilter(filter === cat ? null : cat)}
|
2026-03-02 10:54:28 +01:00
|
|
|
className={`tab-btn ${filter === cat ? "active" : ""} flex items-center gap-1.5`}
|
2026-03-02 10:13:50 +01:00
|
|
|
>
|
|
|
|
|
{cat}
|
2026-03-02 10:54:28 +01:00
|
|
|
<span
|
|
|
|
|
className={`text-[10px] font-bold ${
|
|
|
|
|
filter === cat ? "text-gold" : "text-base-500"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{grouped[cat].length}
|
|
|
|
|
</span>
|
2026-03-02 10:13:50 +01:00
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Entity list */}
|
|
|
|
|
{data.entities.length === 0 ? (
|
2026-03-02 10:54:28 +01:00
|
|
|
<div className="flex flex-col items-center justify-center py-8 text-base-500">
|
|
|
|
|
<Radio className="w-8 h-8 mb-2 opacity-20" />
|
|
|
|
|
<p className="text-xs font-mono">
|
2026-03-02 10:13:50 +01:00
|
|
|
{data.connected
|
2026-03-02 10:54:28 +01:00
|
|
|
? "Warte auf Nachrichten..."
|
|
|
|
|
: "Nicht konfiguriert"}
|
2026-03-02 10:13:50 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-03-02 10:54:28 +01:00
|
|
|
<div className="space-y-px max-h-80 overflow-y-auto">
|
2026-03-02 10:13:50 +01:00
|
|
|
{shown.map((entity) => (
|
|
|
|
|
<EntityRow key={entity.topic} entity={entity} />
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Expand/collapse */}
|
|
|
|
|
{filtered.length > 8 && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
2026-03-02 10:54:28 +01:00
|
|
|
className="mt-3 w-full flex items-center justify-center gap-1.5 text-[10px] font-mono text-base-500 hover:text-base-700 transition-colors py-1.5"
|
2026-03-02 10:13:50 +01:00
|
|
|
>
|
|
|
|
|
{expanded ? (
|
|
|
|
|
<>
|
|
|
|
|
<ChevronUp className="w-3 h-3" />
|
|
|
|
|
Weniger anzeigen
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<ChevronDown className="w-3 h-3" />
|
|
|
|
|
Alle {filtered.length} anzeigen
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function EntityRow({ entity }: { entity: MqttEntity }) {
|
2026-03-02 10:54:28 +01:00
|
|
|
const age = Math.round(Date.now() / 1000 - entity.timestamp);
|
2026-03-02 10:13:50 +01:00
|
|
|
const ageStr =
|
|
|
|
|
age < 60
|
|
|
|
|
? `${age}s`
|
|
|
|
|
: age < 3600
|
|
|
|
|
? `${Math.floor(age / 60)}m`
|
|
|
|
|
: `${Math.floor(age / 3600)}h`;
|
|
|
|
|
|
|
|
|
|
const displayValue = formatValue(entity.value);
|
|
|
|
|
const isNumeric = typeof entity.value === "number";
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-02 10:54:28 +01:00
|
|
|
<div className="flex items-center justify-between gap-3 px-3 py-2 bg-base-100 border-l-2 border-base-300 hover:border-iris transition-colors group">
|
2026-03-02 10:13:50 +01:00
|
|
|
<div className="flex-1 min-w-0">
|
2026-03-02 10:54:28 +01:00
|
|
|
<p className="text-xs text-base-700 truncate">{entity.name}</p>
|
|
|
|
|
<p className="text-[9px] text-base-500 truncate font-mono mt-0.5">
|
|
|
|
|
{entity.topic}
|
|
|
|
|
</p>
|
2026-03-02 10:13:50 +01:00
|
|
|
</div>
|
2026-03-02 10:54:28 +01:00
|
|
|
|
2026-03-02 10:13:50 +01:00
|
|
|
<div className="flex items-center gap-2 shrink-0">
|
|
|
|
|
<span
|
2026-03-02 10:54:28 +01:00
|
|
|
className={`text-sm font-mono font-medium ${
|
|
|
|
|
isNumeric ? "text-iris data-value" : "text-base-800"
|
2026-03-02 10:13:50 +01:00
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{displayValue}
|
|
|
|
|
</span>
|
2026-03-02 10:54:28 +01:00
|
|
|
{(entity as any).unit && (
|
|
|
|
|
<span className="text-[10px] font-mono text-base-500">
|
|
|
|
|
{(entity as any).unit}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
<span className="text-[9px] font-mono text-base-500 w-6 text-right opacity-50">
|
|
|
|
|
{ageStr}
|
|
|
|
|
</span>
|
2026-03-02 10:13:50 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatValue(value: any): string {
|
2026-03-02 10:54:28 +01:00
|
|
|
if (value === null || value === undefined) return "\u2014";
|
2026-03-02 10:13:50 +01:00
|
|
|
if (typeof value === "boolean") return value ? "ON" : "OFF";
|
|
|
|
|
if (typeof value === "number") {
|
|
|
|
|
return Number.isInteger(value) ? value.toString() : value.toFixed(1);
|
|
|
|
|
}
|
|
|
|
|
if (typeof value === "object") {
|
|
|
|
|
return JSON.stringify(value).slice(0, 40);
|
|
|
|
|
}
|
|
|
|
|
const str = String(value);
|
|
|
|
|
if (str === "on" || str === "ON") return "ON";
|
|
|
|
|
if (str === "off" || str === "OFF") return "OFF";
|
|
|
|
|
if (str === "online") return "Online";
|
|
|
|
|
if (str === "offline") return "Offline";
|
2026-03-02 10:54:28 +01:00
|
|
|
return str.length > 30 ? str.slice(0, 30) + "\u2026" : str;
|
2026-03-02 10:13:50 +01:00
|
|
|
}
|