97 lines
4.0 KiB
TypeScript
97 lines
4.0 KiB
TypeScript
import React from 'react';
|
|
import { IotDevice, IotDeviceType } from '../../types.ts';
|
|
import { formatDistanceToNow } from '../../utils/time.ts';
|
|
|
|
interface IotDeviceCardProps {
|
|
device: IotDevice;
|
|
onEdit: () => void;
|
|
onDelete: () => void;
|
|
fullLocationName: string;
|
|
}
|
|
|
|
const deviceConfig: { [key in IotDeviceType]: { icon: string; color: string } } = {
|
|
[IotDeviceType.TEMPERATURE_SENSOR]: { icon: 'fa-temperature-half', color: 'text-orange-500' },
|
|
[IotDeviceType.HUMIDITY_SENSOR]: { icon: 'fa-droplet', color: 'text-blue-500' },
|
|
[IotDeviceType.AMMONIA_SENSOR]: { icon: 'fa-wind', color: 'text-purple-500' },
|
|
[IotDeviceType.CO2_SENSOR]: { icon: 'fa-smog', color: 'text-gray-500' },
|
|
[IotDeviceType.DUST_SENSOR]: { icon: 'fa-lungs-virus', color: 'text-slate-500' },
|
|
[IotDeviceType.LIGHT_SENSOR]: { icon: 'fa-sun', color: 'text-yellow-500' },
|
|
[IotDeviceType.WATER_FLOW_SENSOR]: { icon: 'fa-faucet', color: 'text-cyan-500' },
|
|
[IotDeviceType.FEED_LEVEL_SENSOR]: { icon: 'fa-cubes-stacked', color: 'text-amber-600' },
|
|
[IotDeviceType.WEIGHT_SCALE]: { icon: 'fa-weight-scale', color: 'text-indigo-500' },
|
|
[IotDeviceType.IOT_GATEWAY]: { icon: 'fa-server', color: 'text-green-600' },
|
|
};
|
|
|
|
const statusConfig: { [key in IotDevice['status']]: { text: string; bg: string; pulse: boolean } } =
|
|
{
|
|
online: { text: 'Online', bg: 'bg-green-500', pulse: true },
|
|
offline: { text: 'Offline', bg: 'bg-gray-500', pulse: false },
|
|
error: { text: 'Error', bg: 'bg-red-500', pulse: false },
|
|
};
|
|
|
|
const IotDeviceCard: React.FC<IotDeviceCardProps> = ({
|
|
device,
|
|
onEdit,
|
|
onDelete,
|
|
fullLocationName,
|
|
}) => {
|
|
const config = deviceConfig[device.type];
|
|
const status = statusConfig[device.status];
|
|
|
|
return (
|
|
<div className="bg-white rounded-xl shadow-sm transition-all duration-300 hover:shadow-lg flex flex-col border border-gray-200">
|
|
<div className="p-4 flex-grow">
|
|
<div className="flex justify-between items-start">
|
|
<i
|
|
className={`fa-solid ${config.icon} text-3xl ${config.color} p-3 bg-gray-100 rounded-lg`}
|
|
></i>
|
|
<span
|
|
className={`flex items-center text-xs font-semibold text-white px-2 py-0.5 rounded-full ${status.bg}`}
|
|
>
|
|
{status.pulse && (
|
|
<span className="w-2 h-2 rounded-full mr-1.5 bg-white/70 animate-pulse"></span>
|
|
)}
|
|
{status.text}
|
|
</span>
|
|
</div>
|
|
<h3 className="font-bold text-lg text-gray-800 truncate mt-3" title={device.name}>
|
|
{device.name}
|
|
</h3>
|
|
<p className="text-sm text-gray-500">{device.type}</p>
|
|
<div className="mt-4 border-t pt-3 space-y-2 text-sm text-gray-600">
|
|
<p title={fullLocationName}>
|
|
<i className="fa-solid fa-location-dot w-5 text-gray-400"></i>{' '}
|
|
<span className="truncate">{fullLocationName}</span>
|
|
</p>
|
|
<p>
|
|
<i className="fa-solid fa-clock w-5 text-gray-400"></i>{' '}
|
|
{formatDistanceToNow(device.lastHeartbeat)}
|
|
</p>
|
|
<p>
|
|
<i className="fa-solid fa-arrow-down-up-across-line w-5 text-gray-400"></i>{' '}
|
|
<span className="font-semibold text-gray-800">{device.currentValue}</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className="p-3 bg-gray-50 border-t border-gray-100 rounded-b-xl flex items-center justify-end space-x-2">
|
|
<button
|
|
onClick={onEdit}
|
|
className="text-sm text-gray-600 hover:text-blue-600 font-medium px-3 py-1 rounded-md hover:bg-gray-200 transition-colors"
|
|
aria-label={`Edit ${device.name}`}
|
|
>
|
|
<i className="fa-solid fa-pencil mr-1"></i> Edit
|
|
</button>
|
|
<button
|
|
onClick={onDelete}
|
|
className="text-sm text-gray-600 hover:text-red-600 font-medium px-3 py-1 rounded-md hover:bg-gray-200 transition-colors"
|
|
aria-label={`Delete ${device.name}`}
|
|
>
|
|
<i className="fa-solid fa-trash mr-1"></i> Hapus
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default IotDeviceCard;
|