Files
compor-academy/pages/NotificationsPage.tsx
Cauê Faleiros 50905eefd7
All checks were successful
CI / Validate frontend and API (push) Successful in 3m5s
CI / Build and publish Docker images (push) Successful in 1m42s
feat: add 2FA, secure cookies and publishing tools
2026-11-06 10:29:37 -03:00

130 lines
3.9 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { BellIcon, CircleNotchIcon } from '@phosphor-icons/react';
import { useToast } from '../context/ToastContext';
import {
NotificationFeed,
NotificationItem,
getNotifications,
markAllNotificationsRead,
markNotificationRead,
} from '../services/notifications';
import { useIsMobile } from '../hooks/useMediaQuery';
const formatWhen = (value: string) =>
new Intl.DateTimeFormat('pt-BR', { day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit' }).format(new Date(value));
export const NotificationsPage: React.FC = () => {
const { showToast } = useToast();
const isMobile = useIsMobile();
const [feed, setFeed] = useState<NotificationFeed | null>(null);
const [loading, setLoading] = useState(true);
const load = async () => {
setLoading(true);
try {
setFeed(await getNotifications());
} catch {
setFeed({ items: [], unread: 0 });
showToast('Não foi possível carregar suas notificações.', 'error');
} finally {
setLoading(false);
}
};
useEffect(() => {
void load();
}, []);
const markAll = async () => {
try {
await markAllNotificationsRead();
setFeed((current) =>
current
? { ...current, unread: 0, items: current.items.map((item) => ({ ...item, readAt: item.readAt || new Date().toISOString() })) }
: current,
);
window.dispatchEvent(new Event('compor:notifications-updated'));
} catch {
showToast('Não foi possível marcar as notificações como lidas.', 'error');
}
};
const open = (item: NotificationItem) => {
if (item.readAt) return;
void markNotificationRead(item.id)
.then(() => {
setFeed((current) =>
current
? {
...current,
unread: Math.max(0, current.unread - 1),
items: current.items.map((entry) => (entry.id === item.id ? { ...entry, readAt: new Date().toISOString() } : entry)),
}
: current,
);
window.dispatchEvent(new Event('compor:notifications-updated'));
})
.catch(() => undefined);
};
const list = loading ? (
<div className="loading">
<CircleNotchIcon className="ph spin" />
</div>
) : feed?.items.length ? (
<div className="notification-list">
{feed.items.map((item) => (
<Link className={`notification${item.readAt ? '' : ' unread'}`} to={item.href} key={item.id} onClick={() => open(item)}>
<span className="n-ic">
<BellIcon className="ph" />
</span>
<span className="n-copy">
<b>{item.title}</b>
{item.body && <p>{item.body}</p>}
</span>
<small>{formatWhen(item.createdAt)}</small>
</Link>
))}
</div>
) : (
<div className="empty notification-empty">
<BellIcon className="ph" />
<h2>Nenhuma notificação</h2>
<p>Quando alguém responder seus comentários ou fizer uma pergunta nos seus cursos, você verá aqui.</p>
</div>
);
if (isMobile)
return (
<main className="m-page m-gut enter">
<header className="m-large">
<h1>Notificações</h1>
{Boolean(feed?.unread) && (
<button className="link" type="button" onClick={() => void markAll()}>
Marcar lidas
</button>
)}
</header>
{list}
</main>
);
return (
<main className="wrap page enter">
<div className="ptitle">
<div>
<h1>Notificações</h1>
<p>{feed?.unread ? `${feed.unread} não ${feed.unread === 1 ? 'lida' : 'lidas'}` : 'Tudo em dia.'}</p>
</div>
{Boolean(feed?.unread) && (
<button className="btn sec" type="button" onClick={() => void markAll()}>
Marcar todas como lidas
</button>
)}
</div>
{list}
</main>
);
};