File size: 12,870 Bytes
0f226f2
 
 
 
 
 
 
 
 
7782325
0f226f2
 
7782325
 
 
 
 
 
 
 
 
 
 
0f226f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7782325
0f226f2
 
 
 
7782325
0f226f2
 
 
7782325
0f226f2
 
 
 
 
 
 
 
 
7782325
 
0f226f2
 
 
7782325
0f226f2
7782325
0f226f2
 
 
7782325
 
 
 
 
 
 
 
 
0f226f2
 
 
7782325
 
 
0f226f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7782325
 
 
0f226f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7782325
 
 
 
 
 
 
 
0f226f2
 
7782325
 
 
 
0f226f2
7782325
 
 
 
0f226f2
 
 
 
 
 
 
7782325
 
 
 
 
0f226f2
 
 
7782325
 
 
 
 
0f226f2
 
 
 
7782325
 
 
 
 
0f226f2
 
 
 
 
 
7782325
 
 
 
0f226f2
 
 
 
 
7782325
 
 
 
 
0f226f2
7782325
0f226f2
 
 
 
 
 
 
 
 
 
 
7782325
0f226f2
 
 
 
 
 
 
7782325
 
0f226f2
 
 
7782325
 
 
 
 
 
 
0f226f2
 
 
 
 
7782325
 
 
 
 
 
 
0f226f2
 
 
 
 
 
 
 
 
 
7782325
 
0f226f2
 
 
 
 
 
 
 
 
 
 
 
 
7782325
 
 
 
 
0f226f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState, useEffect, useCallback } from "react";
import { motion, AnimatePresence } from "framer-motion";
import {
  Shield, TrendingUp, AlertTriangle, Sparkles,
  DollarSign, X, CheckCheck, Bell, RefreshCw
} from "lucide-react";
import { notificationsApi, NotificationItem } from "@/lib/api";
import { useThemeStore } from "@/lib/stores/themeStore";

// ─── Type config ──────────────────────────────────────────────────────────────
const typeConfig: Record<string, {
  icon: React.ElementType;
  color: string;
  darkBg: string;
  lightBg: string;
}> = {
  alert:   { icon: Shield,        color: "text-red-500",     darkBg: "bg-red-500/10 border-red-500/20",        lightBg: "bg-red-50 border-red-200/70" },
  insight: { icon: Sparkles,      color: "text-blue-500",    darkBg: "bg-blue-500/10 border-blue-500/20",      lightBg: "bg-blue-50 border-blue-200/70" },
  warning: { icon: AlertTriangle, color: "text-amber-500",   darkBg: "bg-amber-500/10 border-amber-500/20",    lightBg: "bg-amber-50 border-amber-200/70" },
  success: { icon: TrendingUp,    color: "text-emerald-500", darkBg: "bg-emerald-500/10 border-emerald-500/20", lightBg: "bg-emerald-50 border-emerald-200/70" },
  default: { icon: DollarSign,    color: "text-slate-500",   darkBg: "bg-zinc-500/10 border-zinc-500/20",      lightBg: "bg-slate-50 border-slate-200/70" },
};

function getTypeConfig(type: string) {
  return typeConfig[type] || typeConfig.default;
}

function timeAgo(dateStr: string): string {
  const diff = Date.now() - new Date(dateStr).getTime();
  const mins = Math.floor(diff / 60000);
  if (mins < 1) return "Just now";
  if (mins < 60) return `${mins}m ago`;
  const hrs = Math.floor(mins / 60);
  if (hrs < 24) return `${hrs}h ago`;
  return `${Math.floor(hrs / 24)}d ago`;
}

