-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNotificationTargets.tsx
More file actions
647 lines (590 loc) · 19.8 KB
/
NotificationTargets.tsx
File metadata and controls
647 lines (590 loc) · 19.8 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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// src/components/NotificationTargets.tsx
//
// Functions: NotificationTargets, NotificationCard, AddNotificationForm
"use client"
import { useQuery, useQueryClient } from "@tanstack/react-query"
import { H2, H3, Paragraph } from "@typography"
import { useCallback, useState } from "react"
import { Badge } from "@/components/ui/Badge"
import { Button } from "@/components/ui/Button"
import { Card } from "@/components/ui/Card"
import { CollapsibleCard } from "@/components/ui/CollapsibleCard"
import { ConfirmRemove } from "@/components/ui/ConfirmRemove"
import { InfoTip } from "@/components/ui/InfoTip"
import { Input } from "@/components/ui/Input"
import { MaskedSecret } from "@/components/ui/MaskedSecret"
import { Notice } from "@/components/ui/Notice"
import { NumberInput } from "@/components/ui/NumberInput"
import { SaveDiscardBar } from "@/components/ui/SaveDiscardBar"
import { Select } from "@/components/ui/Select"
import { CardListSkeleton } from "@/components/ui/skeletons"
import { Toggle } from "@/components/ui/Toggle"
import { useActionStatus } from "@/hooks/useActionStatus"
import { useCrudCard } from "@/hooks/useCrudCard"
import { DOCS } from "@/lib/constants"
import { formatTimeAgo } from "@/lib/formatters"
import type { NotificationTargetType } from "@/lib/notifications/types"
import type { SafeNotificationTarget } from "@/types/api"
type NotificationTarget = SafeNotificationTarget
const NOTIFICATION_TYPE_OPTIONS: {
value: NotificationTargetType
label: string
disabled?: boolean
}[] = [
{ value: "discord", label: "Discord" },
{ value: "gotify", label: "Gotify (coming soon)", disabled: true },
{ value: "telegram", label: "Telegram (coming soon)", disabled: true },
{ value: "slack", label: "Slack (coming soon)", disabled: true },
{ value: "email", label: "Email (coming soon)", disabled: true },
]
// ---------------------------------------------------------------------------
// NotificationCard
// ---------------------------------------------------------------------------
interface NotificationCardProps {
target: NotificationTarget
onSaved: (id: number, updated: NotificationTarget) => void
onRemove: (id: number) => void
}
/** Fields tracked for dirty detection (excludes transient fields). */
const DRAFT_KEYS: (keyof NotificationTarget)[] = [
"name",
"type",
"enabled",
"notifyRatioDrop",
"notifyHitAndRun",
"notifyTrackerDown",
"notifyBufferMilestone",
"notifyWarned",
"notifyRatioDanger",
"notifyZeroSeeding",
"notifyRankChange",
"notifyAnniversary",
"notifyBonusCap",
"notifyVipExpiring",
"notifyUnsatisfiedLimit",
"notifyActiveHnrs",
"notifyDownloadDisabled",
"thresholds",
"includeTrackerName",
"scope",
]
function buildPatch(
draft: NotificationTarget,
saved: NotificationTarget
): Record<string, unknown> | null {
const patch: Record<string, unknown> = {}
for (const key of DRAFT_KEYS) {
const a = draft[key]
const b = saved[key]
if (a === null && b === null) continue
if (typeof a === "object" && typeof b === "object" && a !== null && b !== null) {
if (JSON.stringify(a) !== JSON.stringify(b)) patch[key] = a
} else if (a !== b) patch[key] = a
}
return Object.keys(patch).length > 0 ? patch : null
}
function NotificationCard({ target, onSaved, onRemove }: NotificationCardProps) {
const {
draft,
setDraft,
updateDraft,
dirty,
saving,
saveError,
expanded,
toggleExpand,
handleSave,
handleDiscard,
} = useCrudCard({
item: target,
apiEndpoint: "/api/notifications",
buildPatch,
onSaved,
})
const { status: webhookStatus, error: webhookError, execute: executeTest } = useActionStatus()
function updateThreshold(patch: Partial<NonNullable<NotificationTarget["thresholds"]>>) {
setDraft((prev) => ({
...prev,
thresholds: { ...(prev.thresholds ?? {}), ...patch },
}))
}
// Webhook config change state
const [changingConfig, setChangingConfig] = useState(!target.hasConfig)
const [newWebhookUrl, setNewWebhookUrl] = useState("")
const [configError, setConfigError] = useState<string | null>(null)
async function handleSaveConfig() {
if (!newWebhookUrl.trim()) return
setConfigError(null)
try {
const res = await fetch(`/api/notifications/${target.id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ config: { webhookUrl: newWebhookUrl.trim() } }),
})
if (!res.ok) {
const data = await res.json().catch(() => ({}))
setConfigError(data.error || "Failed to save webhook URL")
return
}
onSaved(target.id, { ...target, hasConfig: true })
setChangingConfig(false)
setNewWebhookUrl("")
} catch {
setConfigError("Network error — could not save webhook URL")
}
}
const handleTestWebhook = () =>
executeTest(async () => {
const res = await fetch(`/api/notifications/${target.id}/test`, { method: "POST" })
if (!res.ok) {
const data = await res.json().catch(() => ({}))
throw new Error(data.error || "Test failed")
}
})
const ratioDropDelta = draft.thresholds?.ratioDropDelta ?? 0.1
const statusBadge = draft.enabled ? (
<Badge variant="success">Active</Badge>
) : (
<Badge variant="default">Disabled</Badge>
)
const typeBadge = (
<Badge variant="default">
{NOTIFICATION_TYPE_OPTIONS.find((o) => o.value === draft.type)?.label ?? draft.type}
</Badge>
)
return (
<CollapsibleCard
expanded={expanded}
onToggle={toggleExpand}
header={
<div className="flex flex-col gap-0.5">
<div className="flex items-center gap-3">
<H3 className="truncate">{draft.name || "Untitled Target"}</H3>
{typeBadge}
{statusBadge}
</div>
<span className="text-3xs font-mono text-tertiary">
{target.lastDeliveryAt
? `Last sent: ${formatTimeAgo(target.lastDeliveryAt)}`
: "Last sent: Never"}
</span>
</div>
}
>
{/* Row 1: Name + Type + Enabled */}
<div className="form-responsive-row pt-5">
<div className="flex-1">
<Input
label="Name"
value={draft.name}
onChange={(e) => updateDraft({ name: e.target.value })}
placeholder="My Discord Webhook"
/>
</div>
<div className="w-full sm:w-44">
<Select
label="Type"
value={draft.type}
onChange={(v) => updateDraft({ type: v as NotificationTargetType })}
ariaLabel="Notification type"
size="md"
options={NOTIFICATION_TYPE_OPTIONS}
/>
</div>
</div>
<Toggle
label="Enabled"
checked={draft.enabled}
onChange={(v) => updateDraft({ enabled: v })}
description="Disabled targets will not receive any notifications."
/>
<div className="border-t border-border" />
{/* Webhook URL */}
<div className="flex flex-col gap-2">
<H2 className="uppercase tracking-wider flex items-center gap-2">
Webhook URL
<InfoTip
icon="question"
size="sm"
content="Encrypted at rest with AES-256-GCM. Never returned in API responses."
docs={DOCS.WEBHOOKS}
/>
</H2>
{changingConfig ? (
<div className="flex flex-col gap-3">
<Input
label="Webhook URL"
value={newWebhookUrl}
onChange={(e) => setNewWebhookUrl(e.target.value)}
placeholder="https://discord.com/api/webhooks/..."
name="notification-webhook-url"
autoComplete="off"
data-1p-ignore
/>
<div className="flex items-center gap-2">
<Button
size="sm"
variant="primary"
onClick={handleSaveConfig}
disabled={!newWebhookUrl.trim()}
text="Save URL"
/>
{target.hasConfig && (
<button
type="button"
onClick={() => {
setChangingConfig(false)
setNewWebhookUrl("")
setConfigError(null)
}}
className="ghost-link"
>
Cancel
</button>
)}
</div>
<Notice message={configError} />
</div>
) : (
<MaskedSecret onChangeClick={() => setChangingConfig(true)} />
)}
</div>
<div className="border-t border-border" />
{/* Notify when section */}
<div className="flex flex-col gap-4">
<H2 className="uppercase tracking-wider flex items-center gap-2">
Notify when
<InfoTip
icon="question"
size="sm"
content="Each event has a snooze window to prevent repeated alerts."
docs={DOCS.WEBHOOKS}
/>
</H2>
<div className="flex flex-col gap-1">
<Toggle
label="Ratio drops"
checked={draft.notifyRatioDrop}
onChange={(v) => updateDraft({ notifyRatioDrop: v })}
/>
{draft.notifyRatioDrop && (
<div className="ml-15 flex items-center gap-3">
<span className="text-xs font-mono text-tertiary">Threshold (delta)</span>
<NumberInput
value={Math.round(ratioDropDelta * 100)}
onChange={(v) => updateThreshold({ ratioDropDelta: v / 100 })}
min={1}
max={99}
step={1}
/>
<span className="text-xs font-mono text-tertiary">/ 100</span>
</div>
)}
</div>
<Toggle
label="Hit-and-run detected"
checked={draft.notifyHitAndRun}
onChange={(v) => updateDraft({ notifyHitAndRun: v })}
/>
<Toggle
label="Tracker goes down"
checked={draft.notifyTrackerDown}
onChange={(v) => updateDraft({ notifyTrackerDown: v })}
/>
<Toggle
label="Buffer milestone reached"
checked={draft.notifyBufferMilestone}
onChange={(v) => updateDraft({ notifyBufferMilestone: v })}
/>
<Toggle
label="Account warning received"
checked={draft.notifyWarned}
onChange={(v) => updateDraft({ notifyWarned: v })}
/>
<Toggle
label="Ratio below tracker minimum"
checked={draft.notifyRatioDanger}
onChange={(v) => updateDraft({ notifyRatioDanger: v })}
/>
<Toggle
label="Zero active seeds"
checked={draft.notifyZeroSeeding}
onChange={(v) => updateDraft({ notifyZeroSeeding: v })}
/>
<Toggle
label="Rank/class changed"
checked={draft.notifyRankChange}
onChange={(v) => updateDraft({ notifyRankChange: v })}
/>
<Toggle
label="Membership anniversary"
checked={draft.notifyAnniversary}
onChange={(v) => updateDraft({ notifyAnniversary: v })}
/>
<Toggle
label="Bonus Points Capped"
checked={draft.notifyBonusCap}
onChange={(v) => updateDraft({ notifyBonusCap: v })}
description="Alert when seedbonus hits the cap (i.e. 99,999 on MAM)"
/>
<Toggle
label="VIP Expiring Soon"
checked={draft.notifyVipExpiring}
onChange={(v) => updateDraft({ notifyVipExpiring: v })}
description="Alert when VIP status is about to expire (MAM, AvistaZ network)"
/>
<Toggle
label="Unsatisfied Limit Warning"
checked={draft.notifyUnsatisfiedLimit}
onChange={(v) => updateDraft({ notifyUnsatisfiedLimit: v })}
description="Alert when approaching the unsatisfied torrent limit"
/>
<Toggle
label="Active Hit & Runs"
checked={draft.notifyActiveHnrs}
onChange={(v) => updateDraft({ notifyActiveHnrs: v })}
description="Alert when inactive Hit & Runs are detected"
/>
<Toggle
label="Download Privileges Revoked"
checked={draft.notifyDownloadDisabled}
onChange={(v) => updateDraft({ notifyDownloadDisabled: v })}
description="Alert when an AvistaZ network tracker revokes download access"
/>
</div>
<div className="border-t border-border" />
{/* Privacy section */}
<div className="flex flex-col gap-3">
<Toggle
label="Include tracker name in notifications"
checked={draft.includeTrackerName}
onChange={(v) => updateDraft({ includeTrackerName: v })}
/>
<Notice
variant="info"
message="Tracker names reveal which private trackers you use. Disable for maximum privacy."
/>
</div>
<SaveDiscardBar
dirty={dirty}
saving={saving}
onSave={handleSave}
onDiscard={handleDiscard}
error={saveError}
/>
<div className="border-t border-border" />
{/* Actions */}
<div className="flex items-center gap-3 flex-wrap">
<Button
size="sm"
variant="secondary"
onClick={handleTestWebhook}
disabled={webhookStatus === "pending" || !target.hasConfig}
>
{webhookStatus === "pending"
? "Sending..."
: webhookStatus === "success"
? "Sent"
: webhookStatus === "failed"
? "Failed — Retry"
: "Test Webhook"}
</Button>
{webhookStatus === "success" && <Badge variant="success">Delivered</Badge>}
{webhookStatus === "failed" && <Badge variant="danger">Failed</Badge>}
<div className="flex-1" />
<ConfirmRemove onConfirm={() => onRemove(target.id)} />
</div>
{webhookStatus === "failed" && webhookError && <Notice message={webhookError} />}
</CollapsibleCard>
)
}
// ---------------------------------------------------------------------------
// AddNotificationForm
// ---------------------------------------------------------------------------
function AddNotificationForm({
onCreated,
onCancel,
}: {
onCreated: () => void
onCancel: () => void
}) {
const [name, setName] = useState("New Target")
const [type, setType] = useState<NotificationTargetType>("discord")
const [webhookUrl, setWebhookUrl] = useState("")
const [error, setError] = useState<string | null>(null)
const [saving, setSaving] = useState(false)
const canSubmit = name.trim() && webhookUrl.trim()
async function handleSubmit() {
if (!canSubmit) return
setSaving(true)
setError(null)
try {
const res = await fetch("/api/notifications", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: name.trim(),
type,
config: { webhookUrl: webhookUrl.trim() },
}),
})
if (!res.ok) {
const data = await res.json().catch(() => ({}))
setError(data.error || "Failed to create target")
return
}
onCreated()
} catch {
setError("Network error")
} finally {
setSaving(false)
}
}
return (
<Card elevation="raised" className="flex flex-col gap-4">
<H3>Add Notification Target</H3>
<div className="form-responsive-row">
<div className="flex-1">
<Input
label="Name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="My Discord Webhook"
/>
</div>
<div className="w-full sm:w-44">
<Select
label="Type"
value={type}
onChange={(v) => setType(v as NotificationTargetType)}
ariaLabel="Notification type"
size="md"
options={NOTIFICATION_TYPE_OPTIONS}
/>
</div>
</div>
<Input
label="Webhook URL"
value={webhookUrl}
onChange={(e) => setWebhookUrl(e.target.value)}
placeholder="https://discord.com/api/webhooks/..."
name="new-notification-webhook-url"
autoComplete="off"
data-1p-ignore
/>
<Notice message={error} />
<div className="flex items-center gap-3">
<Button
size="sm"
onClick={handleSubmit}
disabled={!canSubmit || saving}
text={saving ? "Creating..." : "Create Target"}
/>
<Button size="sm" variant="ghost" onClick={onCancel} text="Cancel" />
</div>
</Card>
)
}
// ---------------------------------------------------------------------------
// NotificationTargets
// ---------------------------------------------------------------------------
function NotificationTargets() {
const queryClient = useQueryClient()
const [showAddForm, setShowAddForm] = useState(false)
const {
data: targets = [],
isLoading: loading,
error: loadError,
} = useQuery({
queryKey: ["notification-targets"],
queryFn: async ({ signal }) => {
const res = await fetch("/api/notifications", { signal })
if (!res.ok) throw new Error("Failed to load notification targets")
return res.json() as Promise<NotificationTarget[]>
},
})
const handleSaved = useCallback(
(id: number, updated: NotificationTarget) => {
queryClient.setQueryData<NotificationTarget[]>(["notification-targets"], (prev) =>
prev?.map((t) => (t.id === id ? updated : t))
)
},
[queryClient]
)
const handleRemove = useCallback(
async (id: number) => {
const res = await fetch(`/api/notifications/${id}`, { method: "DELETE" })
if (!res.ok) return
queryClient.setQueryData<NotificationTarget[]>(["notification-targets"], (prev) =>
prev?.filter((t) => t.id !== id)
)
},
[queryClient]
)
if (loading) {
return <CardListSkeleton count={2} />
}
if (loadError) {
return (
<Notice
message={
loadError instanceof Error ? loadError.message : "Failed to load notification targets"
}
/>
)
}
return (
<div className="flex flex-col gap-6">
<H2 className="flex items-center gap-2">
Webhooks
<InfoTip
content="Get alerts when something happens on your trackers."
docs={DOCS.WEBHOOKS}
/>
</H2>
{targets.length === 0 && !showAddForm ? (
<Card elevation="raised" className="flex flex-col items-center gap-4 py-10">
<span className="text-2xl" aria-hidden="true">
🔔
</span>
<div className="text-center flex flex-col gap-2">
<H3>No notification targets configured</H3>
<Paragraph>
Add a notification target to receive alerts when your tracker stats change.
</Paragraph>
</div>
<Button size="sm" onClick={() => setShowAddForm(true)} text="Add Notification Target" />
</Card>
) : (
<>
{targets.map((target) => (
<NotificationCard
key={target.id}
target={target}
onSaved={handleSaved}
onRemove={handleRemove}
/>
))}
{showAddForm ? (
<AddNotificationForm
onCreated={() => {
setShowAddForm(false)
queryClient.invalidateQueries({ queryKey: ["notification-targets"] })
}}
onCancel={() => setShowAddForm(false)}
/>
) : (
<Button
size="sm"
variant="secondary"
className="self-start"
onClick={() => setShowAddForm(true)}
text="+ Add Notification Target"
/>
)}
</>
)}
</div>
)
}
export { NotificationTargets }