import React, { useState, useEffect, useRef } from 'react';
import {
Bell,
Check,
X,
CheckCheck,
UserCheck,
UserX,
FileText,
GitPullRequest,
CheckCircle2,
Clock,
Layers,
Sparkles,
ExternalLink
} from 'lucide-react';
import { api } from '../api/client';
export default function NotificationsCenter({ onSelectWorkspace, onOpenProfile, showToast = () => {} }) {
const [isOpen, setIsOpen] = useState(false);
const [notifications, setNotifications] = useState([]);
const [unreadCount, setUnreadCount] = useState(0);
const [filter, setFilter] = useState('ALL'); // 'ALL' | 'REQUESTS' | 'ACTIVITY'
const [actionInProgress, setActionInProgress] = useState({}); // { [memberId]: 'APPROVE' | 'REJECT' }
const [resolvedActions, setResolvedActions] = useState({}); // { [memberId]: 'APPROVED' | 'REJECTED' }
const dropdownRef = useRef(null);
// Poll for notifications every 8 seconds
const fetchNotifications = async () => {
try {
const data = await api.getNotifications();
if (data && Array.isArray(data.notifications)) {
setNotifications(data.notifications);
setUnreadCount(data.unread_count || 0);
}
} catch (err) {
// Quiet fail on network hiccups
}
};
useEffect(() => {
fetchNotifications();
const interval = setInterval(fetchNotifications, 8000);
return () => clearInterval(interval);
}, []);
// Close dropdown on click outside
useEffect(() => {
function handleClickOutside(e) {
if (dropdownRef.current && !dropdownRef.current.contains(e.target)) {
setIsOpen(false);
}
}
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => document.removeEventListener('mousedown', handleClickOutside);
}, [isOpen]);
const handleMarkAllRead = async () => {
try {
await api.markAllNotificationsRead();
setNotifications((prev) => prev.map((n) => ({ ...n, is_read: true })));
setUnreadCount(0);
showToast('All notifications marked as read.', 'info');
} catch (err) {
showToast('Failed to mark notifications read.', 'error');
}
};
const handleMarkRead = async (notifId) => {
try {
await api.markNotificationRead(notifId);
setNotifications((prev) =>
prev.map((n) => (n.id === notifId ? { ...n, is_read: true } : n))
);
setUnreadCount((prev) => Math.max(0, prev - 1));
} catch {
// Ignore
}
};
const handleRespondRequest = async (workspaceId, memberId, decision) => {
setActionInProgress((prev) => ({ ...prev, [memberId]: decision }));
try {
await api.respondWorkspaceRequest(workspaceId, memberId, decision, 'EDITOR');
setResolvedActions((prev) => ({ ...prev, [memberId]: decision === 'APPROVE' ? 'APPROVED' : 'REJECTED' }));
setUnreadCount((prev) => Math.max(0, prev - 1));
showToast(
decision === 'APPROVE'
? 'Collaborator request approved! They can now collaborate.'
: 'Collaborator request declined.',
decision === 'APPROVE' ? 'success' : 'info'
);
setTimeout(fetchNotifications, 1000);
} catch (err) {
showToast(`Error responding to request: ${err.message || err}`, 'error');
} finally {
setActionInProgress((prev) => {
const next = { ...prev };
delete next[memberId];
return next;
});
}
};
const filteredNotifs = notifications.filter((n) => {
if (filter === 'REQUESTS') return n.type === 'ACCESS_REQUEST';
if (filter === 'ACTIVITY') return n.type !== 'ACCESS_REQUEST';
return true;
});
const requestsCount = notifications.filter((n) => n.type === 'ACCESS_REQUEST').length;
return (
{/* Bell Button */}
{/* Floating Notifications Dropdown Popover */}
{isOpen && (
{/* Header */}
Notifications
{unreadCount > 0 && (
{unreadCount} new
)}
{unreadCount > 0 && (
)}
{/* Filter Pills */}
{/* Notifications Items List */}
{filteredNotifs.length === 0 ? (
All caught up!
No notifications matching this filter.
) : (
filteredNotifs.map((notif) => {
const isActionable = notif.is_actionable && notif.type === 'ACCESS_REQUEST';
const resolvedStatus = notif.member_id ? resolvedActions[notif.member_id] : null;
const inProgress = notif.member_id ? actionInProgress[notif.member_id] : null;
return (
{
if (!notif.is_read) handleMarkRead(notif.id);
}}
className={`p-3.5 transition-colors hover:bg-slate-800/40 relative ${
!notif.is_read ? 'bg-teal-950/15' : ''
}`}
>
{/* Unread indicator dot */}
{!notif.is_read && (
)}
{/* Icon */}
{notif.type === 'ACCESS_REQUEST' && (
)}
{notif.type === 'ACCESS_APPROVED' && (
)}
{notif.type === 'DOCUMENT_STATUS' && (
)}
{notif.type === 'PR_UPDATE' && (
)}
{/* Content */}
{notif.title}
{notif.workspace_name && (
{notif.workspace_name}
)}
{notif.message}
{/* Interactive Approval / Rejection Actions for Access Requests */}
{isActionable && (
{resolvedStatus ? (
{resolvedStatus === 'APPROVED' ? (
<>
Approved as Editor
>
) : (
<>
Request Declined
>
)}
) : (
)}
)}
{/* Navigation link if workspace actionable */}
{!isActionable && notif.workspace_id && onSelectWorkspace && (
)}
{/* Time */}
{notif.created_at && (
{new Date(notif.created_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} ยท{' '}
{new Date(notif.created_at).toLocaleDateString([], { month: 'short', day: 'numeric' })}
)}
);
})
)}
{/* Footer */}
DiffWeave Live Notification Stream
)}
);
}