"use client";
import { useState, useEffect } from "react";
import { Box, Paper, Text, Group, CloseButton, Badge, ActionIcon, Stack, Transition } from "@mantine/core";
import { IconBell, IconChevronRight } from "@tabler/icons-react";
import { usePathname, useRouter } from "next/navigation";
interface NewsItem {
id: string | number;
type: "berita" | "pengumuman";
title: string;
content: string;
timestamp?: string | Date;
}
interface ModernNewsNotificationProps {
news: NewsItem[];
autoShowDelay?: number;
}
function stripHtml(html: string): string {
return html
.replace(/<[^>]+>/g, '')
.replace(/ /gi, ' ')
.replace(/&/gi, '&')
.replace(/\s+/g, ' ')
.trim();
}
export default function ModernNewsNotification({
news = [],
autoShowDelay = 2000
}: ModernNewsNotificationProps) {
const router = useRouter();
const [toastVisible, setToastVisible] = useState(false);
const [widgetOpen, setWidgetOpen] = useState(false);
const [hasNewNotifications, setHasNewNotifications] = useState(true);
const [hasShownToast, setHasShownToast] = useState(false);
const [iconVisible, setIconVisible] = useState(true);
const pathname = usePathname();
// Auto show toast on page load
useEffect(() => {
if (news.length > 0 && !toastVisible && !hasShownToast) {
const timer = setTimeout(() => {
setToastVisible(true);
setHasShownToast(true);
}, autoShowDelay);
return () => clearTimeout(timer);
}
}, [news.length, autoShowDelay, toastVisible, hasShownToast]);
// Auto hide toast after 8 seconds
useEffect(() => {
if (toastVisible) {
const timer = setTimeout(() => {
setToastVisible(false);
}, 8000);
return () => clearTimeout(timer);
}
}, [toastVisible]);
// Enhanced scroll handler with better thresholds
useEffect(() => {
let lastScrollY = window.scrollY;
const HIDE_THRESHOLD = 100; // Mulai hide saat scroll > 100px
const SHOW_THRESHOLD = 50; // Hanya show ketika benar-benar di atas (< 50px)
const handleScroll = () => {
const currentScrollY = window.scrollY;
const scrollDirection = currentScrollY > lastScrollY ? 'down' : 'up';
// Logic untuk hide/show icon
if (scrollDirection === 'down' && currentScrollY > HIDE_THRESHOLD) {
// Scroll ke bawah dan sudah melewati threshold → hide
setIconVisible(false);
} else if (scrollDirection === 'up' && currentScrollY < SHOW_THRESHOLD) {
// Scroll ke atas dan sudah di posisi paling atas → show
setIconVisible(true);
}
// Hide toast saat scroll ke bawah melewati 150px
if (currentScrollY > 150 && toastVisible) {
setToastVisible(false);
}
lastScrollY = currentScrollY;
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);
}, [toastVisible]);
const currentNews = news[0];
// Handle notification click
const handleNotificationClick = (item: NewsItem) => {
setWidgetOpen(false);
if (item.type === "berita") {
router.push("/darmasaba/desa/berita/semua");
} else if (item.type === "pengumuman") {
router.push("/darmasaba/desa/pengumuman");
}
};
const handleLihatSemua = () => {
setToastVisible(false);
setWidgetOpen(true);
setHasNewNotifications(false);
};
// Only show on landing page
if (pathname !== '/darmasaba') {
return null;
}
return (
<>
{/* Floating Bell Icon */}
{(transitionStyles) => (
{
setWidgetOpen(!widgetOpen);
setHasNewNotifications(false);
}}
style={{
width: "60px",
height: "60px",
boxShadow: "0 4px 20px rgba(0,0,0,0.15)",
position: "relative",
}}
>
{hasNewNotifications && news.length > 0 && (
{news.length}
)}
)}
{/* Widget Panel */}
{(styles) => (
Berita & Pengumuman
setWidgetOpen(false)}
variant="transparent"
c="white"
/>
{news.length === 0 ? (
Tidak ada berita terbaru
) : (
{news.map((item, index) => (
{
e.currentTarget.style.borderColor = "#1e5a7e";
e.currentTarget.style.transform = "translateY(-2px)";
e.currentTarget.style.boxShadow = "0 4px 12px rgba(0,0,0,0.08)";
}}
onMouseLeave={(e) => {
e.currentTarget.style.borderColor = "#e9ecef";
e.currentTarget.style.transform = "translateY(0)";
e.currentTarget.style.boxShadow = "none";
}}
onClick={() => handleNotificationClick(item)}
>
{item.type === "berita" ? "📰 Berita" : "📢 Pengumuman"}
{item.title || "Tanpa Judul"}
{stripHtml(item.content).substring(0, 100)}...
))}
)}
)}
{/* Toast Notification */}
{(styles) => (
{currentNews?.type === "berita" ? "Berita Terbaru" : "Pengumuman"}
{
e.stopPropagation();
setToastVisible(false);
}}
size="sm"
/>
{currentNews?.title || "Informasi Terbaru"}
{stripHtml(currentNews?.content || "")}
{news.length > 1 ? `${news.length} berita tersedia` : '1 berita'}
Lihat Semua →
)}
>
);
}