// ─── Notification Item ────────────────────────────────────────────────────────
function NotifItem({
  notification,
  onDismiss,
  onRead,
  isLight,
}: {
  notification: NotificationItem;
  onDismiss: (id: string) => void;
  onRead: (id: string) => void;
  isLight: boolean;
}) {
  const config = getTypeConfig(notification.type);
  const Icon = config.icon;
  const bgClass = isLight ? config.lightBg : config.darkBg;

  return (
    <motion.div
      layout
      initial={{ opacity: 0, x: 20 }}
      animate={{ opacity: 1, x: 0 }}
      exit={{ opacity: 0, x: 20, height: 0, marginBottom: 0 }}
      transition={{ duration: 0.25 }}
      onClick={() => onRead(notification.id)}
      className={`relative flex gap-3 rounded-xl border p-3 cursor-pointer transition-all group ${bgClass} ${
        !notification.read ? "ring-1 ring-inset" : "opacity-60"
      }`}
    >
      {!notification.read && (
        <div className="absolute top-3 right-3 h-1.5 w-1.5 rounded-full bg-blue-500" />
      )}
      <div className={`flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-lg border ${bgClass}`}>
        <Icon className={`h-4 w-4 ${config.color}`} />
      </div>
      <div className="flex-1 min-w-0 pr-4">
        <p className="text-xs font-semibold leading-tight" style={{ color: "var(--fg)" }}>
          {notification.title}
        </p>
        <p className="text-xs mt-0.5 leading-relaxed line-clamp-2" style={{ color: "var(--fg-muted)" }}>
          {notification.message}
        </p>
        <p className="text-xs mt-1" style={{ color: "var(--fg-subtle)" }}>
          {timeAgo(notification.created_at)}
        </p>
      </div>
      <button
        onClick={(e) => { e.stopPropagation(); onDismiss(notification.id); }}
        className={`absolute top-2 right-2 p-0.5 rounded transition-colors opacity-0 group-hover:opacity-100 ${
          isLight ? "text-slate-400 hover:text-slate-600" : "text-zinc-600 hover:text-zinc-400"
        }`}
      >
        <X className="h-3 w-3" />
      </button>
    </motion.div>
  );
}

// ─── Notification Panel ───────────────────────────────────────────────────────
export function NotificationPanel({
  isOpen,
  onClose,
}: {
  isOpen: boolean;
  onClose: () => void;
}) {
  const { theme } = useThemeStore();
  const isLight = theme === "light";

  const [notifications, setNotifications] = useState<NotificationItem[]>([]);
  const [unreadCount, setUnreadCount] = useState(0);
  const [filter, setFilter] = useState<"all" | "unread">("all");
  const [loading, setLoading] = useState(false);

  const loadNotifications = useCallback(async () => {
    setLoading(true);
    try {
      const res = await notificationsApi.list();
      setNotifications(res.notifications);
      setUnreadCount(res.unread_count);
    } catch {
      // backend offline β€” keep empty state
    } finally {
      setLoading(false);
    }
  }, []);

  useEffect(() => {
    if (isOpen) loadNotifications();
  }, [isOpen, loadNotifications]);

  const dismiss = async (id: string) => {
    setNotifications((prev) => prev.filter((n) => n.id !== id));
    try { await notificationsApi.dismiss(id); } catch { /* ignore */ }
  };

  const markRead = async (id: string) => {
    setNotifications((prev) => prev.map((n) => n.id === id ? { ...n, read: true } : n));
    setUnreadCount((c) => Math.max(0, c - 1));
    try { await notificationsApi.markRead(id); } catch { /* ignore */ }
  };

  const markAllRead = async () => {
    setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
    setUnreadCount(0);
    try { await notificationsApi.markAllRead(); } catch { /* ignore */ }
  };

  const displayed = filter === "unread" ? notifications.filter((n) => !n.read) : notifications;

  return (
    <AnimatePresence>
      {isOpen && (
        <>
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            onClick={onClose}
            className="fixed inset-0 z-40"
          />
          <motion.div
            initial={{ opacity: 0, y: -10, scale: 0.97 }}
            animate={{ opacity: 1, y: 0, scale: 1 }}
            exit={{ opacity: 0, y: -10, scale: 0.97 }}
            transition={{ duration: 0.2, ease: "easeOut" }}
            className="absolute right-0 top-full mt-2 z-50 w-80 rounded-2xl border backdrop-blur-2xl overflow-hidden"
            style={{
              background: isLight ? "rgba(255,255,255,0.97)" : "rgba(9,9,11,0.97)",
              borderColor: "var(--border-strong)",
              boxShadow: isLight
                ? "0 8px 40px rgba(15,23,42,0.14), 0 2px 8px rgba(15,23,42,0.08)"
                : "0 8px 40px rgba(0,0,0,0.6)",
            }}
          >
            {/* Header */}
            <div
              className="flex items-center justify-between border-b px-4 py-3"
              style={{ borderColor: "var(--border)" }}
            >
              <div className="flex items-center gap-2">
                <Bell className="h-4 w-4" style={{ color: "var(--fg-muted)" }} />
                <span className="text-sm font-semibold" style={{ color: "var(--fg)" }}>
                  Notifications
                </span>
                {unreadCount > 0 && (
                  <span className="flex h-4 w-4 items-center justify-center rounded-full bg-blue-500 text-[10px] font-bold text-white">
                    {unreadCount}
                  </span>
                )}
              </div>
              <div className="flex items-center gap-2">
                <button
                  onClick={loadNotifications}
                  className="transition-colors hover:text-emerald-500"
                  style={{ color: "var(--fg-subtle)" }}
                >
                  <RefreshCw className={`h-3.5 w-3.5 ${loading ? "animate-spin" : ""}`} />
                </button>
                {unreadCount > 0 && (
                  <button
                    onClick={markAllRead}
                    className="flex items-center gap-1 text-xs transition-colors hover:text-emerald-500"
                    style={{ color: "var(--fg-subtle)" }}
                  >
                    <CheckCheck className="h-3 w-3" />
                    All read
                  </button>
                )}
                <button
                  onClick={onClose}
                  className="transition-colors hover:text-red-500"
                  style={{ color: "var(--fg-subtle)" }}
                >
                  <X className="h-4 w-4" />
                </button>
              </div>
            </div>

            {/* Filter tabs */}
            <div
              className="flex gap-1 border-b px-4 py-2"
              style={{ borderColor: "var(--border)" }}
            >
              {(["all", "unread"] as const).map((f) => (
                <button
                  key={f}
                  onClick={() => setFilter(f)}
                  className={`rounded-lg px-3 py-1 text-xs font-medium capitalize transition-all ${
                    filter === f
                      ? isLight
                        ? "bg-slate-100 text-slate-800"
                        : "bg-white/10 text-white"
                      : ""
                  }`}
                  style={filter !== f ? { color: "var(--fg-subtle)" } : undefined}
                >
                  {f} {f === "unread" && unreadCount > 0 && `(${unreadCount})`}
                </button>
              ))}
            </div>

            {/* List */}
            <div className="max-h-96 overflow-y-auto p-3 space-y-2">
              <AnimatePresence mode="popLayout">
                {loading ? (
                  <div className="flex items-center justify-center py-8">
                    <RefreshCw className="h-5 w-5 animate-spin" style={{ color: "var(--fg-subtle)" }} />
                  </div>
                ) : displayed.length === 0 ? (
                  <motion.div
                    initial={{ opacity: 0 }}
                    animate={{ opacity: 1 }}
                    className="flex flex-col items-center gap-2 py-8 text-center"
                  >
                    <Bell className="h-8 w-8" style={{ color: "var(--fg-subtle)" }} />
                    <p className="text-sm" style={{ color: "var(--fg-muted)" }}>No notifications</p>
                  </motion.div>
                ) : (
                  displayed.map((n) => (
                    <NotifItem
                      key={n.id}
                      notification={n}
                      onDismiss={dismiss}
                      onRead={markRead}
                      isLight={isLight}
                    />
                  ))
                )}
              </AnimatePresence>
            </div>

            <div
              className="border-t px-4 py-2.5"
              style={{ borderColor: "var(--border)" }}
            >
              <p className="text-center text-xs" style={{ color: "var(--fg-subtle)" }}>
                Real-time Β· AI-powered Β· Encrypted
              </p>
            </div>
          </motion.div>
        </>
      )}
    </AnimatePresence>
  );
}

