-
Notifications
You must be signed in to change notification settings - Fork 3
feat: notification system #275
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
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,92 @@ | ||
| 'use client'; | ||
|
|
||
| import Link from 'next/link'; | ||
| import { RiCloseLine } from 'react-icons/ri'; | ||
| import { useActiveNotifications } from '@/hooks/useActiveNotifications'; | ||
| import { useNotificationStore } from '@/stores/useNotificationStore'; | ||
|
|
||
| export function NotificationBanner() { | ||
| const { currentNotification, totalCount, isLoading } = useActiveNotifications(); | ||
| const dismiss = useNotificationStore((s) => s.dismiss); | ||
|
|
||
| // Don't render if no notification or still loading | ||
| if (!currentNotification || isLoading) { | ||
| return null; | ||
| } | ||
|
|
||
| const handleDismiss = () => { | ||
| dismiss(currentNotification.id); | ||
| }; | ||
|
|
||
| const action = currentNotification.action; | ||
|
|
||
| return ( | ||
| <div className="relative w-full bg-primary"> | ||
| {/* Grid background overlay */} | ||
| <div | ||
| className="pointer-events-none absolute inset-0 bg-dot-grid opacity-30" | ||
| aria-hidden="true" | ||
| /> | ||
|
|
||
| {/* Content container - same height as header */} | ||
| <div className="relative flex h-[48px] items-center md:h-[56px]"> | ||
| <div className="container mx-auto flex items-center justify-center gap-4 px-4 sm:px-6 md:px-8"> | ||
| {/* Badge for multiple notifications */} | ||
| {totalCount > 1 && ( | ||
| <span className="font-zen text-xs text-primary-foreground/80">1/{totalCount}</span> | ||
| )} | ||
|
|
||
| {/* Custom icon if provided */} | ||
| {currentNotification.icon && ( | ||
| <span className="text-primary-foreground">{currentNotification.icon}</span> | ||
| )} | ||
|
|
||
| {/* Message */} | ||
| <p className="font-zen text-sm text-primary-foreground">{currentNotification.message}</p> | ||
|
|
||
| {/* Action button */} | ||
| {action && | ||
| (action.href ? ( | ||
| <Link | ||
| href={action.href} | ||
| onClick={handleDismiss} | ||
| className="font-zen text-xs text-primary-foreground underline-offset-2 transition-colors hover:underline" | ||
| > | ||
| {action.label} | ||
| </Link> | ||
| ) : ( | ||
| <button | ||
| type="button" | ||
| onClick={() => { | ||
| action.onClick?.(); | ||
| handleDismiss(); | ||
| }} | ||
| className="font-zen text-xs text-primary-foreground underline-offset-2 transition-colors hover:underline" | ||
| > | ||
| {action.label} | ||
| </button> | ||
| ))} | ||
|
|
||
| {/* Close button */} | ||
| <button | ||
| type="button" | ||
| onClick={handleDismiss} | ||
| className="absolute right-4 p-1 text-primary-foreground/80 transition-colors hover:text-primary-foreground sm:right-6 md:right-8" | ||
| aria-label="Dismiss notification" | ||
| > | ||
| <RiCloseLine className="h-5 w-5" /> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Hook to check if notification banner is currently visible. | ||
| * Used by Header to calculate dynamic spacer height. | ||
| */ | ||
| export const useNotificationBannerVisible = (): boolean => { | ||
| const { currentNotification, isLoading } = useActiveNotifications(); | ||
| return !isLoading && currentNotification !== null; | ||
| }; |
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,50 @@ | ||
| import type { ReactNode } from 'react'; | ||
|
|
||
| export type NotificationType = 'info' | 'warning' | 'success' | 'alert'; | ||
|
|
||
| export type NotificationAction = { | ||
| label: string; | ||
| href?: string; | ||
| onClick?: () => void; | ||
| }; | ||
|
|
||
| export type NotificationConfig = { | ||
| /** Unique identifier for persistence */ | ||
| id: string; | ||
| /** Message to display in the banner */ | ||
| message: string; | ||
| /** Optional custom icon (ReactNode) */ | ||
| icon?: ReactNode; | ||
| /** Notification type for styling */ | ||
| type: NotificationType; | ||
| /** Optional action button */ | ||
| action?: NotificationAction; | ||
| /** Optional expiration date - notification auto-hides after this */ | ||
| expiresAt?: Date; | ||
| /** Category: global (all users) or personalized (condition-based) */ | ||
| category: 'global' | 'personalized'; | ||
| /** For personalized notifications, maps to a condition in useNotificationConditions */ | ||
| conditionId?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Centralized notification definitions. | ||
| * Add new notifications here with a unique id. | ||
| * | ||
| * Global notifications show to all users until dismissed or expired. | ||
| * Personalized notifications require a conditionId that maps to useNotificationConditions. | ||
| */ | ||
| export const NOTIFICATIONS: NotificationConfig[] = [ | ||
| // Example global notification (uncomment to test): | ||
| // { | ||
| // id: 'autovault-launch-2026', | ||
| // message: 'AutoVault is now live! Deploy your own automated lending vault.', | ||
| // type: 'info', | ||
| // category: 'global', | ||
| // action: { | ||
| // label: 'Try AutoVault', | ||
| // href: '/autovault', | ||
| // }, | ||
| // expiresAt: new Date('2026-01-04'), | ||
| // }, | ||
| ]; | ||
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,96 @@ | ||
| import { useMemo } from 'react'; | ||
| import { NOTIFICATIONS, type NotificationConfig } from '@/config/notifications'; | ||
| import { useNotificationStore } from '@/stores/useNotificationStore'; | ||
| import { useNotificationConditions } from './useNotificationConditions'; | ||
|
|
||
| export type ActiveNotificationsResult = { | ||
| /** Current notification to display (first in queue) */ | ||
| currentNotification: NotificationConfig | null; | ||
| /** Total count of active notifications (for badge) */ | ||
| totalCount: number; | ||
| /** Current position in queue (1-indexed) */ | ||
| currentIndex: number; | ||
| /** Whether conditions are still loading */ | ||
| isLoading: boolean; | ||
| /** All active notifications */ | ||
| activeNotifications: NotificationConfig[]; | ||
| }; | ||
|
|
||
| /** | ||
| * Combines notification config, dismissed state, and conditions | ||
| * to return the list of active notifications. | ||
| * | ||
| * Filters out: | ||
| * - Expired notifications (expiresAt < now) | ||
| * - Dismissed notifications (in localStorage) | ||
| * - Personalized notifications where condition is false or loading | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const { currentNotification, totalCount, isLoading } = useActiveNotifications(); | ||
| * | ||
| * if (!isLoading && currentNotification) { | ||
| * // Render notification banner | ||
| * } | ||
| * ``` | ||
| */ | ||
| export const useActiveNotifications = (): ActiveNotificationsResult => { | ||
| const isDismissed = useNotificationStore((s) => s.isDismissed); | ||
| const conditions = useNotificationConditions(); | ||
|
|
||
| const { activeNotifications, isLoading } = useMemo(() => { | ||
| const now = new Date(); | ||
| let hasLoadingCondition = false; | ||
|
|
||
| const active = NOTIFICATIONS.filter((notification) => { | ||
| // Check if expired | ||
| if (notification.expiresAt && notification.expiresAt < now) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if dismissed | ||
| if (isDismissed(notification.id)) { | ||
| return false; | ||
| } | ||
|
|
||
| // For personalized notifications, check condition | ||
| if (notification.category === 'personalized' && notification.conditionId) { | ||
| const condition = conditions.get(notification.conditionId); | ||
|
|
||
| // If condition not found, don't show | ||
| if (!condition) { | ||
| return false; | ||
| } | ||
|
|
||
| // Track loading state | ||
| if (condition.isLoading) { | ||
| hasLoadingCondition = true; | ||
| return false; | ||
| } | ||
|
|
||
| // Only show if condition is true | ||
| if (!condition.shouldShow) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| return { | ||
| activeNotifications: active, | ||
| isLoading: hasLoadingCondition, | ||
| }; | ||
| }, [isDismissed, conditions]); | ||
|
|
||
| const currentNotification = activeNotifications[0] ?? null; | ||
| const totalCount = activeNotifications.length; | ||
|
|
||
| return { | ||
| currentNotification, | ||
| totalCount, | ||
| currentIndex: totalCount > 0 ? 1 : 0, | ||
| isLoading, | ||
| activeNotifications, | ||
| }; | ||
| }; |
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,40 @@ | ||
| import { useMemo } from 'react'; | ||
|
|
||
| export type ConditionResult = { | ||
| conditionId: string; | ||
| shouldShow: boolean; | ||
| isLoading: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Evaluates personalized notification conditions. | ||
| * Each condition maps to a conditionId used in notification config. | ||
| * | ||
| * Add new conditions here as needed. Each condition should return: | ||
| * - shouldShow: whether the notification should display | ||
| * - isLoading: whether data is still loading (prevents flash) | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const conditions = useNotificationConditions(); | ||
| * const vaultCondition = conditions.get('vaultSetupIncomplete'); | ||
| * if (vaultCondition?.shouldShow) { ... } | ||
| * ``` | ||
| */ | ||
| export const useNotificationConditions = (): Map<string, ConditionResult> => { | ||
| const conditions = useMemo(() => { | ||
| const map = new Map<string, ConditionResult>(); | ||
|
|
||
| // Add conditions here as needed | ||
| // Example: | ||
| // map.set('vaultSetupIncomplete', { | ||
| // conditionId: 'vaultSetupIncomplete', | ||
| // shouldShow: /* check if user has vault needing setup */, | ||
| // isLoading: /* loading state */, | ||
| // }); | ||
|
|
||
| return map; | ||
| }, []); | ||
|
|
||
| return conditions; | ||
| }; |
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,58 @@ | ||
| import { create } from 'zustand'; | ||
| import { persist } from 'zustand/middleware'; | ||
|
|
||
| type NotificationState = { | ||
| /** Set of dismissed notification IDs */ | ||
| dismissedIds: string[]; | ||
| }; | ||
|
|
||
| type NotificationActions = { | ||
| /** Dismiss a notification by ID */ | ||
| dismiss: (id: string) => void; | ||
| /** Check if a notification is dismissed */ | ||
| isDismissed: (id: string) => boolean; | ||
| /** Bulk update for migration */ | ||
| setAll: (state: Partial<NotificationState>) => void; | ||
| }; | ||
|
|
||
| type NotificationStore = NotificationState & NotificationActions; | ||
|
|
||
| /** | ||
| * Zustand store for tracking dismissed notification IDs. | ||
| * Automatically persisted to localStorage. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const dismiss = useNotificationStore((s) => s.dismiss); | ||
| * const isDismissed = useNotificationStore((s) => s.isDismissed); | ||
| * | ||
| * // Dismiss a notification | ||
| * dismiss('notification-id'); | ||
| * | ||
| * // Check if dismissed | ||
| * if (isDismissed('notification-id')) { ... } | ||
| * ``` | ||
| */ | ||
| export const useNotificationStore = create<NotificationStore>()( | ||
| persist( | ||
| (set, get) => ({ | ||
| dismissedIds: [], | ||
|
|
||
| dismiss: (id) => { | ||
| const { dismissedIds } = get(); | ||
| if (!dismissedIds.includes(id)) { | ||
| set({ dismissedIds: [...dismissedIds, id] }); | ||
| } | ||
| }, | ||
|
|
||
| isDismissed: (id) => { | ||
| return get().dismissedIds.includes(id); | ||
| }, | ||
|
|
||
| setAll: (state) => set(state), | ||
| }), | ||
| { | ||
| name: 'monarch_store_notifications', | ||
| }, | ||
| ), | ||
| ); |
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.
🛠️ Refactor suggestion | 🟠 Major
Make NotificationAction type-safe.
The type allows actions with neither
hrefnoronClick(button does nothing) or both (ambiguous behavior). Use a discriminated union to enforce exactly one action type.🔎 Proposed type-safe structure
🤖 Prompt for AI Agents