File size: 10,954 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from 'react';
import { 
  Bell, BellOff, Check, CheckCheck, Trash2, Volume2, VolumeX, 
  Monitor, AlertTriangle, AlertCircle, Info, Settings, ShieldCheck, ShieldX
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Switch } from '@/components/ui/switch';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';
import {
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
} from '@/components/ui/tabs';
import { ScrollArea } from '@/components/ui/scroll-area';
import { useNotifications, NotificationSeverity, requestNotificationPermission, getNotificationPermission } from '@/contexts/NotificationContext';
import { cn } from '@/lib/utils';

const severityConfig: Record<NotificationSeverity, { icon: any; color: string }> = {
  critical: { icon: AlertTriangle, color: 'text-destructive' },
  warning: { icon: AlertCircle, color: 'text-orange-400' },
  info: { icon: Info, color: 'text-primary' },
};

const NotificationPermissionStatus = () => {
  const [permission, setPermission] = useState<NotificationPermission | 'unsupported'>('default');
  
  useEffect(() => {
    setPermission(getNotificationPermission());
  }, []);

  if (permission === 'unsupported') {
    return (
      <div className="flex items-center gap-2 text-xs text-muted-foreground">
        <ShieldX className="w-3 h-3" />
        <span>Browser understøtter ikke notifikationer</span>
      </div>
    );
  }

  if (permission === 'denied') {
    return (
      <div className="flex items-center gap-2 text-xs text-destructive">
        <ShieldX className="w-3 h-3" />
        <span>Notifikationer blokeret - tillad i browser-indstillinger</span>
      </div>
    );
  }

  if (permission === 'granted') {
    return (
      <div className="flex items-center gap-2 text-xs text-green-500">
        <ShieldCheck className="w-3 h-3" />
        <span>Notifikationer aktiveret</span>
      </div>
    );
  }

  return null;
};

