Spaces:
Running
Running
File size: 24,235 Bytes
f287aeb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | import { useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
import { Button } from "./ui/button";
import { Badge } from "./ui/badge";
import { Switch } from "./ui/switch";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./ui/tabs";
import {
Bell,
AlertTriangle,
Info,
Check,
X,
Mail,
MessageSquare,
Smartphone,
} from "lucide-react";
import { User } from "../types";
interface NotificationCenterProps {
user: User;
notifications: any[];
onUpdateNotifications: (notifications: any[]) => void;
}
export default function NotificationCenter({
user,
notifications,
onUpdateNotifications,
}: NotificationCenterProps) {
const [filter, setFilter] = useState<"all" | "unread" | "critical">("all");
// Notification preferences
const [emailEnabled, setEmailEnabled] = useState(true);
const [smsEnabled, setSmsEnabled] = useState(true);
const [whatsappEnabled, setWhatsappEnabled] = useState(false);
const [criticalAlerts, setCriticalAlerts] = useState(true);
const [inventoryAlerts, setInventoryAlerts] = useState(true);
const [healthAlerts, setHealthAlerts] = useState(true);
const filteredNotifications = notifications.filter((notif) => {
if (filter === "unread") return !notif.read;
if (filter === "critical") return notif.priority === "critical";
return true;
});
const unreadCount = notifications.filter((n) => !n.read).length;
const criticalCount = notifications.filter(
(n) => n.priority === "critical" && !n.read,
).length;
const handleMarkAsRead = (id: string) => {
onUpdateNotifications(
notifications.map((n) => (n.id === id ? { ...n, read: true } : n)),
);
};
const handleMarkAllAsRead = () => {
onUpdateNotifications(notifications.map((n) => ({ ...n, read: true })));
};
const handleDelete = (id: string) => {
onUpdateNotifications(notifications.filter((n) => n.id !== id));
};
const getNotificationIcon = (type: string) => {
switch (type) {
case "alert":
case "WARNING":
case "warning":
return AlertTriangle;
case "CRITICAL":
case "critical":
return AlertTriangle;
case "SUCCESS":
case "success":
return Check;
case "INFO":
case "info":
return Info;
default:
return Bell;
}
};
const getNotificationColor = (type: string) => {
const t = type?.toUpperCase();
switch (t) {
case "CRITICAL":
return "bg-red-50 border-red-200";
case "WARNING":
return "bg-yellow-50 border-yellow-200";
case "SUCCESS":
return "bg-green-50 border-green-200";
case "INFO":
case "NORMAL":
return "bg-blue-50 border-blue-200";
default:
return "bg-gray-50 border-gray-200";
}
};
const getPriorityBadgeColor = (priority: string) => {
switch (priority) {
case "critical":
return "bg-red-100 text-red-800";
case "high":
return "bg-orange-100 text-orange-800";
case "medium":
return "bg-yellow-100 text-yellow-800";
case "low":
return "bg-blue-100 text-blue-800";
default:
return "bg-gray-100 text-gray-800";
}
};
const parseTimestampValue = (value: unknown): Date | null => {
if (value instanceof Date) {
return Number.isNaN(value.getTime()) ? null : value;
}
if (typeof value === "number" && Number.isFinite(value)) {
const normalized = Math.abs(value) < 1e12 ? value * 1000 : value;
const date = new Date(normalized);
return Number.isNaN(date.getTime()) ? null : date;
}
if (typeof value === "string" && value.trim()) {
const trimmed = value.trim();
if (/^\d+(\.\d+)?$/.test(trimmed)) {
const numericValue = Number(trimmed);
if (Number.isFinite(numericValue)) {
const normalized =
Math.abs(numericValue) < 1e12 ? numericValue * 1000 : numericValue;
const date = new Date(normalized);
if (!Number.isNaN(date.getTime())) return date;
}
}
const date = new Date(trimmed);
return Number.isNaN(date.getTime()) ? null : date;
}
return null;
};
const getTimeAgo = (timestamp?: unknown) => {
const time = parseTimestampValue(timestamp);
if (!time) return "Just now";
const diffMs = Date.now() - time.getTime();
if (diffMs <= 0) return "Just now";
const diffInMinutes = Math.floor(diffMs / (1000 * 60));
if (diffInMinutes < 1) return "Just now";
if (diffInMinutes < 60) return `${diffInMinutes}m ago`;
const diffInHours = Math.floor(diffInMinutes / 60);
if (diffInHours < 24) return `${diffInHours}h ago`;
const diffInDays = Math.floor(diffInHours / 24);
if (diffInDays < 7) return `${diffInDays}d ago`;
return time.toLocaleDateString();
};
const getExactTime = (timestamp?: unknown) => {
const time = parseTimestampValue(timestamp);
return time ? time.toLocaleString() : "";
};
const isWaterChangeDone = (notification: any) =>
notification.type === "SUCCESS" ||
notification.type === "success" ||
notification.templateName === "WATER_CHANGE_DONE" ||
notification.subject?.toLowerCase().includes("water change completed") ||
notification.subject?.toLowerCase().includes("water change done");
const isFeedingCompleted = (notification: any) =>
notification.templateName === "FEEDING_DONE" ||
notification.templateName === "FEEDING_COMPLETED" ||
notification.subject?.toLowerCase().includes("feeding completed") ||
notification.subject?.toLowerCase().includes("feeding done");
const isHarvestFinished = (notification: any) =>
notification.templateName === "HARVEST_FINISHED" ||
notification.templateName === "HARVEST_DONE" ||
notification.subject?.toLowerCase().includes("harvest finished") ||
notification.subject?.toLowerCase().includes("harvest completed");
// Notifications styled as a green, "DONE" card
const isCompleted = (notification: any) =>
isWaterChangeDone(notification) ||
isFeedingCompleted(notification) ||
isHarvestFinished(notification);
const isWaterChangeTask = (notification: any) =>
!isCompleted(notification) &&
(notification.templateName === "WATER_CHANGE_TASK" ||
notification.subject?.toLowerCase().includes("water change task"));
const isCritical = (notification: any) =>
notification.priority === "critical" ||
notification.type?.toUpperCase() === "CRITICAL" ||
notification.subject?.toLowerCase().includes("critical");
const isHarvestReminder = (notification: any) =>
!isCritical(notification) &&
(notification.templateName === "HARVEST_REMINDER" ||
notification.subject?.toLowerCase().includes("harvest reminder"));
const isHarvestTask = (notification: any) =>
!isCompleted(notification) &&
(notification.templateName === "HARVEST_TASK" ||
notification.subject?.toLowerCase().includes("harvest task"));
// Notifications styled as a blue, badge-less info card
const isBlueInfo = (notification: any) =>
isWaterChangeTask(notification) ||
isHarvestReminder(notification) ||
isHarvestTask(notification);
return (
<div className="p-6 space-y-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl">Notification Center</h1>
<p className="text-gray-600">
Manage alerts and communication preferences
</p>
</div>
<Button onClick={handleMarkAllAsRead} variant="outline">
<Check className="w-4 h-4 mr-2" />
Mark All Read
</Button>
</div>
{/* Summary Cards */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2">
<CardTitle className="text-sm">Total Notifications</CardTitle>
<Bell className="w-4 h-4 text-blue-600" />
</CardHeader>
<CardContent>
<div className="text-2xl">{notifications.length}</div>
<p className="text-xs text-gray-600 mt-1">{unreadCount} unread</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2">
<CardTitle className="text-sm">Critical Alerts</CardTitle>
<AlertTriangle className="w-4 h-4 text-red-600" />
</CardHeader>
<CardContent>
<div className="text-2xl text-red-600">{criticalCount}</div>
<p className="text-xs text-gray-600 mt-1">Unread critical</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2">
<CardTitle className="text-sm">Active Channels</CardTitle>
<Mail className="w-4 h-4 text-green-600" />
</CardHeader>
<CardContent>
<div className="text-2xl">
{
[emailEnabled, smsEnabled, whatsappEnabled].filter(Boolean)
.length
}
</div>
<p className="text-xs text-gray-600 mt-1">Communication methods</p>
</CardContent>
</Card>
</div>
<Tabs defaultValue="notifications" className="space-y-4">
<TabsList>
<TabsTrigger value="notifications">
<Bell className="w-4 h-4 mr-2" />
Notifications
</TabsTrigger>
</TabsList>
{/* Notifications Tab */}
<TabsContent value="notifications" className="space-y-4">
<Card>
<CardHeader>
<div className="flex items-center gap-2">
<Button
variant={filter === "all" ? "default" : "outline"}
size="sm"
onClick={() => setFilter("all")}
>
All ({notifications.length})
</Button>
<Button
variant={filter === "unread" ? "default" : "outline"}
size="sm"
onClick={() => setFilter("unread")}
>
Unread ({unreadCount})
</Button>
<Button
variant={filter === "critical" ? "default" : "outline"}
size="sm"
onClick={() => setFilter("critical")}
>
Critical ({criticalCount})
</Button>
</div>
</CardHeader>
<CardContent>
<div className="space-y-2">
{filteredNotifications.map((notification) => {
const Icon = getNotificationIcon(notification.type);
const subjectText =
notification.subject ||
notification.title ||
"Notification";
const bodyText =
notification.message || notification.body || "";
return (
<div
key={notification.id}
className={`p-4 border rounded-lg transition-all ${!isCompleted(notification) && !isBlueInfo(notification) && !isCritical(notification) ? getNotificationColor(notification.priority) : ""} ${!notification.read ? "border-l-4" : ""}`}
style={
isCompleted(notification)
? {
backgroundColor: "#f0fdf4",
borderColor: "#bbf7d0",
}
: isBlueInfo(notification)
? {
backgroundColor: "#eff6ff",
borderColor: "#bfdbfe",
}
: isCritical(notification)
? {
backgroundColor: "#fee2e2",
borderColor: "#fca5a5",
}
: undefined
}
>
<div className="flex items-start gap-3">
<div
className="mt-0.5"
style={{
color: isCritical(notification)
? "#dc2626"
: isBlueInfo(notification)
? "#2563eb"
: notification.type?.toUpperCase() === "WARNING"
? "#ca8a04"
: notification.type?.toUpperCase() ===
"SUCCESS" || isCompleted(notification)
? "#16a34a"
: "#2563eb",
}}
>
{notification.type?.toUpperCase() === "SUCCESS" ||
isCompleted(notification) ? (
<Check className="w-5 h-5" />
) : isCritical(notification) ? (
<AlertTriangle className="w-5 h-5" />
) : isBlueInfo(notification) ? (
<Info className="w-5 h-5" />
) : notification.type?.toUpperCase() === "WARNING" ? (
<AlertTriangle className="w-5 h-5" />
) : (
<Info className="w-5 h-5" />
)}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-start justify-between mb-1">
<div className="flex items-center gap-2 flex-1">
<p
className={`text-sm ${!notification.read ? "font-medium" : ""}`}
>
{subjectText}
</p>
{isCompleted(notification) ? (
<Badge
className="bg-green-600 text-white border-green-600"
variant="outline"
>
DONE
</Badge>
) : isBlueInfo(notification) ? null : (
<Badge
className={getPriorityBadgeColor(
notification.priority,
)}
variant="outline"
>
{notification.priority}
</Badge>
)}
{!notification.read && (
<div className="w-2 h-2 bg-blue-600 rounded-full" />
)}
</div>
<span
className="text-xs text-gray-500 whitespace-nowrap ml-2"
title={getExactTime(notification.timestamp)}
>
{getTimeAgo(notification.timestamp)}
</span>
</div>
<p className="text-sm text-gray-700 mb-2">
{bodyText}
</p>
<div className="flex items-center gap-2">
{!notification.read && (
<Button
size="sm"
variant="outline"
onClick={() =>
handleMarkAsRead(notification.id)
}
>
<Check className="w-3 h-3 mr-1" />
Mark Read
</Button>
)}
<Button
size="sm"
variant="ghost"
onClick={() => handleDelete(notification.id)}
>
<X className="w-3 h-3 mr-1" />
Dismiss
</Button>
</div>
</div>
</div>
</div>
);
})}
{filteredNotifications.length === 0 && (
<div className="text-center py-12">
<Bell className="w-12 h-12 text-gray-400 mx-auto mb-3" />
<p className="text-gray-600">No notifications</p>
<p className="text-sm text-gray-500 mt-1">
{filter === "unread"
? "You're all caught up!"
: filter === "critical"
? "No critical alerts at this time"
: "You'll see notifications here"}
</p>
</div>
)}
</div>
</CardContent>
</Card>
</TabsContent>
{/* Preferences Tab */}
<TabsContent value="preferences" className="space-y-4">
<Card>
<CardHeader>
<CardTitle className="text-sm">Notification Channels</CardTitle>
<p className="text-xs text-gray-600">
Choose how you want to receive alerts
</p>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
<div className="flex items-center gap-3">
<Mail className="w-5 h-5 text-blue-600" />
<div>
<p className="text-sm">Email Notifications</p>
<p className="text-xs text-gray-600">{user.email}</p>
</div>
</div>
<Switch
checked={emailEnabled}
onCheckedChange={setEmailEnabled}
/>
</div>
<div className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
<div className="flex items-center gap-3">
<Smartphone className="w-5 h-5 text-green-600" />
<div>
<p className="text-sm">SMS Alerts</p>
<p className="text-xs text-gray-600">{user.phone}</p>
</div>
</div>
<Switch checked={smsEnabled} onCheckedChange={setSmsEnabled} />
</div>
<div className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
<div className="flex items-center gap-3">
<MessageSquare className="w-5 h-5 text-green-700" />
<div>
<p className="text-sm">WhatsApp</p>
<p className="text-xs text-gray-600">{user.phone}</p>
</div>
</div>
<Switch
checked={whatsappEnabled}
onCheckedChange={setWhatsappEnabled}
/>
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-sm">Alert Types</CardTitle>
<p className="text-xs text-gray-600">
Customize which alerts you want to receive
</p>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-3">
<AlertTriangle className="w-5 h-5 text-red-600" />
<div>
<p className="text-sm">Critical System Alerts</p>
<p className="text-xs text-gray-600">
Water quality issues, equipment failures
</p>
</div>
</div>
<Switch
checked={criticalAlerts}
onCheckedChange={setCriticalAlerts}
/>
</div>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-3">
<Bell className="w-5 h-5 text-orange-600" />
<div>
<p className="text-sm">Inventory Alerts</p>
<p className="text-xs text-gray-600">
Low stock, expiring items
</p>
</div>
</div>
<Switch
checked={inventoryAlerts}
onCheckedChange={setInventoryAlerts}
/>
</div>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-3">
<AlertTriangle className="w-5 h-5 text-purple-600" />
<div>
<p className="text-sm">Health & Disease Alerts</p>
<p className="text-xs text-gray-600">
Disease detection, mortality spikes
</p>
</div>
</div>
<Switch
checked={healthAlerts}
onCheckedChange={setHealthAlerts}
/>
</div>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-3">
<Info className="w-5 h-5 text-blue-600" />
<div>
<p className="text-sm">Daily Reports</p>
<p className="text-xs text-gray-600">
Performance summaries, growth updates
</p>
</div>
</div>
<Switch defaultChecked />
</div>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-3">
<Info className="w-5 h-5 text-green-600" />
<div>
<p className="text-sm">Scheduled Tasks</p>
<p className="text-xs text-gray-600">
Feeding reminders, sampling schedules
</p>
</div>
</div>
<Switch defaultChecked />
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-sm">Quiet Hours</CardTitle>
<p className="text-xs text-gray-600">
Set times when non-critical notifications are paused
</p>
</CardHeader>
<CardContent>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div>
<p className="text-sm">Enable Quiet Hours</p>
<p className="text-xs text-gray-600">10:00 PM - 7:00 AM</p>
</div>
<Switch />
</div>
<p className="text-xs text-gray-600 mt-3">
Note: Critical alerts will always be delivered immediately
</p>
</CardContent>
</Card>
<div className="flex justify-end">
<Button>Save Preferences</Button>
</div>
</TabsContent>
</Tabs>
</div>
);
}
|