Skip to content

[Dashboard] Add notifications system with unread tracking #7302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"flat": "^6.0.1",
"framer-motion": "12.9.2",
"fuse.js": "7.1.0",
"idb-keyval": "^6.2.1",
"input-otp": "^1.4.1",
"ioredis": "^5.6.1",
"ipaddr.js": "^2.2.0",
Expand Down Expand Up @@ -105,6 +104,7 @@
"thirdweb": "workspace:*",
"tiny-invariant": "^1.3.3",
"use-debounce": "^10.0.4",
"vaul": "^1.1.2",
"zod": "3.25.24"
},
"devDependencies": {
Expand Down
157 changes: 157 additions & 0 deletions apps/dashboard/src/@/api/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
"use server";

import "server-only";
import { getAuthToken } from "../../app/(app)/api/lib/getAuthToken";
import { NEXT_PUBLIC_THIRDWEB_API_HOST } from "../constants/public-envs";

export type Notification = {
id: string;
createdAt: string;
accountId: string;
teamId: string | null;
description: string;
readAt: string | null;
ctaText: string;
ctaUrl: string;
};

export type NotificationsApiResponse = {
result: Notification[];
nextCursor?: string;
};

export async function getUnreadNotifications(cursor?: string) {
const authToken = await getAuthToken();
if (!authToken) {
throw new Error("No auth token found");
}
const url = new URL(
"/v1/dashboard-notifications/unread",
NEXT_PUBLIC_THIRDWEB_API_HOST,
);
if (cursor) {
url.searchParams.set("cursor", cursor);
}

const response = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
if (!response.ok) {
const body = await response.text();
return {
status: "error",
reason: "unknown",
body,
} as const;
}

const data = (await response.json()) as NotificationsApiResponse;

return {
status: "success",
data,
} as const;
}

export async function getArchivedNotifications(cursor?: string) {
const authToken = await getAuthToken();
if (!authToken) {
throw new Error("No auth token found");
}

const url = new URL(
"/v1/dashboard-notifications/archived",
NEXT_PUBLIC_THIRDWEB_API_HOST,
);
if (cursor) {
url.searchParams.set("cursor", cursor);
}

const response = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
if (!response.ok) {
const body = await response.text();
return {
status: "error",
reason: "unknown",
body,
} as const;
}

const data = (await response.json()) as NotificationsApiResponse;

return {
status: "success",
data,
} as const;
}

export async function getUnreadNotificationsCount() {
const authToken = await getAuthToken();
if (!authToken) {
throw new Error("No auth token found");
}

const url = new URL(
"/v1/dashboard-notifications/unread-count",
NEXT_PUBLIC_THIRDWEB_API_HOST,
);
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
if (!response.ok) {
const body = await response.text();
return {
status: "error",
reason: "unknown",
body,
} as const;
}
const data = (await response.json()) as {
result: {
unreadCount: number;
};
};
return {
status: "success",
data,
} as const;
}

export async function markNotificationAsRead(notificationId?: string) {
const authToken = await getAuthToken();
if (!authToken) {
throw new Error("No auth token found");
}
const url = new URL(
"/v1/dashboard-notifications/mark-as-read",
NEXT_PUBLIC_THIRDWEB_API_HOST,
);
const response = await fetch(url, {
method: "PUT",
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
// if notificationId is provided, mark it as read, otherwise mark all as read
body: JSON.stringify(notificationId ? { notificationId } : {}),
});
if (!response.ok) {
const body = await response.text();
return {
status: "error",
reason: "unknown",
body,
} as const;
}
return {
status: "success",
} as const;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"use client";

import { Button } from "@/components/ui/button";
import {
Drawer,
DrawerContent,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { useIsMobile } from "@/hooks/use-mobile";
import { BellIcon } from "lucide-react";
import { useMemo, useState } from "react";
import { NotificationList } from "./notification-list";
import { useNotifications } from "./state/manager";

export function NotificationsButton(props: { accountId: string }) {
const manager = useNotifications(props.accountId);
const [open, setOpen] = useState(false);

const isMobile = useIsMobile();

const trigger = useMemo(
() => (
<Button variant="outline" size="icon" className="relative rounded-full">
<BellIcon className="h-4 w-4" />
{(manager.unreadNotificationsCount || 0) > 0 && (
<span className="absolute top-0 right-0 flex h-2 w-2">
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-primary opacity-75" />
<span className="relative inline-flex h-2 w-2 rounded-full bg-primary" />
</span>
)}
</Button>
),
[manager.unreadNotificationsCount],
);

if (isMobile) {
return (
<Drawer open={open} onOpenChange={setOpen}>
<DrawerTrigger asChild>{trigger}</DrawerTrigger>
<DrawerContent className="max-h-[90vh] min-h-[66vh]">
<DrawerTitle className="sr-only">Notifications</DrawerTitle>
<NotificationList {...manager} />
</DrawerContent>
</Drawer>
);
}

return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>{trigger}</PopoverTrigger>
<PopoverContent
className="max-h-[90vh] min-h-[500px] w-[400px] max-w-md p-0"
align="end"
>
<NotificationList {...manager} />
</PopoverContent>
</Popover>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"use client";

import type { Notification } from "@/api/notifications";
import { Button } from "@/components/ui/button";
import {
format,
formatDistanceToNow,
isBefore,
parseISO,
subDays,
} from "date-fns";
import { ArchiveIcon } from "lucide-react";
import { useMemo } from "react";

interface NotificationEntryProps {
notification: Notification;
onMarkAsRead?: (id: string) => void;
}

export function NotificationEntry({
notification,
onMarkAsRead,
}: NotificationEntryProps) {
const timeAgo = useMemo(() => {
try {
const now = new Date();
const date = parseISO(notification.createdAt);
// if the date is older than 1 day, show the date
// otherwise, show the time ago

if (isBefore(date, subDays(now, 1))) {
return format(date, "MMM d, yyyy");
}

return formatDistanceToNow(date, {
addSuffix: true,
});
} catch (error) {
console.error("Failed to parse date", error);
return null;
}
}, [notification.createdAt]);

return (
<div className="flex flex-row py-1.5">
{onMarkAsRead && (
<div className="min-h-full w-1 shrink-0 rounded-r-lg bg-primary" />
)}
<div className="flex w-full flex-row justify-between gap-2 border-b px-4 py-2 transition-colors last:border-b-0">
<div className="flex items-start gap-3">
<div className="flex-1 space-y-1">
<p className="text-sm">{notification.description}</p>
{timeAgo && (
<p className="text-muted-foreground text-xs">{timeAgo}</p>
)}
<div className="flex flex-row justify-between gap-2 pt-1">
<Button asChild variant="link" size="sm" className="px-0">
<a
href={notification.ctaUrl}
target="_blank"
rel="noopener noreferrer"
>
{notification.ctaText}
</a>
</Button>
</div>
</div>
</div>
{onMarkAsRead && (
<Button
variant="ghost"
size="icon"
onClick={() => onMarkAsRead(notification.id)}
className="text-muted-foreground hover:text-foreground"
>
<ArchiveIcon className="h-4 w-4" />
</Button>
)}
</div>
</div>
);
}
Loading
Loading