const NotificationCenter = () => {
  const { 
    notifications, 
    unreadCount, 
    settings, 
    markAsRead, 
    markAllAsRead, 
    clearAll,
    updateSettings,
    addNotification
  } = useNotifications();
  const [activeTab, setActiveTab] = useState('notifications');

  const sendTestNotification = (severity: NotificationSeverity) => {
    const testAlerts: Record<NotificationSeverity, { title: string; message: string }> = {
      critical: {
        title: 'KRITISK: Ransomware Detekteret',
        message: 'Mistænkelig krypteringsaktivitet opdaget på endpoint WKS-2847. Øjeblikkelig handling påkrævet.',
      },
      warning: {
        title: 'Advarsel: Brute Force Forsøg',
        message: 'Flere mislykkede login-forsøg fra IP 192.168.1.105 på admin-konto.',
      },
      info: {
        title: 'System Opdatering',
        message: 'Sikkerhedspatch KB5034441 er tilgængelig til installation.',
      },
    };

    const alert = testAlerts[severity];
    addNotification({
      title: alert.title,
      message: alert.message,
      severity,
      source: 'Test',
    });
  };

  const formatTime = (timestamp: number) => {
    const diff = Date.now() - timestamp;
    const minutes = Math.floor(diff / 60000);
    const hours = Math.floor(diff / 3600000);
    const days = Math.floor(diff / 86400000);

    if (minutes < 1) return 'Lige nu';
    if (minutes < 60) return `${minutes}m siden`;
    if (hours < 24) return `${hours}t siden`;
    return `${days}d siden`;
  };

  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button variant="ghost" size="sm" className="relative">
          <Bell className="w-5 h-5" />
          {unreadCount > 0 && (
            <span className="absolute -top-1 -right-1 w-5 h-5 bg-destructive text-destructive-foreground text-xs rounded-full flex items-center justify-center animate-pulse">
              {unreadCount > 9 ? '9+' : unreadCount}
            </span>
          )}
        </Button>
      </PopoverTrigger>
      <PopoverContent align="end" className="w-80 p-0 bg-card border-border">
        <Tabs value={activeTab} onValueChange={setActiveTab}>
          <div className="flex items-center justify-between px-4 py-2 border-b border-border">
            <TabsList className="grid grid-cols-2 h-8 bg-secondary/30">
              <TabsTrigger value="notifications" className="text-xs">
                Notifikationer
              </TabsTrigger>
              <TabsTrigger value="settings" className="text-xs">
                Indstillinger
              </TabsTrigger>
            </TabsList>
          </div>

          <TabsContent value="notifications" className="m-0">
            {notifications.length > 0 && (
              <div className="flex items-center justify-between px-4 py-2 border-b border-border/50">
                <Button variant="ghost" size="sm" onClick={markAllAsRead} className="h-7 text-xs">
                  <CheckCheck className="w-3 h-3 mr-1" />
                  Marker alle læst
                </Button>
                <Button variant="ghost" size="sm" onClick={clearAll} className="h-7 text-xs text-destructive hover:text-destructive">
                  <Trash2 className="w-3 h-3 mr-1" />
                  Ryd
                </Button>
              </div>
            )}

            <ScrollArea className="h-[300px]">
              {notifications.length === 0 ? (
                <div className="flex flex-col items-center justify-center h-[200px] text-muted-foreground">
                  <BellOff className="w-10 h-10 mb-2 opacity-50" />
                  <p className="text-sm">Ingen notifikationer</p>
                </div>
              ) : (
                <div className="divide-y divide-border/50">
                  {notifications.map((notification) => {
                    const config = severityConfig[notification.severity];
                    const Icon = config.icon;
                    
                    return (
                      <div
                        key={notification.id}
                        className={cn(
                          "px-4 py-3 hover:bg-secondary/30 transition-colors cursor-pointer",
                          !notification.read && "bg-primary/5"
                        )}
                        onClick={() => markAsRead(notification.id)}
                      >
                        <div className="flex items-start gap-3">
                          <Icon className={cn("w-4 h-4 mt-0.5 shrink-0", config.color)} />
                          <div className="flex-1 min-w-0">
                            <div className="flex items-center justify-between gap-2">
                              <p className="text-sm font-medium truncate">{notification.title}</p>
                              {!notification.read && (
                                <span className="w-2 h-2 rounded-full bg-primary shrink-0" />
                              )}
                            </div>
                            <p className="text-xs text-muted-foreground line-clamp-2 mt-0.5">
                              {notification.message}
                            </p>
                            <p className="text-xs text-muted-foreground/60 mt-1">
                              {formatTime(notification.timestamp)}
                            </p>
                          </div>
                        </div>
                      </div>
                    );
                  })}
                </div>
              )}
            </ScrollArea>
          </TabsContent>

          <TabsContent value="settings" className="m-0 p-4 space-y-4">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                {settings.soundEnabled ? <Volume2 className="w-4 h-4" /> : <VolumeX className="w-4 h-4" />}
                <span className="text-sm">Lyd</span>
              </div>
              <Switch
                checked={settings.soundEnabled}
                onCheckedChange={(v) => updateSettings({ soundEnabled: v })}
              />
            </div>
            
            <div className="space-y-2">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <Monitor className="w-4 h-4" />
                  <span className="text-sm">Desktop notifikationer</span>
                </div>
                <Switch
                  checked={settings.desktopEnabled}
                  onCheckedChange={async (v) => {
                    if (v) {
                      const permission = await requestNotificationPermission();
                      if (permission === 'granted') {
                        updateSettings({ desktopEnabled: true });
                      }
                    } else {
                      updateSettings({ desktopEnabled: false });
                    }
                  }}
                />
              </div>
              <NotificationPermissionStatus />
            </div>
            
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <AlertTriangle className="w-4 h-4" />
                <span className="text-sm">Kun kritiske</span>
              </div>
              <Switch
                checked={settings.criticalOnly}
                onCheckedChange={(v) => updateSettings({ criticalOnly: v })}
              />
            </div>

            <div className="pt-3 border-t border-border/50 space-y-2">
              <p className="text-xs text-muted-foreground mb-2">Test notifikationer</p>
              <div className="flex gap-2">
                <Button 
                  variant="outline" 
                  size="sm" 
                  className="flex-1 text-xs h-8 border-destructive/50 text-destructive hover:bg-destructive hover:text-destructive-foreground"
                  onClick={() => sendTestNotification('critical')}
                >
                  Kritisk
                </Button>
                <Button 
                  variant="outline" 
                  size="sm" 
                  className="flex-1 text-xs h-8 border-orange-400/50 text-orange-400 hover:bg-orange-400 hover:text-white"
                  onClick={() => sendTestNotification('warning')}
                >
                  Advarsel
                </Button>
                <Button 
                  variant="outline" 
                  size="sm" 
                  className="flex-1 text-xs h-8 border-primary/50 text-primary hover:bg-primary hover:text-primary-foreground"
                  onClick={() => sendTestNotification('info')}
                >
                  Info
                </Button>
              </div>
            </div>
          </TabsContent>
        </Tabs>
      </PopoverContent>
    </Popover>
  );
};

export default NotificationCenter;