-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add in-app notification system with welcome message (#98) #100
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
hrx01-dev
merged 2 commits into
main
from
devin/1782065796-notification-welcome-trigger
Jun 21, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,198 @@ | ||
| import { useState, useRef, useEffect } from "react"; | ||
| import { Link, useNavigate } from "react-router-dom"; | ||
| import { motion, AnimatePresence } from "motion/react"; | ||
| import { | ||
| Bell, | ||
| CheckCheck, | ||
| Info, | ||
| CheckCircle2, | ||
| AlertTriangle, | ||
| XCircle, | ||
| ExternalLink, | ||
| } from "lucide-react"; | ||
| import { Button } from "../../app/components/ui/button"; | ||
| import { useNotifications } from "./useNotifications"; | ||
| import type { Notification, NotificationType } from "./types"; | ||
| import { formatDistanceToNow } from "date-fns"; | ||
|
|
||
| function typeIcon(type: NotificationType) { | ||
| switch (type) { | ||
| case "success": | ||
| return <CheckCircle2 className="h-4 w-4 text-emerald-500 shrink-0" />; | ||
| case "warning": | ||
| return <AlertTriangle className="h-4 w-4 text-amber-500 shrink-0" />; | ||
| case "error": | ||
| return <XCircle className="h-4 w-4 text-red-500 shrink-0" />; | ||
| default: | ||
| return <Info className="h-4 w-4 text-blue-500 shrink-0" />; | ||
| } | ||
| } | ||
|
|
||
| function NotificationItem({ | ||
| notification, | ||
| onRead, | ||
| onNavigate, | ||
| }: { | ||
| notification: Notification; | ||
| onRead: (id: string) => void; | ||
| onNavigate: () => void; | ||
| }) { | ||
| const navigate = useNavigate(); | ||
|
|
||
| const handleClick = () => { | ||
| if (!notification.isRead) onRead(notification.id); | ||
| if (notification.actionUrl) { | ||
| onNavigate(); | ||
| navigate(notification.actionUrl); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <button | ||
| onClick={handleClick} | ||
| className={`w-full text-left px-4 py-3 flex items-start gap-3 hover:bg-gray-50 dark:hover:bg-slate-800 transition-colors ${ | ||
| !notification.isRead | ||
| ? "bg-indigo-50/50 dark:bg-indigo-950/20" | ||
| : "" | ||
| }`} | ||
| > | ||
| <div className="mt-0.5">{typeIcon(notification.type)}</div> | ||
| <div className="flex-1 min-w-0"> | ||
| <p | ||
| className={`text-sm leading-tight ${ | ||
| !notification.isRead | ||
| ? "font-semibold text-gray-900 dark:text-gray-100" | ||
| : "font-medium text-gray-700 dark:text-gray-300" | ||
| }`} | ||
| > | ||
| {notification.title} | ||
| </p> | ||
| <p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5 line-clamp-2"> | ||
| {notification.message} | ||
| </p> | ||
| <p className="text-xs text-gray-400 dark:text-gray-500 mt-1"> | ||
| {formatDistanceToNow(notification.createdAt, { addSuffix: true })} | ||
| </p> | ||
| </div> | ||
| {notification.actionUrl && ( | ||
| <ExternalLink className="h-3.5 w-3.5 text-gray-400 shrink-0 mt-1" /> | ||
| )} | ||
| {!notification.isRead && ( | ||
| <div className="h-2 w-2 rounded-full bg-indigo-500 shrink-0 mt-1.5" /> | ||
| )} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| export function NotificationBell() { | ||
| const [open, setOpen] = useState(false); | ||
| const panelRef = useRef<HTMLDivElement>(null); | ||
| const { notifications, unreadCount, loading, markAsRead, markAllAsRead } = | ||
| useNotifications(); | ||
|
|
||
| useEffect(() => { | ||
| function handleClickOutside(e: MouseEvent) { | ||
| if (panelRef.current && !panelRef.current.contains(e.target as Node)) { | ||
| setOpen(false); | ||
| } | ||
| } | ||
| if (open) document.addEventListener("mousedown", handleClickOutside); | ||
| return () => document.removeEventListener("mousedown", handleClickOutside); | ||
| }, [open]); | ||
|
|
||
| const recentNotifications = notifications.slice(0, 8); | ||
|
|
||
| return ( | ||
| <div className="relative" ref={panelRef}> | ||
| <Button | ||
| variant="ghost" | ||
| size="icon" | ||
| onClick={() => setOpen((prev) => !prev)} | ||
| className="relative" | ||
| aria-label={`Notifications${unreadCount > 0 ? ` (${unreadCount} unread)` : ""}`} | ||
| > | ||
| <Bell className="h-5 w-5" /> | ||
| {unreadCount > 0 && ( | ||
| <motion.span | ||
| initial={{ scale: 0 }} | ||
| animate={{ scale: 1 }} | ||
| className="absolute -top-0.5 -right-0.5 flex h-4.5 w-4.5 items-center justify-center rounded-full bg-red-500 text-[10px] font-bold text-white ring-2 ring-white dark:ring-slate-900" | ||
| > | ||
| {unreadCount > 99 ? "99+" : unreadCount} | ||
| </motion.span> | ||
| )} | ||
| </Button> | ||
|
|
||
| <AnimatePresence> | ||
| {open && ( | ||
| <motion.div | ||
| initial={{ opacity: 0, y: -8, scale: 0.96 }} | ||
| animate={{ opacity: 1, y: 0, scale: 1 }} | ||
| exit={{ opacity: 0, y: -8, scale: 0.96 }} | ||
| transition={{ duration: 0.15 }} | ||
| className="absolute right-0 top-full mt-2 w-[360px] max-h-[480px] rounded-xl border border-gray-200 bg-white shadow-xl dark:border-slate-700 dark:bg-slate-900 z-50 flex flex-col overflow-hidden" | ||
| > | ||
| {/* Header */} | ||
| <div className="flex items-center justify-between px-4 py-3 border-b border-gray-100 dark:border-slate-800"> | ||
| <h3 className="text-sm font-semibold text-gray-900 dark:text-gray-100"> | ||
| Notifications | ||
| </h3> | ||
| {unreadCount > 0 && ( | ||
| <button | ||
| onClick={() => markAllAsRead()} | ||
| className="flex items-center gap-1 text-xs font-medium text-indigo-600 hover:text-indigo-700 dark:text-indigo-400 dark:hover:text-indigo-300" | ||
| > | ||
| <CheckCheck className="h-3.5 w-3.5" /> | ||
| Mark all read | ||
| </button> | ||
| )} | ||
| </div> | ||
|
|
||
| {/* Body */} | ||
| <div className="flex-1 overflow-y-auto"> | ||
| {loading ? ( | ||
| <div className="flex items-center justify-center py-12"> | ||
| <div className="h-6 w-6 animate-spin rounded-full border-2 border-indigo-500 border-t-transparent" /> | ||
| </div> | ||
| ) : recentNotifications.length === 0 ? ( | ||
| <div className="flex flex-col items-center py-12 px-4 text-center"> | ||
| <Bell className="h-10 w-10 text-gray-300 dark:text-gray-600 mb-3" /> | ||
| <p className="text-sm font-medium text-gray-500 dark:text-gray-400"> | ||
| No notifications yet | ||
| </p> | ||
| <p className="text-xs text-gray-400 dark:text-gray-500 mt-1"> | ||
| We'll notify you when something important happens. | ||
| </p> | ||
| </div> | ||
| ) : ( | ||
| <div className="divide-y divide-gray-100 dark:divide-slate-800"> | ||
| {recentNotifications.map((n) => ( | ||
| <NotificationItem | ||
| key={n.id} | ||
| notification={n} | ||
| onRead={markAsRead} | ||
| onNavigate={() => setOpen(false)} | ||
| /> | ||
| ))} | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| {/* Footer */} | ||
| {notifications.length > 0 && ( | ||
| <div className="border-t border-gray-100 dark:border-slate-800 px-4 py-2.5"> | ||
| <Link | ||
| to="/dashboard/notifications" | ||
| onClick={() => setOpen(false)} | ||
| className="block w-full text-center text-xs font-medium text-indigo-600 hover:text-indigo-700 dark:text-indigo-400 dark:hover:text-indigo-300" | ||
| > | ||
| View all notifications | ||
| </Link> | ||
| </div> | ||
| )} | ||
| </motion.div> | ||
| )} | ||
| </AnimatePresence> | ||
| </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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate welcome notifications for returning Google users.
signInWithPopupsucceeds for both new and existing users. A returning user who uses the signup page will receive another welcome notification. UsegetAdditionalUserInfoto check if the user is new before triggering the notification.🐛 Proposed fix to only notify new users
+import { getAdditionalUserInfo } from 'firebase/auth';const handleGoogleSignIn = async () => { setError(''); try { const provider = new GoogleAuthProvider(); - const { user } = await signInWithPopup(auth, provider); - notifyWelcome(user.uid, user.displayName ?? user.email ?? 'there'); + const result = await signInWithPopup(auth, provider); + const { user } = result; + if (getAdditionalUserInfo(result)?.isNewUser) { + notifyWelcome(user.uid, user.displayName ?? user.email ?? 'there'); + } navigate('/dashboard'); } catch (err: unknown) {📝 Committable suggestion
🤖 Prompt for AI Agents