Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { MouseEvent, useMemo, useRef, useState } from "react";
import { useRouter } from "@tanstack/react-router";
import { Bell, BellIcon } from "lucide-react";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
Badge,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
IconButton,
Loader
} from "@app/components/v3";
import {
useDeleteAllNotifications,
useDeleteNotification,
useMarkAllNotificationsAsRead,
useUpdateNotification
} from "@app/hooks/api/notifications/mutations";
import { useGetMyNotifications } from "@app/hooks/api/notifications/queries";
import { isCriticalNotification, TUserNotification } from "@app/hooks/api/notifications/types";
import { Notification } from "./Notification";
const NOTIFICATIONS_PER_PAGE = 20;
export const NotificationDropdown = () => {
const router = useRouter();
const [visibleNotificationCount, setVisibleNotificationCount] = useState(NOTIFICATIONS_PER_PAGE);
const [isClearAllOpen, setIsClearAllOpen] = useState(false);
const clearAllButtonRef = useRef<HTMLButtonElement>(null);
const { data: notifications, isLoading } = useGetMyNotifications();
const { mutate: markAllAsRead } = useMarkAllNotificationsAsRead();
const { mutate: updateNotification } = useUpdateNotification();
const { mutate: deleteNotification } = useDeleteNotification();
const { mutate: deleteAllNotifications, isPending: isClearingAll } = useDeleteAllNotifications();
// Links are stored server-side and may be site-relative or absolute, and may carry a query string. Passing them
// straight to `to` would resolve them against the current path and swallow the search params, so normalize to a
// path + search + hash and let the router parse it via `href`.
const getNotificationHref = (link: string) => {
try {
const { pathname, search, hash } = new URL(link, window.location.origin);
return `${pathname}${search}${hash}`;
} catch {
return undefined;
}
};
const markNotificationRead = (notification: TUserNotification) => {
if (!notification.isRead) {
updateNotification({ notificationId: notification.id, isRead: true });
}
};
const handleNotificationOpen = (
event: MouseEvent<HTMLElement>,
notification: TUserNotification,
href?: string
) => {
markNotificationRead(notification);
if (
!href ||
event.defaultPrevented ||
event.button !== 0 ||
event.metaKey ||
event.ctrlKey ||
event.shiftKey ||
event.altKey
) {
return;
}
event.preventDefault();
router.navigate({ href });
};
const unreadCount = useMemo(
() => notifications?.filter((n) => !n.isRead).length || 0,
[notifications]
);
const criticalCount = useMemo(
() => notifications?.filter((n) => !n.isRead && isCriticalNotification(n.type)).length || 0,
[notifications]
);
const hasCritical = criticalCount > 0;
const visibleNotifications = notifications?.slice(0, visibleNotificationCount);
const hasMoreNotifications = notifications && visibleNotificationCount < notifications.length;
return (
<>
<DropdownMenu
modal={false}
onOpenChange={(isOpen) => {
if (isOpen) setVisibleNotificationCount(NOTIFICATIONS_PER_PAGE);
}}
>
<DropdownMenuTrigger asChild>
<IconButton variant="outline" size="sm" aria-label="Notifications" className="relative">
{unreadCount > 0 ? <BellIcon className="text-warning" /> : <Bell />}
{unreadCount > 0 && (
<span
aria-hidden="true"
className="absolute -top-0.5 -right-0.5 z-10 size-2 rounded-full bg-warning ring-2 ring-background"
/>
)}
</IconButton>
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
side="bottom"
className="flex h-[550px] w-[400px] overflow-hidden p-0"
onInteractOutside={(e) => {
if (isClearAllOpen) e.preventDefault();
}}
>
<div className="flex w-full flex-col">
<div className="flex items-center justify-between border-b border-border px-3 py-2">
<div className="flex items-center gap-2">
<span className="font-medium text-foreground">Notifications</span>
{hasCritical && (
<Badge variant="danger">
{criticalCount > 99 ? "99+" : criticalCount} Critical
</Badge>
)}
</div>
<button
ref={clearAllButtonRef}
type="button"
className="text-xs font-medium text-muted hover:text-foreground disabled:pointer-events-none disabled:opacity-50"
onClick={(e) => {
e.preventDefault();
if (unreadCount > 0) markAllAsRead();
else setIsClearAllOpen(true);
}}
disabled={!notifications?.length || isClearingAll}
>
{unreadCount > 0 ? "Mark all as read" : "Clear All"}
</button>
</div>
<div className="flex h-full w-full overflow-auto">
{isLoading && (
<div className="flex h-full w-full items-center justify-center">
<Loader className="pointer-events-none" size="sm" />
</div>
)}
{!isLoading && notifications?.length === 0 && (
<div className="flex h-full w-full flex-col items-center justify-center">
<Bell className="size-10 text-muted" />
<span className="mt-4 text-sm text-accent">No new notifications</span>
<span className="text-xs text-muted">
We'll let you know when something important happens.
</span>
</div>
)}
{!isLoading && notifications && notifications.length > 0 && (
<div className="flex w-full flex-col">
{visibleNotifications?.map((notification) => {
const href = notification.link
? getNotificationHref(notification.link)
: undefined;
return (
<Notification
key={notification.id}
notification={notification}
href={href}
onOpen={(event) => handleNotificationOpen(event, notification, href)}
onAuxOpen={() => markNotificationRead(notification)}
onDelete={deleteNotification}
/>
);
})}
{hasMoreNotifications && (
<DropdownMenuItem
onSelect={(e) => {
e.preventDefault();
setVisibleNotificationCount((count) => count + NOTIFICATIONS_PER_PAGE);
}}
>
Show More
</DropdownMenuItem>
)}
</div>
)}
</div>
</div>
</DropdownMenuContent>
</DropdownMenu>
<AlertDialog open={isClearAllOpen} onOpenChange={setIsClearAllOpen}>
<AlertDialogContent
onCloseAutoFocus={(e) => {
e.preventDefault();
if (clearAllButtonRef.current && !clearAllButtonRef.current.disabled) {
clearAllButtonRef.current.focus();
} else {
clearAllButtonRef.current?.closest<HTMLElement>("[role='menu']")?.focus();
}
}}
>
<AlertDialogHeader>
<AlertDialogTitle>Clear All Notifications?</AlertDialogTitle>
<AlertDialogDescription>
This will remove all your read notifications for this organization, including account
notifications. This cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel variant="outline" disabled={isClearingAll}>
Cancel
</AlertDialogCancel>
<AlertDialogAction
variant="danger"
isPending={isClearingAll}
onClick={(e) => {
e.preventDefault();
deleteAllNotifications(undefined, {
onSuccess: () => setIsClearAllOpen(false)
});
}}
>
Clear All
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
};