// ─── Notification Bell ────────────────────────────────────────────────────────
export function NotificationBell() {
  const { theme } = useThemeStore();
  const isLight = theme === "light";
  const [isOpen, setIsOpen] = useState(false);
  const [unreadCount, setUnreadCount] = useState(0);

  useEffect(() => {
    notificationsApi.list().then((res) => setUnreadCount(res.unread_count)).catch(() => {});
  }, []);

  return (
    <div className="relative">
      <motion.button
        whileHover={{ scale: 1.05 }}
        whileTap={{ scale: 0.95 }}
        onClick={() => setIsOpen((v) => !v)}
        className={`relative rounded-xl p-2.5 transition-all duration-200 focus:outline-none ${
          isLight
            ? "bg-slate-100 text-slate-500 hover:bg-slate-200 hover:text-slate-800"
            : "bg-white/5 text-zinc-400 hover:text-white hover:bg-white/10"
        }`}
      >
        <Bell className="h-5 w-5" aria-hidden="true" />
        {unreadCount > 0 && (
          <motion.span
            initial={{ scale: 0 }}
            animate={{ scale: 1 }}
            className="absolute -top-0.5 -right-0.5 flex h-4 w-4 items-center justify-center rounded-full bg-red-500 text-[9px] font-bold text-white"
          >
            {unreadCount > 9 ? "9+" : unreadCount}
          </motion.span>
        )}
      </motion.button>
      <NotificationPanel isOpen={isOpen} onClose={() => setIsOpen(false)} />
    </div>
  );
}