Files
Epicure/apps/web/components/social/notification-bell.tsx
T
Arnaud 362f65656b fix: audit fixes — tier-quota bypass, webhook SSRF, auth hardening, pagination, a11y
Full audit (bugs/UI-UX/backend/feature-gap) turned up a money-leak AI quota
bypass, webhook SSRF, and a long tail of missing pagination/auth/a11y work.
Fixes land together since HANDOFF.md tracked them as one backlog.

- AI routes charge tier quota before generating; nutrition POST is author-only
- Webhook dispatch re-validates URL per delivery (SSRF/DNS-rebinding), treats
  redirects as failures; recipe.published now actually dispatches
- New indexes/unique constraints on recipes, meal-planning, comments FK cascade
- Recipe PUT/restore snapshot only inside the transaction, after validation
- Recipe DELETE cleans up S3 objects (recipe + review photos)
- Optimistic UI (favorite/star/follow/shopping-list) rolls back on failure
- Upload presign enforces file size cap + per-tier storage quota
- Route-level loading/error/not-found states across (app), admin, and root
- middleware.ts guards (app)/admin; requireAdmin checks DB role, not cached
  session; rate limiting applied to both session and API-key branches,
  bucketed per key; Stripe webhook dedupes by event id
- Pagination added to recipes, feed, profile, comments, pantry, admin tables
- Nav shows real avatar + profile link + dark-mode toggle; destructive actions
  standardized on AlertDialog
- Unsaved-changes guard + real ingredient/step validation on recipe form;
  canonical /recipes/[id] used in-app; next/image migration; aria-labels and
  alt text across icon buttons, avatars, recipe photos
- packages/api-types removed (zero callers, too drifted to safely rewire);
  openapi.ts and ai-keys error shape drift fixed; BYOK decrypt failures now
  surface instead of silently falling back to the platform key

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-09 21:50:35 +02:00

111 lines
3.8 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { useTranslations } from "next-intl";
import { Bell } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Badge } from "@/components/ui/badge";
import { cn } from "@/lib/utils";
type Notification = {
id: string;
type: "follow" | "comment" | "reply" | "reaction" | "rating" | "mention";
recipeId: string | null;
commentId: string | null;
read: boolean;
createdAt: string;
actorId: string;
actorName: string;
actorUsername: string | null;
};
function notificationHref(n: Notification): string {
if (n.type === "follow") return n.actorUsername ? `/u/${n.actorUsername}` : "#";
if (n.recipeId) return `/recipes/${n.recipeId}`;
return "#";
}
export function NotificationBell() {
const t = useTranslations("notifications");
const [notifications, setNotifications] = useState<Notification[]>([]);
const [unreadCount, setUnreadCount] = useState(0);
const [open, setOpen] = useState(false);
async function load() {
try {
const res = await fetch("/api/v1/notifications");
if (!res.ok) return;
const data = (await res.json()) as { notifications: Notification[]; unreadCount: number };
setNotifications(data.notifications);
setUnreadCount(data.unreadCount);
} catch {
// silent — bell just stays at last known state
}
}
useEffect(() => {
void load();
const interval = setInterval(() => { void load(); }, 30_000);
return () => clearInterval(interval);
}, []);
async function markAllRead() {
setUnreadCount(0);
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
await fetch("/api/v1/notifications/read", { method: "POST" });
}
return (
<DropdownMenu open={open} onOpenChange={(next) => { setOpen(next); if (next) void load(); }}>
<DropdownMenuTrigger render={<Button variant="ghost" size="icon" className="relative" aria-label={t("title")} />}>
<Bell className="h-4 w-4" />
{unreadCount > 0 && (
<Badge
variant="destructive"
className="absolute -top-1 -right-1 h-4 min-w-4 px-1 text-[10px] leading-none flex items-center justify-center"
>
{unreadCount > 9 ? "9+" : unreadCount}
</Badge>
)}
<span className="sr-only">{t("title")}</span>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-80">
<div className="flex items-center justify-between px-2 py-1.5">
<span className="text-sm font-medium">{t("title")}</span>
{unreadCount > 0 && (
<button
type="button"
onClick={() => { void markAllRead(); }}
className="text-xs text-muted-foreground hover:text-foreground underline underline-offset-4"
>
{t("markAllRead")}
</button>
)}
</div>
{notifications.length === 0 && (
<p className="px-2 py-4 text-sm text-muted-foreground text-center">{t("empty")}</p>
)}
<div className="max-h-96 overflow-y-auto">
{notifications.map((n) => (
<DropdownMenuItem key={n.id} render={<Link href={notificationHref(n)} />}>
<div className={cn("flex items-start gap-2 py-1", !n.read && "font-medium")}>
{!n.read && <span className="mt-1.5 h-1.5 w-1.5 rounded-full bg-primary shrink-0" />}
<span className={cn("text-sm", n.read && "text-muted-foreground")}>
{t(n.type, { name: n.actorName })}
</span>
</div>
</DropdownMenuItem>
))}
</div>
</DropdownMenuContent>
</DropdownMenu>
);
}