import { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { useAuth } from "../context/AuthContext"; import { getNotifications, markNotificationsRead } from "../api/client"; import { BellIcon, HeartIcon, ReplyIcon } from "../components/Icons"; function getNotificationRoute(n) { if (n.type === "reply" && n.subject?.inReplyTo) { return `/annotation/${encodeURIComponent(n.subject.inReplyTo)}`; } if (!n.subjectUri) return "/"; if (n.subjectUri.includes("at.margin.bookmark")) { return `/bookmarks`; } if (n.subjectUri.includes("at.margin.highlight")) { return `/highlights`; } return `/annotation/${encodeURIComponent(n.subjectUri)}`; } export default function Notifications() { const { user } = useAuth(); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!user?.did) return; async function load() { try { setLoading(true); const data = await getNotifications(); setNotifications(data.items || []); await markNotificationsRead(); } catch (err) { setError(err.message); } finally { setLoading(false); } } load(); }, [user?.did]); const formatTime = (dateStr) => { const date = new Date(dateStr); const now = new Date(); const diffMs = now - date; const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMs / 3600000); const diffDays = Math.floor(diffMs / 86400000); if (diffMins < 1) return "just now"; if (diffMins < 60) return `${diffMins}m ago`; if (diffHours < 24) return `${diffHours}h ago`; if (diffDays < 7) return `${diffDays}d ago`; return date.toLocaleDateString(); }; const getNotificationIcon = (type) => { switch (type) { case "like": return ; case "reply": return ; default: return ; } }; const getNotificationText = (n) => { const name = n.actor?.displayName || n.actor?.handle || "Unknown"; const handle = n.actor?.handle; switch (n.type) { case "like": return ( e.stopPropagation()} > {name} {" "} liked your annotation ); case "reply": return ( e.stopPropagation()} > {name} {" "} replied to your annotation ); default: return ( e.stopPropagation()} > {name} {" "} interacted with your content ); } }; if (!user) { return (

Notifications

Sign in to see notifications

Get notified when people like or reply to your content

); } return (

Notifications

Likes and replies on your annotations

{loading && (
)} {error && (

Error: {error}

)} {!loading && !error && notifications.length === 0 && (

No notifications yet

When someone likes or replies to your content, you'll see it here

)} {!loading && !error && notifications.length > 0 && (
{notifications.map((n, i) => (
{n.actor?.avatar ? ( {n.actor.handle} ) : (
{(n.actor?.handle || "?")[0].toUpperCase()}
)}
{getNotificationIcon(n.type)}

{getNotificationText(n)}

{formatTime(n.createdAt)}
))}
)}
); }