-
Notifications
You must be signed in to change notification settings - Fork 544
[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
graphite-app
merged 1 commit into
main
from
_Dashboard_Add_notifications_system_with_unread_tracking
Jun 9, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
65 changes: 65 additions & 0 deletions
65
apps/dashboard/src/@/components/blocks/notifications/notification-button.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
82 changes: 82 additions & 0 deletions
82
apps/dashboard/src/@/components/blocks/notifications/notification-entry.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.