import { useState, useEffect } from "react"; import Layout from "@/components/Layout"; import SEO from "@/components/SEO"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Bell, Pin, Loader2, Eye, EyeOff } from "lucide-react"; import { useAuth } from "@/contexts/AuthContext"; import { aethexToast } from "@/lib/aethex-toast"; interface Announcement { id: string; title: string; content: string; category: string; priority: string; is_pinned: boolean; is_read: boolean; published_at: string; author?: { full_name: string; avatar_url: string }; } const getPriorityColor = (priority: string) => { switch (priority) { case "urgent": return "bg-red-500/20 text-red-300 border-red-500/30"; case "high": return "bg-orange-500/20 text-orange-300 border-orange-500/30"; case "normal": return "bg-blue-500/20 text-blue-300 border-blue-500/30"; case "low": return "bg-slate-500/20 text-slate-300 border-slate-500/30"; default: return "bg-slate-500/20 text-slate-300"; } }; const getCategoryColor = (category: string) => { switch (category) { case "urgent": return "bg-red-600"; case "policy": return "bg-purple-600"; case "event": return "bg-blue-600"; case "celebration": return "bg-green-600"; default: return "bg-slate-600"; } }; export default function StaffAnnouncements() { const { session } = useAuth(); const [loading, setLoading] = useState(true); const [announcements, setAnnouncements] = useState([]); const [selectedCategory, setSelectedCategory] = useState("all"); const [showRead, setShowRead] = useState(true); useEffect(() => { if (session?.access_token) fetchAnnouncements(); }, [session?.access_token]); const fetchAnnouncements = async () => { try { const res = await fetch("/api/staff/announcements", { headers: { Authorization: `Bearer ${session?.access_token}` }, }); if (res.ok) { const data = await res.json(); setAnnouncements(data.announcements || []); } } catch (err) { aethexToast.error("Failed to load announcements"); } finally { setLoading(false); } }; const markAsRead = async (id: string) => { try { await fetch("/api/staff/announcements", { method: "POST", headers: { Authorization: `Bearer ${session?.access_token}`, "Content-Type": "application/json", }, body: JSON.stringify({ action: "mark_read", id }), }); setAnnouncements(prev => prev.map(a => a.id === id ? { ...a, is_read: true } : a)); } catch (err) { console.error(err); } }; const formatDate = (date: string) => { const d = new Date(date); const now = new Date(); const diff = now.getTime() - d.getTime(); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); if (days === 0) return "Today"; if (days === 1) return "Yesterday"; if (days < 7) return `${days} days ago`; return d.toLocaleDateString(); }; const categories = ["all", ...new Set(announcements.map(a => a.category))]; const filtered = announcements.filter(a => { const matchesCategory = selectedCategory === "all" || a.category === selectedCategory; const matchesRead = showRead || !a.is_read; return matchesCategory && matchesRead; }); const pinned = filtered.filter(a => a.is_pinned); const unpinned = filtered.filter(a => !a.is_pinned); if (loading) { return (
); } return (

Announcements

Company news, updates, and important information

{categories.map(cat => ( ))}
{pinned.length > 0 && (

Pinned

{pinned.map(ann => ( !ann.is_read && markAsRead(ann.id)} >
{ann.title} by {ann.author?.full_name || "Staff"} • {formatDate(ann.published_at)}
{ann.priority}

{ann.content}

{ann.category}
))}
)} {unpinned.length > 0 && (

Recent Announcements

{unpinned.map(ann => ( !ann.is_read && markAsRead(ann.id)} >
{ann.title} by {ann.author?.full_name || "Staff"} • {formatDate(ann.published_at)}
{ann.priority}

{ann.content}

{ann.category}
))}
)} {filtered.length === 0 && (

No announcements found

)}
); }