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
Get notified when people like or reply to your content
Likes and replies on your annotations
Error: {error}
When someone likes or replies to your content, you'll see it here
{getNotificationText(n)}
{formatTime(n.createdAt)}