-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotifications-section.tsx
More file actions
162 lines (148 loc) · 5.89 KB
/
notifications-section.tsx
File metadata and controls
162 lines (148 loc) · 5.89 KB
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
import { useState, useEffect, useCallback } from "react"
import { Loader2, Plus, AlertTriangle } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Alert, AlertDescription } from "@/components/ui/alert"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import { listChannels, deleteChannel, updateChannel, getHealthDetails } from "@/lib/api"
import type { NotificationChannelInfo } from "@/lib/api"
import { toast } from "sonner"
import { ChannelTable } from "./notifications/channel-table"
import { ChannelDialog } from "./notifications/channel-dialog"
import { ChannelEmptyState } from "./notifications/channel-empty-state"
interface NotificationsSectionProps {
onChannelCountChange: (count: number) => void
}
export function NotificationsSection({ onChannelCountChange }: NotificationsSectionProps) {
const [channels, setChannels] = useState<NotificationChannelInfo[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const [dialogOpen, setDialogOpen] = useState(false)
const [editingChannel, setEditingChannel] = useState<NotificationChannelInfo | null>(null)
const [deletingChannel, setDeletingChannel] = useState<NotificationChannelInfo | null>(null)
const [baseUrlHttps, setBaseUrlHttps] = useState<boolean | null>(null)
const fetchChannels = useCallback(async () => {
setLoading(true)
setError(null)
try {
const data = await listChannels()
setChannels(data)
onChannelCountChange(data.filter((c) => c.enabled).length)
} catch {
setError("Failed to load notification channels")
} finally {
setLoading(false)
}
}, [onChannelCountChange])
useEffect(() => { void fetchChannels() }, [fetchChannels])
useEffect(() => {
getHealthDetails().then((h) => setBaseUrlHttps(h.base_url_https ?? null)).catch(() => {})
}, [])
function openCreate() {
setEditingChannel(null)
setDialogOpen(true)
}
function openEdit(ch: NotificationChannelInfo) {
setEditingChannel(ch)
setDialogOpen(true)
}
async function handleToggleEnabled(ch: NotificationChannelInfo) {
try {
await updateChannel(ch.id, { enabled: !ch.enabled })
toast.success(`${ch.name} ${ch.enabled ? "disabled" : "enabled"}`)
void fetchChannels()
} catch {
toast.error(`Failed to ${ch.enabled ? "disable" : "enable"} channel`)
}
}
async function handleDelete() {
if (!deletingChannel) return
try {
await deleteChannel(deletingChannel.id)
toast.success(`Deleted ${deletingChannel.name}`)
setDeletingChannel(null)
void fetchChannels()
} catch {
toast.error("Failed to delete channel")
}
}
const hasInteractiveChannels = channels.some((c) =>
["telegram", "discord", "slack_app"].includes(c.channel_type)
)
return (
<div className="space-y-4">
{baseUrlHttps === false && hasInteractiveChannels && (
<Alert variant="destructive">
<AlertTriangle className="size-4" />
<AlertDescription>
<strong>Interactive approvals won't work.</strong> The server's{" "}
<code className="text-xs font-mono">EDICTUM_BASE_URL</code> is not set to an HTTPS
URL, so Telegram / Discord / Slack buttons cannot send callbacks back to this server.
Set <code className="text-xs font-mono">EDICTUM_BASE_URL=https://your-domain.com</code>{" "}
and redeploy to enable Approve / Deny buttons.
</AlertDescription>
</Alert>
)}
<div className="flex items-center justify-between">
<div>
<h2 className="text-base font-semibold">Notification Channels</h2>
<p className="text-sm text-muted-foreground">
Receive alerts for approvals, deployments, and agent disconnects.
</p>
</div>
{channels.length > 0 && (
<Button size="sm" onClick={openCreate}>
<Plus className="mr-1.5 size-4" />Add Channel
</Button>
)}
</div>
{loading && channels.length === 0 ? (
<div className="flex items-center justify-center py-12">
<Loader2 className="size-6 animate-spin text-muted-foreground" />
</div>
) : error && channels.length === 0 ? (
<div className="flex flex-col items-center gap-2 py-12 text-center">
<p className="text-sm text-muted-foreground">{error}</p>
<Button variant="outline" size="sm" onClick={() => void fetchChannels()}>Retry</Button>
</div>
) : channels.length === 0 ? (
<ChannelEmptyState onCreateClick={openCreate} />
) : (
<ChannelTable channels={channels} baseUrlHttps={baseUrlHttps} onEdit={openEdit} onDelete={setDeletingChannel} onToggleEnabled={handleToggleEnabled} />
)}
<ChannelDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
channel={editingChannel}
onSaved={fetchChannels}
/>
<AlertDialog open={!!deletingChannel} onOpenChange={(o) => !o && setDeletingChannel(null)}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete {deletingChannel?.name}?</AlertDialogTitle>
<AlertDialogDescription>
This will stop all notifications through this channel. This action cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={handleDelete}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
Delete
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)
}